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