Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1 | /* |
| 2 | * x_tables core - Backend for {ip,ip6,arp}_tables |
| 3 | * |
| 4 | * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org> |
| 5 | * |
| 6 | * Based on existing ip_tables code which is |
| 7 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling |
| 8 | * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | */ |
| 15 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 16 | #include <linux/kernel.h> |
| 17 | #include <linux/socket.h> |
| 18 | #include <linux/net.h> |
| 19 | #include <linux/proc_fs.h> |
| 20 | #include <linux/seq_file.h> |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/vmalloc.h> |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Al Viro | d7fe0f2 | 2006-12-03 23:15:30 -0500 | [diff] [blame] | 24 | #include <linux/mm.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 25 | #include <net/net_namespace.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 26 | |
| 27 | #include <linux/netfilter/x_tables.h> |
| 28 | #include <linux/netfilter_arp.h> |
| 29 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 30 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 31 | MODULE_LICENSE("GPL"); |
| 32 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
| 33 | MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module"); |
| 34 | |
| 35 | #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1)) |
| 36 | |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 37 | struct compat_delta { |
| 38 | struct compat_delta *next; |
| 39 | unsigned int offset; |
| 40 | short delta; |
| 41 | }; |
| 42 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 43 | struct xt_af { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 44 | struct mutex mutex; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 45 | struct list_head match; |
| 46 | struct list_head target; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 47 | #ifdef CONFIG_COMPAT |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 48 | struct mutex compat_mutex; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 49 | struct compat_delta *compat_offsets; |
| 50 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static struct xt_af *xt; |
| 54 | |
| 55 | #ifdef DEBUG_IP_FIREWALL_USER |
| 56 | #define duprintf(format, args...) printk(format , ## args) |
| 57 | #else |
| 58 | #define duprintf(format, args...) |
| 59 | #endif |
| 60 | |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 61 | static const char *xt_prefix[NPROTO] = { |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 62 | [AF_INET] = "ip", |
| 63 | [AF_INET6] = "ip6", |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 64 | [NF_ARP] = "arp", |
| 65 | }; |
| 66 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 67 | /* Registration hooks for targets. */ |
| 68 | int |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 69 | xt_register_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 70 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 71 | int ret, af = target->family; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 72 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 73 | ret = mutex_lock_interruptible(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 74 | if (ret != 0) |
| 75 | return ret; |
| 76 | list_add(&target->list, &xt[af].target); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 77 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 78 | return ret; |
| 79 | } |
| 80 | EXPORT_SYMBOL(xt_register_target); |
| 81 | |
| 82 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 83 | xt_unregister_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 84 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 85 | int af = target->family; |
| 86 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 87 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 88 | list_del(&target->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 89 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 90 | } |
| 91 | EXPORT_SYMBOL(xt_unregister_target); |
| 92 | |
| 93 | int |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 94 | xt_register_targets(struct xt_target *target, unsigned int n) |
| 95 | { |
| 96 | unsigned int i; |
| 97 | int err = 0; |
| 98 | |
| 99 | for (i = 0; i < n; i++) { |
| 100 | err = xt_register_target(&target[i]); |
| 101 | if (err) |
| 102 | goto err; |
| 103 | } |
| 104 | return err; |
| 105 | |
| 106 | err: |
| 107 | if (i > 0) |
| 108 | xt_unregister_targets(target, i); |
| 109 | return err; |
| 110 | } |
| 111 | EXPORT_SYMBOL(xt_register_targets); |
| 112 | |
| 113 | void |
| 114 | xt_unregister_targets(struct xt_target *target, unsigned int n) |
| 115 | { |
| 116 | unsigned int i; |
| 117 | |
| 118 | for (i = 0; i < n; i++) |
| 119 | xt_unregister_target(&target[i]); |
| 120 | } |
| 121 | EXPORT_SYMBOL(xt_unregister_targets); |
| 122 | |
| 123 | int |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 124 | xt_register_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 125 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 126 | int ret, af = match->family; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 127 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 128 | ret = mutex_lock_interruptible(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 129 | if (ret != 0) |
| 130 | return ret; |
| 131 | |
| 132 | list_add(&match->list, &xt[af].match); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 133 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | EXPORT_SYMBOL(xt_register_match); |
| 138 | |
| 139 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 140 | xt_unregister_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 141 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 142 | int af = match->family; |
| 143 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 144 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 145 | list_del(&match->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 146 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 147 | } |
| 148 | EXPORT_SYMBOL(xt_unregister_match); |
| 149 | |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 150 | int |
| 151 | xt_register_matches(struct xt_match *match, unsigned int n) |
| 152 | { |
| 153 | unsigned int i; |
| 154 | int err = 0; |
| 155 | |
| 156 | for (i = 0; i < n; i++) { |
| 157 | err = xt_register_match(&match[i]); |
| 158 | if (err) |
| 159 | goto err; |
| 160 | } |
| 161 | return err; |
| 162 | |
| 163 | err: |
| 164 | if (i > 0) |
| 165 | xt_unregister_matches(match, i); |
| 166 | return err; |
| 167 | } |
| 168 | EXPORT_SYMBOL(xt_register_matches); |
| 169 | |
| 170 | void |
| 171 | xt_unregister_matches(struct xt_match *match, unsigned int n) |
| 172 | { |
| 173 | unsigned int i; |
| 174 | |
| 175 | for (i = 0; i < n; i++) |
| 176 | xt_unregister_match(&match[i]); |
| 177 | } |
| 178 | EXPORT_SYMBOL(xt_unregister_matches); |
| 179 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * These are weird, but module loading must not be done with mutex |
| 183 | * held (since they will register), and we have to have a single |
| 184 | * function to use try_then_request_module(). |
| 185 | */ |
| 186 | |
| 187 | /* Find match, grabs ref. Returns ERR_PTR() on error. */ |
| 188 | struct xt_match *xt_find_match(int af, const char *name, u8 revision) |
| 189 | { |
| 190 | struct xt_match *m; |
| 191 | int err = 0; |
| 192 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 193 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 194 | return ERR_PTR(-EINTR); |
| 195 | |
| 196 | list_for_each_entry(m, &xt[af].match, list) { |
| 197 | if (strcmp(m->name, name) == 0) { |
| 198 | if (m->revision == revision) { |
| 199 | if (try_module_get(m->me)) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 200 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 201 | return m; |
| 202 | } |
| 203 | } else |
| 204 | err = -EPROTOTYPE; /* Found something. */ |
| 205 | } |
| 206 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 207 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 208 | return ERR_PTR(err); |
| 209 | } |
| 210 | EXPORT_SYMBOL(xt_find_match); |
| 211 | |
| 212 | /* Find target, grabs ref. Returns ERR_PTR() on error. */ |
| 213 | struct xt_target *xt_find_target(int af, const char *name, u8 revision) |
| 214 | { |
| 215 | struct xt_target *t; |
| 216 | int err = 0; |
| 217 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 218 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 219 | return ERR_PTR(-EINTR); |
| 220 | |
| 221 | list_for_each_entry(t, &xt[af].target, list) { |
| 222 | if (strcmp(t->name, name) == 0) { |
| 223 | if (t->revision == revision) { |
| 224 | if (try_module_get(t->me)) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 225 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 226 | return t; |
| 227 | } |
| 228 | } else |
| 229 | err = -EPROTOTYPE; /* Found something. */ |
| 230 | } |
| 231 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 232 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 233 | return ERR_PTR(err); |
| 234 | } |
| 235 | EXPORT_SYMBOL(xt_find_target); |
| 236 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 237 | struct xt_target *xt_request_find_target(int af, const char *name, u8 revision) |
| 238 | { |
| 239 | struct xt_target *target; |
| 240 | |
| 241 | target = try_then_request_module(xt_find_target(af, name, revision), |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 242 | "%st_%s", xt_prefix[af], name); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 243 | if (IS_ERR(target) || !target) |
| 244 | return NULL; |
| 245 | return target; |
| 246 | } |
| 247 | EXPORT_SYMBOL_GPL(xt_request_find_target); |
| 248 | |
| 249 | static int match_revfn(int af, const char *name, u8 revision, int *bestp) |
| 250 | { |
| 251 | struct xt_match *m; |
| 252 | int have_rev = 0; |
| 253 | |
| 254 | list_for_each_entry(m, &xt[af].match, list) { |
| 255 | if (strcmp(m->name, name) == 0) { |
| 256 | if (m->revision > *bestp) |
| 257 | *bestp = m->revision; |
| 258 | if (m->revision == revision) |
| 259 | have_rev = 1; |
| 260 | } |
| 261 | } |
| 262 | return have_rev; |
| 263 | } |
| 264 | |
| 265 | static int target_revfn(int af, const char *name, u8 revision, int *bestp) |
| 266 | { |
| 267 | struct xt_target *t; |
| 268 | int have_rev = 0; |
| 269 | |
| 270 | list_for_each_entry(t, &xt[af].target, list) { |
| 271 | if (strcmp(t->name, name) == 0) { |
| 272 | if (t->revision > *bestp) |
| 273 | *bestp = t->revision; |
| 274 | if (t->revision == revision) |
| 275 | have_rev = 1; |
| 276 | } |
| 277 | } |
| 278 | return have_rev; |
| 279 | } |
| 280 | |
| 281 | /* Returns true or false (if no such extension at all) */ |
| 282 | int xt_find_revision(int af, const char *name, u8 revision, int target, |
| 283 | int *err) |
| 284 | { |
| 285 | int have_rev, best = -1; |
| 286 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 287 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 288 | *err = -EINTR; |
| 289 | return 1; |
| 290 | } |
| 291 | if (target == 1) |
| 292 | have_rev = target_revfn(af, name, revision, &best); |
| 293 | else |
| 294 | have_rev = match_revfn(af, name, revision, &best); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 295 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 296 | |
| 297 | /* Nothing at all? Return 0 to try loading module. */ |
| 298 | if (best == -1) { |
| 299 | *err = -ENOENT; |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | *err = best; |
| 304 | if (!have_rev) |
| 305 | *err = -EPROTONOSUPPORT; |
| 306 | return 1; |
| 307 | } |
| 308 | EXPORT_SYMBOL_GPL(xt_find_revision); |
| 309 | |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 310 | int xt_check_match(const struct xt_match *match, unsigned short family, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 311 | unsigned int size, const char *table, unsigned int hook_mask, |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 312 | unsigned short proto, int inv_proto) |
| 313 | { |
| 314 | if (XT_ALIGN(match->matchsize) != size) { |
| 315 | printk("%s_tables: %s match: invalid size %Zu != %u\n", |
| 316 | xt_prefix[family], match->name, |
| 317 | XT_ALIGN(match->matchsize), size); |
| 318 | return -EINVAL; |
| 319 | } |
| 320 | if (match->table && strcmp(match->table, table)) { |
| 321 | printk("%s_tables: %s match: only valid in %s table, not %s\n", |
| 322 | xt_prefix[family], match->name, match->table, table); |
| 323 | return -EINVAL; |
| 324 | } |
| 325 | if (match->hooks && (hook_mask & ~match->hooks) != 0) { |
Balazs Scheidler | 5faf415 | 2007-07-07 22:41:01 -0700 | [diff] [blame] | 326 | printk("%s_tables: %s match: bad hook_mask %u/%u\n", |
| 327 | xt_prefix[family], match->name, hook_mask, match->hooks); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 328 | return -EINVAL; |
| 329 | } |
| 330 | if (match->proto && (match->proto != proto || inv_proto)) { |
| 331 | printk("%s_tables: %s match: only valid for protocol %u\n", |
| 332 | xt_prefix[family], match->name, match->proto); |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | EXPORT_SYMBOL_GPL(xt_check_match); |
| 338 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 339 | #ifdef CONFIG_COMPAT |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 340 | int xt_compat_add_offset(int af, unsigned int offset, short delta) |
| 341 | { |
| 342 | struct compat_delta *tmp; |
| 343 | |
| 344 | tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL); |
| 345 | if (!tmp) |
| 346 | return -ENOMEM; |
| 347 | |
| 348 | tmp->offset = offset; |
| 349 | tmp->delta = delta; |
| 350 | |
| 351 | if (xt[af].compat_offsets) { |
| 352 | tmp->next = xt[af].compat_offsets->next; |
| 353 | xt[af].compat_offsets->next = tmp; |
| 354 | } else { |
| 355 | xt[af].compat_offsets = tmp; |
| 356 | tmp->next = NULL; |
| 357 | } |
| 358 | return 0; |
| 359 | } |
| 360 | EXPORT_SYMBOL_GPL(xt_compat_add_offset); |
| 361 | |
| 362 | void xt_compat_flush_offsets(int af) |
| 363 | { |
| 364 | struct compat_delta *tmp, *next; |
| 365 | |
| 366 | if (xt[af].compat_offsets) { |
| 367 | for (tmp = xt[af].compat_offsets; tmp; tmp = next) { |
| 368 | next = tmp->next; |
| 369 | kfree(tmp); |
| 370 | } |
| 371 | xt[af].compat_offsets = NULL; |
| 372 | } |
| 373 | } |
| 374 | EXPORT_SYMBOL_GPL(xt_compat_flush_offsets); |
| 375 | |
| 376 | short xt_compat_calc_jump(int af, unsigned int offset) |
| 377 | { |
| 378 | struct compat_delta *tmp; |
| 379 | short delta; |
| 380 | |
| 381 | for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next) |
| 382 | if (tmp->offset < offset) |
| 383 | delta += tmp->delta; |
| 384 | return delta; |
| 385 | } |
| 386 | EXPORT_SYMBOL_GPL(xt_compat_calc_jump); |
| 387 | |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 388 | int xt_compat_match_offset(struct xt_match *match) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 389 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 390 | u_int16_t csize = match->compatsize ? : match->matchsize; |
| 391 | return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 392 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 393 | EXPORT_SYMBOL_GPL(xt_compat_match_offset); |
| 394 | |
Patrick McHardy | 8956695 | 2007-12-17 21:46:40 -0800 | [diff] [blame] | 395 | int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr, |
Patrick McHardy | b0a6363 | 2008-01-31 04:10:18 -0800 | [diff] [blame] | 396 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 397 | { |
| 398 | struct xt_match *match = m->u.kernel.match; |
| 399 | struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m; |
| 400 | int pad, off = xt_compat_match_offset(match); |
| 401 | u_int16_t msize = cm->u.user.match_size; |
| 402 | |
| 403 | m = *dstptr; |
| 404 | memcpy(m, cm, sizeof(*cm)); |
| 405 | if (match->compat_from_user) |
| 406 | match->compat_from_user(m->data, cm->data); |
| 407 | else |
| 408 | memcpy(m->data, cm->data, msize - sizeof(*cm)); |
| 409 | pad = XT_ALIGN(match->matchsize) - match->matchsize; |
| 410 | if (pad > 0) |
| 411 | memset(m->data + match->matchsize, 0, pad); |
| 412 | |
| 413 | msize += off; |
| 414 | m->u.user.match_size = msize; |
| 415 | |
| 416 | *size += off; |
| 417 | *dstptr += msize; |
Patrick McHardy | 8956695 | 2007-12-17 21:46:40 -0800 | [diff] [blame] | 418 | return 0; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 419 | } |
| 420 | EXPORT_SYMBOL_GPL(xt_compat_match_from_user); |
| 421 | |
| 422 | int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr, |
Patrick McHardy | b0a6363 | 2008-01-31 04:10:18 -0800 | [diff] [blame] | 423 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 424 | { |
| 425 | struct xt_match *match = m->u.kernel.match; |
| 426 | struct compat_xt_entry_match __user *cm = *dstptr; |
| 427 | int off = xt_compat_match_offset(match); |
| 428 | u_int16_t msize = m->u.user.match_size - off; |
| 429 | |
| 430 | if (copy_to_user(cm, m, sizeof(*cm)) || |
Patrick McHardy | a18aa31 | 2007-12-12 10:35:16 -0800 | [diff] [blame] | 431 | put_user(msize, &cm->u.user.match_size) || |
| 432 | copy_to_user(cm->u.user.name, m->u.kernel.match->name, |
| 433 | strlen(m->u.kernel.match->name) + 1)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 434 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 435 | |
| 436 | if (match->compat_to_user) { |
| 437 | if (match->compat_to_user((void __user *)cm->data, m->data)) |
| 438 | return -EFAULT; |
| 439 | } else { |
| 440 | if (copy_to_user(cm->data, m->data, msize - sizeof(*cm))) |
| 441 | return -EFAULT; |
| 442 | } |
| 443 | |
| 444 | *size -= off; |
| 445 | *dstptr += msize; |
| 446 | return 0; |
| 447 | } |
| 448 | EXPORT_SYMBOL_GPL(xt_compat_match_to_user); |
| 449 | #endif /* CONFIG_COMPAT */ |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 450 | |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 451 | int xt_check_target(const struct xt_target *target, unsigned short family, |
| 452 | unsigned int size, const char *table, unsigned int hook_mask, |
| 453 | unsigned short proto, int inv_proto) |
| 454 | { |
| 455 | if (XT_ALIGN(target->targetsize) != size) { |
| 456 | printk("%s_tables: %s target: invalid size %Zu != %u\n", |
| 457 | xt_prefix[family], target->name, |
| 458 | XT_ALIGN(target->targetsize), size); |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | if (target->table && strcmp(target->table, table)) { |
| 462 | printk("%s_tables: %s target: only valid in %s table, not %s\n", |
| 463 | xt_prefix[family], target->name, target->table, table); |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | if (target->hooks && (hook_mask & ~target->hooks) != 0) { |
Balazs Scheidler | 5faf415 | 2007-07-07 22:41:01 -0700 | [diff] [blame] | 467 | printk("%s_tables: %s target: bad hook_mask %u/%u\n", |
| 468 | xt_prefix[family], target->name, hook_mask, |
| 469 | target->hooks); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 470 | return -EINVAL; |
| 471 | } |
| 472 | if (target->proto && (target->proto != proto || inv_proto)) { |
| 473 | printk("%s_tables: %s target: only valid for protocol %u\n", |
| 474 | xt_prefix[family], target->name, target->proto); |
| 475 | return -EINVAL; |
| 476 | } |
| 477 | return 0; |
| 478 | } |
| 479 | EXPORT_SYMBOL_GPL(xt_check_target); |
| 480 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 481 | #ifdef CONFIG_COMPAT |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 482 | int xt_compat_target_offset(struct xt_target *target) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 483 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 484 | u_int16_t csize = target->compatsize ? : target->targetsize; |
| 485 | return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 486 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 487 | EXPORT_SYMBOL_GPL(xt_compat_target_offset); |
| 488 | |
| 489 | void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr, |
Patrick McHardy | b0a6363 | 2008-01-31 04:10:18 -0800 | [diff] [blame] | 490 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 491 | { |
| 492 | struct xt_target *target = t->u.kernel.target; |
| 493 | struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t; |
| 494 | int pad, off = xt_compat_target_offset(target); |
| 495 | u_int16_t tsize = ct->u.user.target_size; |
| 496 | |
| 497 | t = *dstptr; |
| 498 | memcpy(t, ct, sizeof(*ct)); |
| 499 | if (target->compat_from_user) |
| 500 | target->compat_from_user(t->data, ct->data); |
| 501 | else |
| 502 | memcpy(t->data, ct->data, tsize - sizeof(*ct)); |
| 503 | pad = XT_ALIGN(target->targetsize) - target->targetsize; |
| 504 | if (pad > 0) |
| 505 | memset(t->data + target->targetsize, 0, pad); |
| 506 | |
| 507 | tsize += off; |
| 508 | t->u.user.target_size = tsize; |
| 509 | |
| 510 | *size += off; |
| 511 | *dstptr += tsize; |
| 512 | } |
| 513 | EXPORT_SYMBOL_GPL(xt_compat_target_from_user); |
| 514 | |
| 515 | int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr, |
Patrick McHardy | b0a6363 | 2008-01-31 04:10:18 -0800 | [diff] [blame] | 516 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 517 | { |
| 518 | struct xt_target *target = t->u.kernel.target; |
| 519 | struct compat_xt_entry_target __user *ct = *dstptr; |
| 520 | int off = xt_compat_target_offset(target); |
| 521 | u_int16_t tsize = t->u.user.target_size - off; |
| 522 | |
| 523 | if (copy_to_user(ct, t, sizeof(*ct)) || |
Patrick McHardy | a18aa31 | 2007-12-12 10:35:16 -0800 | [diff] [blame] | 524 | put_user(tsize, &ct->u.user.target_size) || |
| 525 | copy_to_user(ct->u.user.name, t->u.kernel.target->name, |
| 526 | strlen(t->u.kernel.target->name) + 1)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 527 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 528 | |
| 529 | if (target->compat_to_user) { |
| 530 | if (target->compat_to_user((void __user *)ct->data, t->data)) |
| 531 | return -EFAULT; |
| 532 | } else { |
| 533 | if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct))) |
| 534 | return -EFAULT; |
| 535 | } |
| 536 | |
| 537 | *size -= off; |
| 538 | *dstptr += tsize; |
| 539 | return 0; |
| 540 | } |
| 541 | EXPORT_SYMBOL_GPL(xt_compat_target_to_user); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 542 | #endif |
| 543 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 544 | struct xt_table_info *xt_alloc_table_info(unsigned int size) |
| 545 | { |
| 546 | struct xt_table_info *newinfo; |
| 547 | int cpu; |
| 548 | |
| 549 | /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */ |
| 550 | if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages) |
| 551 | return NULL; |
| 552 | |
Eric Dumazet | 259d4e4 | 2007-12-04 23:24:56 -0800 | [diff] [blame] | 553 | newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 554 | if (!newinfo) |
| 555 | return NULL; |
| 556 | |
| 557 | newinfo->size = size; |
| 558 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 559 | for_each_possible_cpu(cpu) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 560 | if (size <= PAGE_SIZE) |
| 561 | newinfo->entries[cpu] = kmalloc_node(size, |
| 562 | GFP_KERNEL, |
| 563 | cpu_to_node(cpu)); |
| 564 | else |
| 565 | newinfo->entries[cpu] = vmalloc_node(size, |
| 566 | cpu_to_node(cpu)); |
| 567 | |
| 568 | if (newinfo->entries[cpu] == NULL) { |
| 569 | xt_free_table_info(newinfo); |
| 570 | return NULL; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return newinfo; |
| 575 | } |
| 576 | EXPORT_SYMBOL(xt_alloc_table_info); |
| 577 | |
| 578 | void xt_free_table_info(struct xt_table_info *info) |
| 579 | { |
| 580 | int cpu; |
| 581 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 582 | for_each_possible_cpu(cpu) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 583 | if (info->size <= PAGE_SIZE) |
| 584 | kfree(info->entries[cpu]); |
| 585 | else |
| 586 | vfree(info->entries[cpu]); |
| 587 | } |
| 588 | kfree(info); |
| 589 | } |
| 590 | EXPORT_SYMBOL(xt_free_table_info); |
| 591 | |
| 592 | /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */ |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 593 | struct xt_table *xt_find_table_lock(struct net *net, int af, const char *name) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 594 | { |
| 595 | struct xt_table *t; |
| 596 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 597 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 598 | return ERR_PTR(-EINTR); |
| 599 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 600 | list_for_each_entry(t, &net->xt.tables[af], list) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 601 | if (strcmp(t->name, name) == 0 && try_module_get(t->me)) |
| 602 | return t; |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 603 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 604 | return NULL; |
| 605 | } |
| 606 | EXPORT_SYMBOL_GPL(xt_find_table_lock); |
| 607 | |
| 608 | void xt_table_unlock(struct xt_table *table) |
| 609 | { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 610 | mutex_unlock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 611 | } |
| 612 | EXPORT_SYMBOL_GPL(xt_table_unlock); |
| 613 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 614 | #ifdef CONFIG_COMPAT |
| 615 | void xt_compat_lock(int af) |
| 616 | { |
| 617 | mutex_lock(&xt[af].compat_mutex); |
| 618 | } |
| 619 | EXPORT_SYMBOL_GPL(xt_compat_lock); |
| 620 | |
| 621 | void xt_compat_unlock(int af) |
| 622 | { |
| 623 | mutex_unlock(&xt[af].compat_mutex); |
| 624 | } |
| 625 | EXPORT_SYMBOL_GPL(xt_compat_unlock); |
| 626 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 627 | |
| 628 | struct xt_table_info * |
| 629 | xt_replace_table(struct xt_table *table, |
| 630 | unsigned int num_counters, |
| 631 | struct xt_table_info *newinfo, |
| 632 | int *error) |
| 633 | { |
| 634 | struct xt_table_info *oldinfo, *private; |
| 635 | |
| 636 | /* Do the substitution. */ |
| 637 | write_lock_bh(&table->lock); |
| 638 | private = table->private; |
| 639 | /* Check inside lock: is the old number correct? */ |
| 640 | if (num_counters != private->number) { |
| 641 | duprintf("num_counters != table->private->number (%u/%u)\n", |
| 642 | num_counters, private->number); |
| 643 | write_unlock_bh(&table->lock); |
| 644 | *error = -EAGAIN; |
| 645 | return NULL; |
| 646 | } |
| 647 | oldinfo = private; |
| 648 | table->private = newinfo; |
| 649 | newinfo->initial_entries = oldinfo->initial_entries; |
| 650 | write_unlock_bh(&table->lock); |
| 651 | |
| 652 | return oldinfo; |
| 653 | } |
| 654 | EXPORT_SYMBOL_GPL(xt_replace_table); |
| 655 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 656 | struct xt_table *xt_register_table(struct net *net, struct xt_table *table, |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 657 | struct xt_table_info *bootstrap, |
| 658 | struct xt_table_info *newinfo) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 659 | { |
| 660 | int ret; |
| 661 | struct xt_table_info *private; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 662 | struct xt_table *t; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 663 | |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 664 | /* Don't add one object to multiple lists. */ |
| 665 | table = kmemdup(table, sizeof(struct xt_table), GFP_KERNEL); |
| 666 | if (!table) { |
| 667 | ret = -ENOMEM; |
| 668 | goto out; |
| 669 | } |
| 670 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 671 | ret = mutex_lock_interruptible(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 672 | if (ret != 0) |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 673 | goto out_free; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 674 | |
| 675 | /* Don't autoload: we'd eat our tail... */ |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 676 | list_for_each_entry(t, &net->xt.tables[table->af], list) { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 677 | if (strcmp(t->name, table->name) == 0) { |
| 678 | ret = -EEXIST; |
| 679 | goto unlock; |
| 680 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Simplifies replace_table code. */ |
| 684 | table->private = bootstrap; |
Dmitry Mishin | 91536b7 | 2006-04-24 17:18:25 -0700 | [diff] [blame] | 685 | rwlock_init(&table->lock); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 686 | if (!xt_replace_table(table, 0, newinfo, &ret)) |
| 687 | goto unlock; |
| 688 | |
| 689 | private = table->private; |
| 690 | duprintf("table->private->number = %u\n", private->number); |
| 691 | |
| 692 | /* save number of initial entries */ |
| 693 | private->initial_entries = private->number; |
| 694 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 695 | list_add(&table->list, &net->xt.tables[table->af]); |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 696 | mutex_unlock(&xt[table->af].mutex); |
| 697 | return table; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 698 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 699 | unlock: |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 700 | mutex_unlock(&xt[table->af].mutex); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 701 | out_free: |
| 702 | kfree(table); |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 703 | out: |
| 704 | return ERR_PTR(ret); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 705 | } |
| 706 | EXPORT_SYMBOL_GPL(xt_register_table); |
| 707 | |
| 708 | void *xt_unregister_table(struct xt_table *table) |
| 709 | { |
| 710 | struct xt_table_info *private; |
| 711 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 712 | mutex_lock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 713 | private = table->private; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 714 | list_del(&table->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 715 | mutex_unlock(&xt[table->af].mutex); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 716 | kfree(table); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 717 | |
| 718 | return private; |
| 719 | } |
| 720 | EXPORT_SYMBOL_GPL(xt_unregister_table); |
| 721 | |
| 722 | #ifdef CONFIG_PROC_FS |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 723 | struct xt_names_priv { |
| 724 | struct seq_net_private p; |
| 725 | int af; |
| 726 | }; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 727 | static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 728 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 729 | struct xt_names_priv *priv = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 730 | struct net *net = seq_file_net(seq); |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 731 | int af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 732 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 733 | mutex_lock(&xt[af].mutex); |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 734 | return seq_list_start(&net->xt.tables[af], *pos); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 737 | static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 738 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 739 | struct xt_names_priv *priv = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 740 | struct net *net = seq_file_net(seq); |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 741 | int af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 742 | |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 743 | return seq_list_next(v, &net->xt.tables[af], pos); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 746 | static void xt_table_seq_stop(struct seq_file *seq, void *v) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 747 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 748 | struct xt_names_priv *priv = seq->private; |
| 749 | int af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 750 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 751 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 754 | static int xt_table_seq_show(struct seq_file *seq, void *v) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 755 | { |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 756 | struct xt_table *table = list_entry(v, struct xt_table, list); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 757 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 758 | if (strlen(table->name)) |
| 759 | return seq_printf(seq, "%s\n", table->name); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 760 | else |
| 761 | return 0; |
| 762 | } |
| 763 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 764 | static const struct seq_operations xt_table_seq_ops = { |
| 765 | .start = xt_table_seq_start, |
| 766 | .next = xt_table_seq_next, |
| 767 | .stop = xt_table_seq_stop, |
| 768 | .show = xt_table_seq_show, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 769 | }; |
| 770 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 771 | static int xt_table_open(struct inode *inode, struct file *file) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 772 | { |
| 773 | int ret; |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 774 | struct xt_names_priv *priv; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 775 | |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 776 | ret = seq_open_net(inode, file, &xt_table_seq_ops, |
| 777 | sizeof(struct xt_names_priv)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 778 | if (!ret) { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 779 | priv = ((struct seq_file *)file->private_data)->private; |
| 780 | priv->af = (unsigned long)PDE(inode)->data; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 781 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 782 | return ret; |
| 783 | } |
| 784 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 785 | static const struct file_operations xt_table_ops = { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 786 | .owner = THIS_MODULE, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 787 | .open = xt_table_open, |
| 788 | .read = seq_read, |
| 789 | .llseek = seq_lseek, |
| 790 | .release = seq_release, |
| 791 | }; |
| 792 | |
| 793 | static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos) |
| 794 | { |
| 795 | struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private; |
| 796 | u_int16_t af = (unsigned long)pde->data; |
| 797 | |
| 798 | mutex_lock(&xt[af].mutex); |
| 799 | return seq_list_start(&xt[af].match, *pos); |
| 800 | } |
| 801 | |
| 802 | static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 803 | { |
| 804 | struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private; |
| 805 | u_int16_t af = (unsigned long)pde->data; |
| 806 | |
| 807 | return seq_list_next(v, &xt[af].match, pos); |
| 808 | } |
| 809 | |
| 810 | static void xt_match_seq_stop(struct seq_file *seq, void *v) |
| 811 | { |
| 812 | struct proc_dir_entry *pde = seq->private; |
| 813 | u_int16_t af = (unsigned long)pde->data; |
| 814 | |
| 815 | mutex_unlock(&xt[af].mutex); |
| 816 | } |
| 817 | |
| 818 | static int xt_match_seq_show(struct seq_file *seq, void *v) |
| 819 | { |
| 820 | struct xt_match *match = list_entry(v, struct xt_match, list); |
| 821 | |
| 822 | if (strlen(match->name)) |
| 823 | return seq_printf(seq, "%s\n", match->name); |
| 824 | else |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static const struct seq_operations xt_match_seq_ops = { |
| 829 | .start = xt_match_seq_start, |
| 830 | .next = xt_match_seq_next, |
| 831 | .stop = xt_match_seq_stop, |
| 832 | .show = xt_match_seq_show, |
| 833 | }; |
| 834 | |
| 835 | static int xt_match_open(struct inode *inode, struct file *file) |
| 836 | { |
| 837 | int ret; |
| 838 | |
| 839 | ret = seq_open(file, &xt_match_seq_ops); |
| 840 | if (!ret) { |
| 841 | struct seq_file *seq = file->private_data; |
| 842 | |
| 843 | seq->private = PDE(inode); |
| 844 | } |
| 845 | return ret; |
| 846 | } |
| 847 | |
| 848 | static const struct file_operations xt_match_ops = { |
| 849 | .owner = THIS_MODULE, |
| 850 | .open = xt_match_open, |
| 851 | .read = seq_read, |
| 852 | .llseek = seq_lseek, |
| 853 | .release = seq_release, |
| 854 | }; |
| 855 | |
| 856 | static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos) |
| 857 | { |
| 858 | struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private; |
| 859 | u_int16_t af = (unsigned long)pde->data; |
| 860 | |
| 861 | mutex_lock(&xt[af].mutex); |
| 862 | return seq_list_start(&xt[af].target, *pos); |
| 863 | } |
| 864 | |
| 865 | static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 866 | { |
| 867 | struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private; |
| 868 | u_int16_t af = (unsigned long)pde->data; |
| 869 | |
| 870 | return seq_list_next(v, &xt[af].target, pos); |
| 871 | } |
| 872 | |
| 873 | static void xt_target_seq_stop(struct seq_file *seq, void *v) |
| 874 | { |
| 875 | struct proc_dir_entry *pde = seq->private; |
| 876 | u_int16_t af = (unsigned long)pde->data; |
| 877 | |
| 878 | mutex_unlock(&xt[af].mutex); |
| 879 | } |
| 880 | |
| 881 | static int xt_target_seq_show(struct seq_file *seq, void *v) |
| 882 | { |
| 883 | struct xt_target *target = list_entry(v, struct xt_target, list); |
| 884 | |
| 885 | if (strlen(target->name)) |
| 886 | return seq_printf(seq, "%s\n", target->name); |
| 887 | else |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | static const struct seq_operations xt_target_seq_ops = { |
| 892 | .start = xt_target_seq_start, |
| 893 | .next = xt_target_seq_next, |
| 894 | .stop = xt_target_seq_stop, |
| 895 | .show = xt_target_seq_show, |
| 896 | }; |
| 897 | |
| 898 | static int xt_target_open(struct inode *inode, struct file *file) |
| 899 | { |
| 900 | int ret; |
| 901 | |
| 902 | ret = seq_open(file, &xt_target_seq_ops); |
| 903 | if (!ret) { |
| 904 | struct seq_file *seq = file->private_data; |
| 905 | |
| 906 | seq->private = PDE(inode); |
| 907 | } |
| 908 | return ret; |
| 909 | } |
| 910 | |
| 911 | static const struct file_operations xt_target_ops = { |
| 912 | .owner = THIS_MODULE, |
| 913 | .open = xt_target_open, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 914 | .read = seq_read, |
| 915 | .llseek = seq_lseek, |
| 916 | .release = seq_release, |
| 917 | }; |
| 918 | |
| 919 | #define FORMAT_TABLES "_tables_names" |
| 920 | #define FORMAT_MATCHES "_tables_matches" |
| 921 | #define FORMAT_TARGETS "_tables_targets" |
| 922 | |
| 923 | #endif /* CONFIG_PROC_FS */ |
| 924 | |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 925 | int xt_proto_init(struct net *net, int af) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 926 | { |
| 927 | #ifdef CONFIG_PROC_FS |
| 928 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 929 | struct proc_dir_entry *proc; |
| 930 | #endif |
| 931 | |
| 932 | if (af >= NPROTO) |
| 933 | return -EINVAL; |
| 934 | |
| 935 | |
| 936 | #ifdef CONFIG_PROC_FS |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 937 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 938 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 939 | proc = proc_net_fops_create(net, buf, 0440, &xt_table_ops); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 940 | if (!proc) |
| 941 | goto out; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 942 | proc->data = (void *)(unsigned long)af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 943 | |
| 944 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 945 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 946 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 947 | proc = proc_net_fops_create(net, buf, 0440, &xt_match_ops); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 948 | if (!proc) |
| 949 | goto out_remove_tables; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 950 | proc->data = (void *)(unsigned long)af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 951 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 952 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 953 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 954 | proc = proc_net_fops_create(net, buf, 0440, &xt_target_ops); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 955 | if (!proc) |
| 956 | goto out_remove_matches; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 957 | proc->data = (void *)(unsigned long)af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 958 | #endif |
| 959 | |
| 960 | return 0; |
| 961 | |
| 962 | #ifdef CONFIG_PROC_FS |
| 963 | out_remove_matches: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 964 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 965 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 966 | proc_net_remove(net, buf); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 967 | |
| 968 | out_remove_tables: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 969 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 970 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 971 | proc_net_remove(net, buf); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 972 | out: |
| 973 | return -1; |
| 974 | #endif |
| 975 | } |
| 976 | EXPORT_SYMBOL_GPL(xt_proto_init); |
| 977 | |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 978 | void xt_proto_fini(struct net *net, int af) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 979 | { |
| 980 | #ifdef CONFIG_PROC_FS |
| 981 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 982 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 983 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 984 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 985 | proc_net_remove(net, buf); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 986 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 987 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 988 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 989 | proc_net_remove(net, buf); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 990 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 991 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 992 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Alexey Dobriyan | 3cb609d | 2008-01-31 04:49:35 -0800 | [diff] [blame] | 993 | proc_net_remove(net, buf); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 994 | #endif /*CONFIG_PROC_FS*/ |
| 995 | } |
| 996 | EXPORT_SYMBOL_GPL(xt_proto_fini); |
| 997 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 998 | static int __net_init xt_net_init(struct net *net) |
| 999 | { |
| 1000 | int i; |
| 1001 | |
| 1002 | for (i = 0; i < NPROTO; i++) |
| 1003 | INIT_LIST_HEAD(&net->xt.tables[i]); |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | static struct pernet_operations xt_net_ops = { |
| 1008 | .init = xt_net_init, |
| 1009 | }; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1010 | |
| 1011 | static int __init xt_init(void) |
| 1012 | { |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1013 | int i, rv; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1014 | |
| 1015 | xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL); |
| 1016 | if (!xt) |
| 1017 | return -ENOMEM; |
| 1018 | |
| 1019 | for (i = 0; i < NPROTO; i++) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1020 | mutex_init(&xt[i].mutex); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1021 | #ifdef CONFIG_COMPAT |
| 1022 | mutex_init(&xt[i].compat_mutex); |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 1023 | xt[i].compat_offsets = NULL; |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1024 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1025 | INIT_LIST_HEAD(&xt[i].target); |
| 1026 | INIT_LIST_HEAD(&xt[i].match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1027 | } |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1028 | rv = register_pernet_subsys(&xt_net_ops); |
| 1029 | if (rv < 0) |
| 1030 | kfree(xt); |
| 1031 | return rv; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | static void __exit xt_fini(void) |
| 1035 | { |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1036 | unregister_pernet_subsys(&xt_net_ops); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1037 | kfree(xt); |
| 1038 | } |
| 1039 | |
| 1040 | module_init(xt_init); |
| 1041 | module_exit(xt_fini); |
| 1042 | |