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> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 25 | |
| 26 | #include <linux/netfilter/x_tables.h> |
| 27 | #include <linux/netfilter_arp.h> |
| 28 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 29 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 30 | MODULE_LICENSE("GPL"); |
| 31 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
| 32 | MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module"); |
| 33 | |
| 34 | #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1)) |
| 35 | |
| 36 | struct xt_af { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 37 | struct mutex mutex; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 38 | struct list_head match; |
| 39 | struct list_head target; |
| 40 | struct list_head tables; |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 41 | struct mutex compat_mutex; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static struct xt_af *xt; |
| 45 | |
| 46 | #ifdef DEBUG_IP_FIREWALL_USER |
| 47 | #define duprintf(format, args...) printk(format , ## args) |
| 48 | #else |
| 49 | #define duprintf(format, args...) |
| 50 | #endif |
| 51 | |
| 52 | enum { |
| 53 | TABLE, |
| 54 | TARGET, |
| 55 | MATCH, |
| 56 | }; |
| 57 | |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 58 | static const char *xt_prefix[NPROTO] = { |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 59 | [AF_INET] = "ip", |
| 60 | [AF_INET6] = "ip6", |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 61 | [NF_ARP] = "arp", |
| 62 | }; |
| 63 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 64 | /* Registration hooks for targets. */ |
| 65 | int |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 66 | xt_register_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 67 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 68 | int ret, af = target->family; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 69 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 70 | ret = mutex_lock_interruptible(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 71 | if (ret != 0) |
| 72 | return ret; |
| 73 | list_add(&target->list, &xt[af].target); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 74 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 75 | return ret; |
| 76 | } |
| 77 | EXPORT_SYMBOL(xt_register_target); |
| 78 | |
| 79 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 80 | xt_unregister_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 81 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 82 | int af = target->family; |
| 83 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 84 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 85 | list_del(&target->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 86 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 87 | } |
| 88 | EXPORT_SYMBOL(xt_unregister_target); |
| 89 | |
| 90 | int |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 91 | xt_register_targets(struct xt_target *target, unsigned int n) |
| 92 | { |
| 93 | unsigned int i; |
| 94 | int err = 0; |
| 95 | |
| 96 | for (i = 0; i < n; i++) { |
| 97 | err = xt_register_target(&target[i]); |
| 98 | if (err) |
| 99 | goto err; |
| 100 | } |
| 101 | return err; |
| 102 | |
| 103 | err: |
| 104 | if (i > 0) |
| 105 | xt_unregister_targets(target, i); |
| 106 | return err; |
| 107 | } |
| 108 | EXPORT_SYMBOL(xt_register_targets); |
| 109 | |
| 110 | void |
| 111 | xt_unregister_targets(struct xt_target *target, unsigned int n) |
| 112 | { |
| 113 | unsigned int i; |
| 114 | |
| 115 | for (i = 0; i < n; i++) |
| 116 | xt_unregister_target(&target[i]); |
| 117 | } |
| 118 | EXPORT_SYMBOL(xt_unregister_targets); |
| 119 | |
| 120 | int |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 121 | xt_register_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 122 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 123 | int ret, af = match->family; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 124 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 125 | ret = mutex_lock_interruptible(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 126 | if (ret != 0) |
| 127 | return ret; |
| 128 | |
| 129 | list_add(&match->list, &xt[af].match); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 130 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | EXPORT_SYMBOL(xt_register_match); |
| 135 | |
| 136 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 137 | xt_unregister_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 138 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 139 | int af = match->family; |
| 140 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 141 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 142 | list_del(&match->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 143 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 144 | } |
| 145 | EXPORT_SYMBOL(xt_unregister_match); |
| 146 | |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 147 | int |
| 148 | xt_register_matches(struct xt_match *match, unsigned int n) |
| 149 | { |
| 150 | unsigned int i; |
| 151 | int err = 0; |
| 152 | |
| 153 | for (i = 0; i < n; i++) { |
| 154 | err = xt_register_match(&match[i]); |
| 155 | if (err) |
| 156 | goto err; |
| 157 | } |
| 158 | return err; |
| 159 | |
| 160 | err: |
| 161 | if (i > 0) |
| 162 | xt_unregister_matches(match, i); |
| 163 | return err; |
| 164 | } |
| 165 | EXPORT_SYMBOL(xt_register_matches); |
| 166 | |
| 167 | void |
| 168 | xt_unregister_matches(struct xt_match *match, unsigned int n) |
| 169 | { |
| 170 | unsigned int i; |
| 171 | |
| 172 | for (i = 0; i < n; i++) |
| 173 | xt_unregister_match(&match[i]); |
| 174 | } |
| 175 | EXPORT_SYMBOL(xt_unregister_matches); |
| 176 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * These are weird, but module loading must not be done with mutex |
| 180 | * held (since they will register), and we have to have a single |
| 181 | * function to use try_then_request_module(). |
| 182 | */ |
| 183 | |
| 184 | /* Find match, grabs ref. Returns ERR_PTR() on error. */ |
| 185 | struct xt_match *xt_find_match(int af, const char *name, u8 revision) |
| 186 | { |
| 187 | struct xt_match *m; |
| 188 | int err = 0; |
| 189 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 190 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 191 | return ERR_PTR(-EINTR); |
| 192 | |
| 193 | list_for_each_entry(m, &xt[af].match, list) { |
| 194 | if (strcmp(m->name, name) == 0) { |
| 195 | if (m->revision == revision) { |
| 196 | if (try_module_get(m->me)) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 197 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 198 | return m; |
| 199 | } |
| 200 | } else |
| 201 | err = -EPROTOTYPE; /* Found something. */ |
| 202 | } |
| 203 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 204 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 205 | return ERR_PTR(err); |
| 206 | } |
| 207 | EXPORT_SYMBOL(xt_find_match); |
| 208 | |
| 209 | /* Find target, grabs ref. Returns ERR_PTR() on error. */ |
| 210 | struct xt_target *xt_find_target(int af, const char *name, u8 revision) |
| 211 | { |
| 212 | struct xt_target *t; |
| 213 | int err = 0; |
| 214 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 215 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 216 | return ERR_PTR(-EINTR); |
| 217 | |
| 218 | list_for_each_entry(t, &xt[af].target, list) { |
| 219 | if (strcmp(t->name, name) == 0) { |
| 220 | if (t->revision == revision) { |
| 221 | if (try_module_get(t->me)) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 222 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 223 | return t; |
| 224 | } |
| 225 | } else |
| 226 | err = -EPROTOTYPE; /* Found something. */ |
| 227 | } |
| 228 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 229 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 230 | return ERR_PTR(err); |
| 231 | } |
| 232 | EXPORT_SYMBOL(xt_find_target); |
| 233 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 234 | struct xt_target *xt_request_find_target(int af, const char *name, u8 revision) |
| 235 | { |
| 236 | struct xt_target *target; |
| 237 | |
| 238 | target = try_then_request_module(xt_find_target(af, name, revision), |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 239 | "%st_%s", xt_prefix[af], name); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 240 | if (IS_ERR(target) || !target) |
| 241 | return NULL; |
| 242 | return target; |
| 243 | } |
| 244 | EXPORT_SYMBOL_GPL(xt_request_find_target); |
| 245 | |
| 246 | static int match_revfn(int af, const char *name, u8 revision, int *bestp) |
| 247 | { |
| 248 | struct xt_match *m; |
| 249 | int have_rev = 0; |
| 250 | |
| 251 | list_for_each_entry(m, &xt[af].match, list) { |
| 252 | if (strcmp(m->name, name) == 0) { |
| 253 | if (m->revision > *bestp) |
| 254 | *bestp = m->revision; |
| 255 | if (m->revision == revision) |
| 256 | have_rev = 1; |
| 257 | } |
| 258 | } |
| 259 | return have_rev; |
| 260 | } |
| 261 | |
| 262 | static int target_revfn(int af, const char *name, u8 revision, int *bestp) |
| 263 | { |
| 264 | struct xt_target *t; |
| 265 | int have_rev = 0; |
| 266 | |
| 267 | list_for_each_entry(t, &xt[af].target, list) { |
| 268 | if (strcmp(t->name, name) == 0) { |
| 269 | if (t->revision > *bestp) |
| 270 | *bestp = t->revision; |
| 271 | if (t->revision == revision) |
| 272 | have_rev = 1; |
| 273 | } |
| 274 | } |
| 275 | return have_rev; |
| 276 | } |
| 277 | |
| 278 | /* Returns true or false (if no such extension at all) */ |
| 279 | int xt_find_revision(int af, const char *name, u8 revision, int target, |
| 280 | int *err) |
| 281 | { |
| 282 | int have_rev, best = -1; |
| 283 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 284 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 285 | *err = -EINTR; |
| 286 | return 1; |
| 287 | } |
| 288 | if (target == 1) |
| 289 | have_rev = target_revfn(af, name, revision, &best); |
| 290 | else |
| 291 | have_rev = match_revfn(af, name, revision, &best); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 292 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 293 | |
| 294 | /* Nothing at all? Return 0 to try loading module. */ |
| 295 | if (best == -1) { |
| 296 | *err = -ENOENT; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | *err = best; |
| 301 | if (!have_rev) |
| 302 | *err = -EPROTONOSUPPORT; |
| 303 | return 1; |
| 304 | } |
| 305 | EXPORT_SYMBOL_GPL(xt_find_revision); |
| 306 | |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 307 | int xt_check_match(const struct xt_match *match, unsigned short family, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 308 | unsigned int size, const char *table, unsigned int hook_mask, |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 309 | unsigned short proto, int inv_proto) |
| 310 | { |
| 311 | if (XT_ALIGN(match->matchsize) != size) { |
| 312 | printk("%s_tables: %s match: invalid size %Zu != %u\n", |
| 313 | xt_prefix[family], match->name, |
| 314 | XT_ALIGN(match->matchsize), size); |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | if (match->table && strcmp(match->table, table)) { |
| 318 | printk("%s_tables: %s match: only valid in %s table, not %s\n", |
| 319 | xt_prefix[family], match->name, match->table, table); |
| 320 | return -EINVAL; |
| 321 | } |
| 322 | if (match->hooks && (hook_mask & ~match->hooks) != 0) { |
| 323 | printk("%s_tables: %s match: bad hook_mask %u\n", |
| 324 | xt_prefix[family], match->name, hook_mask); |
| 325 | return -EINVAL; |
| 326 | } |
| 327 | if (match->proto && (match->proto != proto || inv_proto)) { |
| 328 | printk("%s_tables: %s match: only valid for protocol %u\n", |
| 329 | xt_prefix[family], match->name, match->proto); |
| 330 | return -EINVAL; |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(xt_check_match); |
| 335 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 336 | #ifdef CONFIG_COMPAT |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 337 | int xt_compat_match_offset(struct xt_match *match) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 338 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 339 | u_int16_t csize = match->compatsize ? : match->matchsize; |
| 340 | return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 341 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 342 | EXPORT_SYMBOL_GPL(xt_compat_match_offset); |
| 343 | |
| 344 | void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr, |
| 345 | int *size) |
| 346 | { |
| 347 | struct xt_match *match = m->u.kernel.match; |
| 348 | struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m; |
| 349 | int pad, off = xt_compat_match_offset(match); |
| 350 | u_int16_t msize = cm->u.user.match_size; |
| 351 | |
| 352 | m = *dstptr; |
| 353 | memcpy(m, cm, sizeof(*cm)); |
| 354 | if (match->compat_from_user) |
| 355 | match->compat_from_user(m->data, cm->data); |
| 356 | else |
| 357 | memcpy(m->data, cm->data, msize - sizeof(*cm)); |
| 358 | pad = XT_ALIGN(match->matchsize) - match->matchsize; |
| 359 | if (pad > 0) |
| 360 | memset(m->data + match->matchsize, 0, pad); |
| 361 | |
| 362 | msize += off; |
| 363 | m->u.user.match_size = msize; |
| 364 | |
| 365 | *size += off; |
| 366 | *dstptr += msize; |
| 367 | } |
| 368 | EXPORT_SYMBOL_GPL(xt_compat_match_from_user); |
| 369 | |
| 370 | int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr, |
| 371 | int *size) |
| 372 | { |
| 373 | struct xt_match *match = m->u.kernel.match; |
| 374 | struct compat_xt_entry_match __user *cm = *dstptr; |
| 375 | int off = xt_compat_match_offset(match); |
| 376 | u_int16_t msize = m->u.user.match_size - off; |
| 377 | |
| 378 | if (copy_to_user(cm, m, sizeof(*cm)) || |
| 379 | put_user(msize, &cm->u.user.match_size)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 380 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 381 | |
| 382 | if (match->compat_to_user) { |
| 383 | if (match->compat_to_user((void __user *)cm->data, m->data)) |
| 384 | return -EFAULT; |
| 385 | } else { |
| 386 | if (copy_to_user(cm->data, m->data, msize - sizeof(*cm))) |
| 387 | return -EFAULT; |
| 388 | } |
| 389 | |
| 390 | *size -= off; |
| 391 | *dstptr += msize; |
| 392 | return 0; |
| 393 | } |
| 394 | EXPORT_SYMBOL_GPL(xt_compat_match_to_user); |
| 395 | #endif /* CONFIG_COMPAT */ |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 396 | |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 397 | int xt_check_target(const struct xt_target *target, unsigned short family, |
| 398 | unsigned int size, const char *table, unsigned int hook_mask, |
| 399 | unsigned short proto, int inv_proto) |
| 400 | { |
| 401 | if (XT_ALIGN(target->targetsize) != size) { |
| 402 | printk("%s_tables: %s target: invalid size %Zu != %u\n", |
| 403 | xt_prefix[family], target->name, |
| 404 | XT_ALIGN(target->targetsize), size); |
| 405 | return -EINVAL; |
| 406 | } |
| 407 | if (target->table && strcmp(target->table, table)) { |
| 408 | printk("%s_tables: %s target: only valid in %s table, not %s\n", |
| 409 | xt_prefix[family], target->name, target->table, table); |
| 410 | return -EINVAL; |
| 411 | } |
| 412 | if (target->hooks && (hook_mask & ~target->hooks) != 0) { |
| 413 | printk("%s_tables: %s target: bad hook_mask %u\n", |
| 414 | xt_prefix[family], target->name, hook_mask); |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | if (target->proto && (target->proto != proto || inv_proto)) { |
| 418 | printk("%s_tables: %s target: only valid for protocol %u\n", |
| 419 | xt_prefix[family], target->name, target->proto); |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | return 0; |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(xt_check_target); |
| 425 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 426 | #ifdef CONFIG_COMPAT |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 427 | int xt_compat_target_offset(struct xt_target *target) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 428 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 429 | u_int16_t csize = target->compatsize ? : target->targetsize; |
| 430 | return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 431 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 432 | EXPORT_SYMBOL_GPL(xt_compat_target_offset); |
| 433 | |
| 434 | void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 435 | int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 436 | { |
| 437 | struct xt_target *target = t->u.kernel.target; |
| 438 | struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t; |
| 439 | int pad, off = xt_compat_target_offset(target); |
| 440 | u_int16_t tsize = ct->u.user.target_size; |
| 441 | |
| 442 | t = *dstptr; |
| 443 | memcpy(t, ct, sizeof(*ct)); |
| 444 | if (target->compat_from_user) |
| 445 | target->compat_from_user(t->data, ct->data); |
| 446 | else |
| 447 | memcpy(t->data, ct->data, tsize - sizeof(*ct)); |
| 448 | pad = XT_ALIGN(target->targetsize) - target->targetsize; |
| 449 | if (pad > 0) |
| 450 | memset(t->data + target->targetsize, 0, pad); |
| 451 | |
| 452 | tsize += off; |
| 453 | t->u.user.target_size = tsize; |
| 454 | |
| 455 | *size += off; |
| 456 | *dstptr += tsize; |
| 457 | } |
| 458 | EXPORT_SYMBOL_GPL(xt_compat_target_from_user); |
| 459 | |
| 460 | int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr, |
| 461 | int *size) |
| 462 | { |
| 463 | struct xt_target *target = t->u.kernel.target; |
| 464 | struct compat_xt_entry_target __user *ct = *dstptr; |
| 465 | int off = xt_compat_target_offset(target); |
| 466 | u_int16_t tsize = t->u.user.target_size - off; |
| 467 | |
| 468 | if (copy_to_user(ct, t, sizeof(*ct)) || |
| 469 | put_user(tsize, &ct->u.user.target_size)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 470 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 471 | |
| 472 | if (target->compat_to_user) { |
| 473 | if (target->compat_to_user((void __user *)ct->data, t->data)) |
| 474 | return -EFAULT; |
| 475 | } else { |
| 476 | if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct))) |
| 477 | return -EFAULT; |
| 478 | } |
| 479 | |
| 480 | *size -= off; |
| 481 | *dstptr += tsize; |
| 482 | return 0; |
| 483 | } |
| 484 | EXPORT_SYMBOL_GPL(xt_compat_target_to_user); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 485 | #endif |
| 486 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 487 | struct xt_table_info *xt_alloc_table_info(unsigned int size) |
| 488 | { |
| 489 | struct xt_table_info *newinfo; |
| 490 | int cpu; |
| 491 | |
| 492 | /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */ |
| 493 | if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages) |
| 494 | return NULL; |
| 495 | |
| 496 | newinfo = kzalloc(sizeof(struct xt_table_info), GFP_KERNEL); |
| 497 | if (!newinfo) |
| 498 | return NULL; |
| 499 | |
| 500 | newinfo->size = size; |
| 501 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 502 | for_each_possible_cpu(cpu) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 503 | if (size <= PAGE_SIZE) |
| 504 | newinfo->entries[cpu] = kmalloc_node(size, |
| 505 | GFP_KERNEL, |
| 506 | cpu_to_node(cpu)); |
| 507 | else |
| 508 | newinfo->entries[cpu] = vmalloc_node(size, |
| 509 | cpu_to_node(cpu)); |
| 510 | |
| 511 | if (newinfo->entries[cpu] == NULL) { |
| 512 | xt_free_table_info(newinfo); |
| 513 | return NULL; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return newinfo; |
| 518 | } |
| 519 | EXPORT_SYMBOL(xt_alloc_table_info); |
| 520 | |
| 521 | void xt_free_table_info(struct xt_table_info *info) |
| 522 | { |
| 523 | int cpu; |
| 524 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 525 | for_each_possible_cpu(cpu) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 526 | if (info->size <= PAGE_SIZE) |
| 527 | kfree(info->entries[cpu]); |
| 528 | else |
| 529 | vfree(info->entries[cpu]); |
| 530 | } |
| 531 | kfree(info); |
| 532 | } |
| 533 | EXPORT_SYMBOL(xt_free_table_info); |
| 534 | |
| 535 | /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */ |
| 536 | struct xt_table *xt_find_table_lock(int af, const char *name) |
| 537 | { |
| 538 | struct xt_table *t; |
| 539 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 540 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 541 | return ERR_PTR(-EINTR); |
| 542 | |
| 543 | list_for_each_entry(t, &xt[af].tables, list) |
| 544 | if (strcmp(t->name, name) == 0 && try_module_get(t->me)) |
| 545 | return t; |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 546 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 547 | return NULL; |
| 548 | } |
| 549 | EXPORT_SYMBOL_GPL(xt_find_table_lock); |
| 550 | |
| 551 | void xt_table_unlock(struct xt_table *table) |
| 552 | { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 553 | mutex_unlock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 554 | } |
| 555 | EXPORT_SYMBOL_GPL(xt_table_unlock); |
| 556 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 557 | #ifdef CONFIG_COMPAT |
| 558 | void xt_compat_lock(int af) |
| 559 | { |
| 560 | mutex_lock(&xt[af].compat_mutex); |
| 561 | } |
| 562 | EXPORT_SYMBOL_GPL(xt_compat_lock); |
| 563 | |
| 564 | void xt_compat_unlock(int af) |
| 565 | { |
| 566 | mutex_unlock(&xt[af].compat_mutex); |
| 567 | } |
| 568 | EXPORT_SYMBOL_GPL(xt_compat_unlock); |
| 569 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 570 | |
| 571 | struct xt_table_info * |
| 572 | xt_replace_table(struct xt_table *table, |
| 573 | unsigned int num_counters, |
| 574 | struct xt_table_info *newinfo, |
| 575 | int *error) |
| 576 | { |
| 577 | struct xt_table_info *oldinfo, *private; |
| 578 | |
| 579 | /* Do the substitution. */ |
| 580 | write_lock_bh(&table->lock); |
| 581 | private = table->private; |
| 582 | /* Check inside lock: is the old number correct? */ |
| 583 | if (num_counters != private->number) { |
| 584 | duprintf("num_counters != table->private->number (%u/%u)\n", |
| 585 | num_counters, private->number); |
| 586 | write_unlock_bh(&table->lock); |
| 587 | *error = -EAGAIN; |
| 588 | return NULL; |
| 589 | } |
| 590 | oldinfo = private; |
| 591 | table->private = newinfo; |
| 592 | newinfo->initial_entries = oldinfo->initial_entries; |
| 593 | write_unlock_bh(&table->lock); |
| 594 | |
| 595 | return oldinfo; |
| 596 | } |
| 597 | EXPORT_SYMBOL_GPL(xt_replace_table); |
| 598 | |
| 599 | int xt_register_table(struct xt_table *table, |
| 600 | struct xt_table_info *bootstrap, |
| 601 | struct xt_table_info *newinfo) |
| 602 | { |
| 603 | int ret; |
| 604 | struct xt_table_info *private; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 605 | struct xt_table *t; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 606 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 607 | ret = mutex_lock_interruptible(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 608 | if (ret != 0) |
| 609 | return ret; |
| 610 | |
| 611 | /* Don't autoload: we'd eat our tail... */ |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 612 | list_for_each_entry(t, &xt[table->af].tables, list) { |
| 613 | if (strcmp(t->name, table->name) == 0) { |
| 614 | ret = -EEXIST; |
| 615 | goto unlock; |
| 616 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | /* Simplifies replace_table code. */ |
| 620 | table->private = bootstrap; |
Dmitry Mishin | 91536b7 | 2006-04-24 17:18:25 -0700 | [diff] [blame] | 621 | rwlock_init(&table->lock); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 622 | if (!xt_replace_table(table, 0, newinfo, &ret)) |
| 623 | goto unlock; |
| 624 | |
| 625 | private = table->private; |
| 626 | duprintf("table->private->number = %u\n", private->number); |
| 627 | |
| 628 | /* save number of initial entries */ |
| 629 | private->initial_entries = private->number; |
| 630 | |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 631 | list_add(&table->list, &xt[table->af].tables); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 632 | |
| 633 | ret = 0; |
| 634 | unlock: |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 635 | mutex_unlock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 636 | return ret; |
| 637 | } |
| 638 | EXPORT_SYMBOL_GPL(xt_register_table); |
| 639 | |
| 640 | void *xt_unregister_table(struct xt_table *table) |
| 641 | { |
| 642 | struct xt_table_info *private; |
| 643 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 644 | mutex_lock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 645 | private = table->private; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 646 | list_del(&table->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 647 | mutex_unlock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 648 | |
| 649 | return private; |
| 650 | } |
| 651 | EXPORT_SYMBOL_GPL(xt_unregister_table); |
| 652 | |
| 653 | #ifdef CONFIG_PROC_FS |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 654 | static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos) |
| 655 | { |
| 656 | struct list_head *head = list->next; |
| 657 | |
| 658 | if (!head || list_empty(list)) |
| 659 | return NULL; |
| 660 | |
| 661 | while (pos && (head = head->next)) { |
| 662 | if (head == list) |
| 663 | return NULL; |
| 664 | pos--; |
| 665 | } |
| 666 | return pos ? NULL : head; |
| 667 | } |
| 668 | |
| 669 | static struct list_head *type2list(u_int16_t af, u_int16_t type) |
| 670 | { |
| 671 | struct list_head *list; |
| 672 | |
| 673 | switch (type) { |
| 674 | case TARGET: |
| 675 | list = &xt[af].target; |
| 676 | break; |
| 677 | case MATCH: |
| 678 | list = &xt[af].match; |
| 679 | break; |
| 680 | case TABLE: |
| 681 | list = &xt[af].tables; |
| 682 | break; |
| 683 | default: |
| 684 | list = NULL; |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | return list; |
| 689 | } |
| 690 | |
| 691 | static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos) |
| 692 | { |
| 693 | struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private; |
| 694 | u_int16_t af = (unsigned long)pde->data & 0xffff; |
| 695 | u_int16_t type = (unsigned long)pde->data >> 16; |
| 696 | struct list_head *list; |
| 697 | |
| 698 | if (af >= NPROTO) |
| 699 | return NULL; |
| 700 | |
| 701 | list = type2list(af, type); |
| 702 | if (!list) |
| 703 | return NULL; |
| 704 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 705 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 706 | return NULL; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 707 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 708 | return xt_get_idx(list, seq, *pos); |
| 709 | } |
| 710 | |
| 711 | static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 712 | { |
| 713 | struct proc_dir_entry *pde = seq->private; |
| 714 | u_int16_t af = (unsigned long)pde->data & 0xffff; |
| 715 | u_int16_t type = (unsigned long)pde->data >> 16; |
| 716 | struct list_head *list; |
| 717 | |
| 718 | if (af >= NPROTO) |
| 719 | return NULL; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 720 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 721 | list = type2list(af, type); |
| 722 | if (!list) |
| 723 | return NULL; |
| 724 | |
| 725 | (*pos)++; |
| 726 | return xt_get_idx(list, seq, *pos); |
| 727 | } |
| 728 | |
| 729 | static void xt_tgt_seq_stop(struct seq_file *seq, void *v) |
| 730 | { |
| 731 | struct proc_dir_entry *pde = seq->private; |
| 732 | u_int16_t af = (unsigned long)pde->data & 0xffff; |
| 733 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 734 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | static int xt_name_seq_show(struct seq_file *seq, void *v) |
| 738 | { |
| 739 | char *name = (char *)v + sizeof(struct list_head); |
| 740 | |
| 741 | if (strlen(name)) |
| 742 | return seq_printf(seq, "%s\n", name); |
| 743 | else |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | static struct seq_operations xt_tgt_seq_ops = { |
| 748 | .start = xt_tgt_seq_start, |
| 749 | .next = xt_tgt_seq_next, |
| 750 | .stop = xt_tgt_seq_stop, |
| 751 | .show = xt_name_seq_show, |
| 752 | }; |
| 753 | |
| 754 | static int xt_tgt_open(struct inode *inode, struct file *file) |
| 755 | { |
| 756 | int ret; |
| 757 | |
| 758 | ret = seq_open(file, &xt_tgt_seq_ops); |
| 759 | if (!ret) { |
| 760 | struct seq_file *seq = file->private_data; |
| 761 | struct proc_dir_entry *pde = PDE(inode); |
| 762 | |
| 763 | seq->private = pde; |
| 764 | } |
| 765 | |
| 766 | return ret; |
| 767 | } |
| 768 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 769 | static const struct file_operations xt_file_ops = { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 770 | .owner = THIS_MODULE, |
| 771 | .open = xt_tgt_open, |
| 772 | .read = seq_read, |
| 773 | .llseek = seq_lseek, |
| 774 | .release = seq_release, |
| 775 | }; |
| 776 | |
| 777 | #define FORMAT_TABLES "_tables_names" |
| 778 | #define FORMAT_MATCHES "_tables_matches" |
| 779 | #define FORMAT_TARGETS "_tables_targets" |
| 780 | |
| 781 | #endif /* CONFIG_PROC_FS */ |
| 782 | |
| 783 | int xt_proto_init(int af) |
| 784 | { |
| 785 | #ifdef CONFIG_PROC_FS |
| 786 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 787 | struct proc_dir_entry *proc; |
| 788 | #endif |
| 789 | |
| 790 | if (af >= NPROTO) |
| 791 | return -EINVAL; |
| 792 | |
| 793 | |
| 794 | #ifdef CONFIG_PROC_FS |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 795 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 796 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
| 797 | proc = proc_net_fops_create(buf, 0440, &xt_file_ops); |
| 798 | if (!proc) |
| 799 | goto out; |
| 800 | proc->data = (void *) ((unsigned long) af | (TABLE << 16)); |
| 801 | |
| 802 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 803 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 804 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
| 805 | proc = proc_net_fops_create(buf, 0440, &xt_file_ops); |
| 806 | if (!proc) |
| 807 | goto out_remove_tables; |
| 808 | proc->data = (void *) ((unsigned long) af | (MATCH << 16)); |
| 809 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 810 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 811 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
| 812 | proc = proc_net_fops_create(buf, 0440, &xt_file_ops); |
| 813 | if (!proc) |
| 814 | goto out_remove_matches; |
| 815 | proc->data = (void *) ((unsigned long) af | (TARGET << 16)); |
| 816 | #endif |
| 817 | |
| 818 | return 0; |
| 819 | |
| 820 | #ifdef CONFIG_PROC_FS |
| 821 | out_remove_matches: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 822 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 823 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
| 824 | proc_net_remove(buf); |
| 825 | |
| 826 | out_remove_tables: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 827 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 828 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
| 829 | proc_net_remove(buf); |
| 830 | out: |
| 831 | return -1; |
| 832 | #endif |
| 833 | } |
| 834 | EXPORT_SYMBOL_GPL(xt_proto_init); |
| 835 | |
| 836 | void xt_proto_fini(int af) |
| 837 | { |
| 838 | #ifdef CONFIG_PROC_FS |
| 839 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 840 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 841 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 842 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
| 843 | proc_net_remove(buf); |
| 844 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 845 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 846 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
| 847 | proc_net_remove(buf); |
| 848 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 849 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 850 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
| 851 | proc_net_remove(buf); |
| 852 | #endif /*CONFIG_PROC_FS*/ |
| 853 | } |
| 854 | EXPORT_SYMBOL_GPL(xt_proto_fini); |
| 855 | |
| 856 | |
| 857 | static int __init xt_init(void) |
| 858 | { |
| 859 | int i; |
| 860 | |
| 861 | xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL); |
| 862 | if (!xt) |
| 863 | return -ENOMEM; |
| 864 | |
| 865 | for (i = 0; i < NPROTO; i++) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 866 | mutex_init(&xt[i].mutex); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 867 | #ifdef CONFIG_COMPAT |
| 868 | mutex_init(&xt[i].compat_mutex); |
| 869 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 870 | INIT_LIST_HEAD(&xt[i].target); |
| 871 | INIT_LIST_HEAD(&xt[i].match); |
| 872 | INIT_LIST_HEAD(&xt[i].tables); |
| 873 | } |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | static void __exit xt_fini(void) |
| 878 | { |
| 879 | kfree(xt); |
| 880 | } |
| 881 | |
| 882 | module_init(xt_init); |
| 883 | module_exit(xt_fini); |
| 884 | |