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 | */ |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 18 | #include <linux/socket.h> |
| 19 | #include <linux/net.h> |
| 20 | #include <linux/proc_fs.h> |
| 21 | #include <linux/seq_file.h> |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/vmalloc.h> |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Al Viro | d7fe0f2 | 2006-12-03 23:15:30 -0500 | [diff] [blame] | 25 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Thomas Graf | fbabf31 | 2011-01-16 18:12:59 +0100 | [diff] [blame] | 27 | #include <linux/audit.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 28 | #include <net/net_namespace.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 29 | |
| 30 | #include <linux/netfilter/x_tables.h> |
| 31 | #include <linux/netfilter_arp.h> |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 32 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 33 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 34 | #include <linux/netfilter_arp/arp_tables.h> |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 35 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 36 | MODULE_LICENSE("GPL"); |
| 37 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 38 | MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 39 | |
| 40 | #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1)) |
| 41 | |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 42 | struct compat_delta { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 43 | unsigned int offset; /* offset in kernel */ |
| 44 | int delta; /* delta in 32bit user land */ |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 47 | struct xt_af { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 48 | struct mutex mutex; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 49 | struct list_head match; |
| 50 | struct list_head target; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 51 | #ifdef CONFIG_COMPAT |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 52 | struct mutex compat_mutex; |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 53 | struct compat_delta *compat_tab; |
| 54 | unsigned int number; /* number of slots in compat_tab[] */ |
| 55 | unsigned int cur; /* number of used slots in compat_tab[] */ |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 56 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static struct xt_af *xt; |
| 60 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 61 | static const char *const xt_prefix[NFPROTO_NUMPROTO] = { |
| 62 | [NFPROTO_UNSPEC] = "x", |
| 63 | [NFPROTO_IPV4] = "ip", |
| 64 | [NFPROTO_ARP] = "arp", |
| 65 | [NFPROTO_BRIDGE] = "eb", |
| 66 | [NFPROTO_IPV6] = "ip6", |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 69 | /* Allow this many total (re)entries. */ |
| 70 | static const unsigned int xt_jumpstack_multiplier = 2; |
| 71 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 72 | /* Registration hooks for targets. */ |
| 73 | int |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 74 | xt_register_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 75 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 76 | u_int8_t af = target->family; |
| 77 | int ret; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 78 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 79 | ret = mutex_lock_interruptible(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 80 | if (ret != 0) |
| 81 | return ret; |
| 82 | list_add(&target->list, &xt[af].target); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 83 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 84 | return ret; |
| 85 | } |
| 86 | EXPORT_SYMBOL(xt_register_target); |
| 87 | |
| 88 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 89 | xt_unregister_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 90 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 91 | u_int8_t af = target->family; |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 92 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 93 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 94 | list_del(&target->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 95 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 96 | } |
| 97 | EXPORT_SYMBOL(xt_unregister_target); |
| 98 | |
| 99 | int |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 100 | xt_register_targets(struct xt_target *target, unsigned int n) |
| 101 | { |
| 102 | unsigned int i; |
| 103 | int err = 0; |
| 104 | |
| 105 | for (i = 0; i < n; i++) { |
| 106 | err = xt_register_target(&target[i]); |
| 107 | if (err) |
| 108 | goto err; |
| 109 | } |
| 110 | return err; |
| 111 | |
| 112 | err: |
| 113 | if (i > 0) |
| 114 | xt_unregister_targets(target, i); |
| 115 | return err; |
| 116 | } |
| 117 | EXPORT_SYMBOL(xt_register_targets); |
| 118 | |
| 119 | void |
| 120 | xt_unregister_targets(struct xt_target *target, unsigned int n) |
| 121 | { |
Changli Gao | f68c530 | 2010-10-04 22:24:12 +0200 | [diff] [blame] | 122 | while (n-- > 0) |
| 123 | xt_unregister_target(&target[n]); |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 124 | } |
| 125 | EXPORT_SYMBOL(xt_unregister_targets); |
| 126 | |
| 127 | int |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 128 | xt_register_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 129 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 130 | u_int8_t af = match->family; |
| 131 | int ret; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 132 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 133 | ret = mutex_lock_interruptible(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 134 | if (ret != 0) |
| 135 | return ret; |
| 136 | |
| 137 | list_add(&match->list, &xt[af].match); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 138 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | EXPORT_SYMBOL(xt_register_match); |
| 143 | |
| 144 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 145 | xt_unregister_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 146 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 147 | u_int8_t af = match->family; |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 148 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 149 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 150 | list_del(&match->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 151 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 152 | } |
| 153 | EXPORT_SYMBOL(xt_unregister_match); |
| 154 | |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 155 | int |
| 156 | xt_register_matches(struct xt_match *match, unsigned int n) |
| 157 | { |
| 158 | unsigned int i; |
| 159 | int err = 0; |
| 160 | |
| 161 | for (i = 0; i < n; i++) { |
| 162 | err = xt_register_match(&match[i]); |
| 163 | if (err) |
| 164 | goto err; |
| 165 | } |
| 166 | return err; |
| 167 | |
| 168 | err: |
| 169 | if (i > 0) |
| 170 | xt_unregister_matches(match, i); |
| 171 | return err; |
| 172 | } |
| 173 | EXPORT_SYMBOL(xt_register_matches); |
| 174 | |
| 175 | void |
| 176 | xt_unregister_matches(struct xt_match *match, unsigned int n) |
| 177 | { |
Changli Gao | f68c530 | 2010-10-04 22:24:12 +0200 | [diff] [blame] | 178 | while (n-- > 0) |
| 179 | xt_unregister_match(&match[n]); |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 180 | } |
| 181 | EXPORT_SYMBOL(xt_unregister_matches); |
| 182 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * These are weird, but module loading must not be done with mutex |
| 186 | * held (since they will register), and we have to have a single |
Stephen Hemminger | adb00ae | 2011-03-09 14:14:26 +0100 | [diff] [blame] | 187 | * function to use. |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 188 | */ |
| 189 | |
| 190 | /* Find match, grabs ref. Returns ERR_PTR() on error. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 191 | struct xt_match *xt_find_match(u8 af, const char *name, u8 revision) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 192 | { |
| 193 | struct xt_match *m; |
Patrick McHardy | 42046e2 | 2011-03-14 19:11:44 +0100 | [diff] [blame] | 194 | int err = -ENOENT; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 195 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 196 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 197 | return ERR_PTR(-EINTR); |
| 198 | |
| 199 | list_for_each_entry(m, &xt[af].match, list) { |
| 200 | if (strcmp(m->name, name) == 0) { |
| 201 | if (m->revision == revision) { |
| 202 | if (try_module_get(m->me)) { |
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 m; |
| 205 | } |
| 206 | } else |
| 207 | err = -EPROTOTYPE; /* Found something. */ |
| 208 | } |
| 209 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 210 | mutex_unlock(&xt[af].mutex); |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 211 | |
| 212 | if (af != NFPROTO_UNSPEC) |
| 213 | /* Try searching again in the family-independent list */ |
| 214 | return xt_find_match(NFPROTO_UNSPEC, name, revision); |
| 215 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 216 | return ERR_PTR(err); |
| 217 | } |
| 218 | EXPORT_SYMBOL(xt_find_match); |
| 219 | |
Jan Engelhardt | fd0ec0e | 2009-07-10 19:27:47 +0200 | [diff] [blame] | 220 | struct xt_match * |
| 221 | xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision) |
| 222 | { |
| 223 | struct xt_match *match; |
| 224 | |
Stephen Hemminger | adb00ae | 2011-03-09 14:14:26 +0100 | [diff] [blame] | 225 | match = xt_find_match(nfproto, name, revision); |
| 226 | if (IS_ERR(match)) { |
| 227 | request_module("%st_%s", xt_prefix[nfproto], name); |
| 228 | match = xt_find_match(nfproto, name, revision); |
| 229 | } |
| 230 | |
| 231 | return match; |
Jan Engelhardt | fd0ec0e | 2009-07-10 19:27:47 +0200 | [diff] [blame] | 232 | } |
| 233 | EXPORT_SYMBOL_GPL(xt_request_find_match); |
| 234 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 235 | /* Find target, grabs ref. Returns ERR_PTR() on error. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 236 | struct xt_target *xt_find_target(u8 af, const char *name, u8 revision) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 237 | { |
| 238 | struct xt_target *t; |
Patrick McHardy | 42046e2 | 2011-03-14 19:11:44 +0100 | [diff] [blame] | 239 | int err = -ENOENT; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 240 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 241 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 242 | return ERR_PTR(-EINTR); |
| 243 | |
| 244 | list_for_each_entry(t, &xt[af].target, list) { |
| 245 | if (strcmp(t->name, name) == 0) { |
| 246 | if (t->revision == revision) { |
| 247 | if (try_module_get(t->me)) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 248 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 249 | return t; |
| 250 | } |
| 251 | } else |
| 252 | err = -EPROTOTYPE; /* Found something. */ |
| 253 | } |
| 254 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 255 | mutex_unlock(&xt[af].mutex); |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 256 | |
| 257 | if (af != NFPROTO_UNSPEC) |
| 258 | /* Try searching again in the family-independent list */ |
| 259 | return xt_find_target(NFPROTO_UNSPEC, name, revision); |
| 260 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 261 | return ERR_PTR(err); |
| 262 | } |
| 263 | EXPORT_SYMBOL(xt_find_target); |
| 264 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 265 | struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 266 | { |
| 267 | struct xt_target *target; |
| 268 | |
Stephen Hemminger | adb00ae | 2011-03-09 14:14:26 +0100 | [diff] [blame] | 269 | target = xt_find_target(af, name, revision); |
| 270 | if (IS_ERR(target)) { |
| 271 | request_module("%st_%s", xt_prefix[af], name); |
| 272 | target = xt_find_target(af, name, revision); |
| 273 | } |
| 274 | |
| 275 | return target; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 276 | } |
| 277 | EXPORT_SYMBOL_GPL(xt_request_find_target); |
| 278 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 279 | static int match_revfn(u8 af, const char *name, u8 revision, int *bestp) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 280 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 281 | const struct xt_match *m; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 282 | int have_rev = 0; |
| 283 | |
| 284 | list_for_each_entry(m, &xt[af].match, list) { |
| 285 | if (strcmp(m->name, name) == 0) { |
| 286 | if (m->revision > *bestp) |
| 287 | *bestp = m->revision; |
| 288 | if (m->revision == revision) |
| 289 | have_rev = 1; |
| 290 | } |
| 291 | } |
Patrick McHardy | 656caff | 2009-01-12 00:06:04 +0000 | [diff] [blame] | 292 | |
| 293 | if (af != NFPROTO_UNSPEC && !have_rev) |
| 294 | return match_revfn(NFPROTO_UNSPEC, name, revision, bestp); |
| 295 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 296 | return have_rev; |
| 297 | } |
| 298 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 299 | static int target_revfn(u8 af, const char *name, u8 revision, int *bestp) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 300 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 301 | const struct xt_target *t; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 302 | int have_rev = 0; |
| 303 | |
| 304 | list_for_each_entry(t, &xt[af].target, list) { |
| 305 | if (strcmp(t->name, name) == 0) { |
| 306 | if (t->revision > *bestp) |
| 307 | *bestp = t->revision; |
| 308 | if (t->revision == revision) |
| 309 | have_rev = 1; |
| 310 | } |
| 311 | } |
Patrick McHardy | 656caff | 2009-01-12 00:06:04 +0000 | [diff] [blame] | 312 | |
| 313 | if (af != NFPROTO_UNSPEC && !have_rev) |
| 314 | return target_revfn(NFPROTO_UNSPEC, name, revision, bestp); |
| 315 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 316 | return have_rev; |
| 317 | } |
| 318 | |
| 319 | /* Returns true or false (if no such extension at all) */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 320 | int xt_find_revision(u8 af, const char *name, u8 revision, int target, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 321 | int *err) |
| 322 | { |
| 323 | int have_rev, best = -1; |
| 324 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 325 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 326 | *err = -EINTR; |
| 327 | return 1; |
| 328 | } |
| 329 | if (target == 1) |
| 330 | have_rev = target_revfn(af, name, revision, &best); |
| 331 | else |
| 332 | have_rev = match_revfn(af, name, revision, &best); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 333 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 334 | |
| 335 | /* Nothing at all? Return 0 to try loading module. */ |
| 336 | if (best == -1) { |
| 337 | *err = -ENOENT; |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | *err = best; |
| 342 | if (!have_rev) |
| 343 | *err = -EPROTONOSUPPORT; |
| 344 | return 1; |
| 345 | } |
| 346 | EXPORT_SYMBOL_GPL(xt_find_revision); |
| 347 | |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 348 | static char * |
| 349 | textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto) |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 350 | { |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 351 | static const char *const inetbr_names[] = { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 352 | "PREROUTING", "INPUT", "FORWARD", |
| 353 | "OUTPUT", "POSTROUTING", "BROUTING", |
| 354 | }; |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 355 | static const char *const arp_names[] = { |
| 356 | "INPUT", "FORWARD", "OUTPUT", |
| 357 | }; |
| 358 | const char *const *names; |
| 359 | unsigned int i, max; |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 360 | char *p = buf; |
| 361 | bool np = false; |
| 362 | int res; |
| 363 | |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 364 | names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names; |
| 365 | max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) : |
| 366 | ARRAY_SIZE(inetbr_names); |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 367 | *p = '\0'; |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 368 | for (i = 0; i < max; ++i) { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 369 | if (!(mask & (1 << i))) |
| 370 | continue; |
| 371 | res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]); |
| 372 | if (res > 0) { |
| 373 | size -= res; |
| 374 | p += res; |
| 375 | } |
| 376 | np = true; |
| 377 | } |
| 378 | |
| 379 | return buf; |
| 380 | } |
| 381 | |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 382 | int xt_check_match(struct xt_mtchk_param *par, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 383 | unsigned int size, u_int8_t proto, bool inv_proto) |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 384 | { |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 385 | int ret; |
| 386 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 387 | if (XT_ALIGN(par->match->matchsize) != size && |
| 388 | par->match->matchsize != -1) { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 389 | /* |
| 390 | * ebt_among is exempt from centralized matchsize checking |
| 391 | * because it uses a dynamic-size data set. |
| 392 | */ |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 393 | pr_err("%s_tables: %s.%u match: invalid size " |
| 394 | "%u (kernel) != (user) %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 395 | xt_prefix[par->family], par->match->name, |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 396 | par->match->revision, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 397 | XT_ALIGN(par->match->matchsize), size); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 398 | return -EINVAL; |
| 399 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 400 | if (par->match->table != NULL && |
| 401 | strcmp(par->match->table, par->table) != 0) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 402 | pr_err("%s_tables: %s match: only valid in %s table, not %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 403 | xt_prefix[par->family], par->match->name, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 404 | par->match->table, par->table); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 405 | return -EINVAL; |
| 406 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 407 | if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 408 | char used[64], allow[64]; |
| 409 | |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 410 | pr_err("%s_tables: %s match: used from hooks %s, but only " |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 411 | "valid from %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 412 | xt_prefix[par->family], par->match->name, |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 413 | textify_hooks(used, sizeof(used), par->hook_mask, |
| 414 | par->family), |
| 415 | textify_hooks(allow, sizeof(allow), par->match->hooks, |
| 416 | par->family)); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 417 | return -EINVAL; |
| 418 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 419 | if (par->match->proto && (par->match->proto != proto || inv_proto)) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 420 | pr_err("%s_tables: %s match: only valid for protocol %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 421 | xt_prefix[par->family], par->match->name, |
| 422 | par->match->proto); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 423 | return -EINVAL; |
| 424 | } |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 425 | if (par->match->checkentry != NULL) { |
| 426 | ret = par->match->checkentry(par); |
| 427 | if (ret < 0) |
| 428 | return ret; |
| 429 | else if (ret > 0) |
| 430 | /* Flag up potential errors. */ |
| 431 | return -EIO; |
| 432 | } |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | EXPORT_SYMBOL_GPL(xt_check_match); |
| 436 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 437 | #ifdef CONFIG_COMPAT |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 438 | int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta) |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 439 | { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 440 | struct xt_af *xp = &xt[af]; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 441 | |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 442 | if (!xp->compat_tab) { |
| 443 | if (!xp->number) |
| 444 | return -EINVAL; |
| 445 | xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number); |
| 446 | if (!xp->compat_tab) |
| 447 | return -ENOMEM; |
| 448 | xp->cur = 0; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 449 | } |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 450 | |
| 451 | if (xp->cur >= xp->number) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | if (xp->cur) |
| 455 | delta += xp->compat_tab[xp->cur - 1].delta; |
| 456 | xp->compat_tab[xp->cur].offset = offset; |
| 457 | xp->compat_tab[xp->cur].delta = delta; |
| 458 | xp->cur++; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | EXPORT_SYMBOL_GPL(xt_compat_add_offset); |
| 462 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 463 | void xt_compat_flush_offsets(u_int8_t af) |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 464 | { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 465 | if (xt[af].compat_tab) { |
| 466 | vfree(xt[af].compat_tab); |
| 467 | xt[af].compat_tab = NULL; |
| 468 | xt[af].number = 0; |
Eric Dumazet | 5a6351e | 2011-04-21 10:57:21 +0200 | [diff] [blame] | 469 | xt[af].cur = 0; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | EXPORT_SYMBOL_GPL(xt_compat_flush_offsets); |
| 473 | |
Florian Westphal | 3e5e524 | 2010-02-15 18:17:10 +0100 | [diff] [blame] | 474 | int xt_compat_calc_jump(u_int8_t af, unsigned int offset) |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 475 | { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 476 | struct compat_delta *tmp = xt[af].compat_tab; |
| 477 | int mid, left = 0, right = xt[af].cur - 1; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 478 | |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 479 | while (left <= right) { |
| 480 | mid = (left + right) >> 1; |
| 481 | if (offset > tmp[mid].offset) |
| 482 | left = mid + 1; |
| 483 | else if (offset < tmp[mid].offset) |
| 484 | right = mid - 1; |
| 485 | else |
| 486 | return mid ? tmp[mid - 1].delta : 0; |
| 487 | } |
Eric Dumazet | 5a6351e | 2011-04-21 10:57:21 +0200 | [diff] [blame] | 488 | return left ? tmp[left - 1].delta : 0; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 489 | } |
| 490 | EXPORT_SYMBOL_GPL(xt_compat_calc_jump); |
| 491 | |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 492 | void xt_compat_init_offsets(u_int8_t af, unsigned int number) |
| 493 | { |
| 494 | xt[af].number = number; |
| 495 | xt[af].cur = 0; |
| 496 | } |
| 497 | EXPORT_SYMBOL(xt_compat_init_offsets); |
| 498 | |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 499 | int xt_compat_match_offset(const struct xt_match *match) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 500 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 501 | u_int16_t csize = match->compatsize ? : match->matchsize; |
| 502 | return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 503 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 504 | EXPORT_SYMBOL_GPL(xt_compat_match_offset); |
| 505 | |
Patrick McHardy | 8956695 | 2007-12-17 21:46:40 -0800 | [diff] [blame] | 506 | 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] | 507 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 508 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 509 | const struct xt_match *match = m->u.kernel.match; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 510 | struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m; |
| 511 | int pad, off = xt_compat_match_offset(match); |
| 512 | u_int16_t msize = cm->u.user.match_size; |
| 513 | |
| 514 | m = *dstptr; |
| 515 | memcpy(m, cm, sizeof(*cm)); |
| 516 | if (match->compat_from_user) |
| 517 | match->compat_from_user(m->data, cm->data); |
| 518 | else |
| 519 | memcpy(m->data, cm->data, msize - sizeof(*cm)); |
| 520 | pad = XT_ALIGN(match->matchsize) - match->matchsize; |
| 521 | if (pad > 0) |
| 522 | memset(m->data + match->matchsize, 0, pad); |
| 523 | |
| 524 | msize += off; |
| 525 | m->u.user.match_size = msize; |
| 526 | |
| 527 | *size += off; |
| 528 | *dstptr += msize; |
Patrick McHardy | 8956695 | 2007-12-17 21:46:40 -0800 | [diff] [blame] | 529 | return 0; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 530 | } |
| 531 | EXPORT_SYMBOL_GPL(xt_compat_match_from_user); |
| 532 | |
Jan Engelhardt | 739674f | 2009-06-26 08:23:19 +0200 | [diff] [blame] | 533 | int xt_compat_match_to_user(const struct xt_entry_match *m, |
| 534 | void __user **dstptr, unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 535 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 536 | const struct xt_match *match = m->u.kernel.match; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 537 | struct compat_xt_entry_match __user *cm = *dstptr; |
| 538 | int off = xt_compat_match_offset(match); |
| 539 | u_int16_t msize = m->u.user.match_size - off; |
| 540 | |
| 541 | if (copy_to_user(cm, m, sizeof(*cm)) || |
Patrick McHardy | a18aa31 | 2007-12-12 10:35:16 -0800 | [diff] [blame] | 542 | put_user(msize, &cm->u.user.match_size) || |
| 543 | copy_to_user(cm->u.user.name, m->u.kernel.match->name, |
| 544 | strlen(m->u.kernel.match->name) + 1)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 545 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 546 | |
| 547 | if (match->compat_to_user) { |
| 548 | if (match->compat_to_user((void __user *)cm->data, m->data)) |
| 549 | return -EFAULT; |
| 550 | } else { |
| 551 | if (copy_to_user(cm->data, m->data, msize - sizeof(*cm))) |
| 552 | return -EFAULT; |
| 553 | } |
| 554 | |
| 555 | *size -= off; |
| 556 | *dstptr += msize; |
| 557 | return 0; |
| 558 | } |
| 559 | EXPORT_SYMBOL_GPL(xt_compat_match_to_user); |
| 560 | #endif /* CONFIG_COMPAT */ |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 561 | |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 562 | int xt_check_target(struct xt_tgchk_param *par, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 563 | unsigned int size, u_int8_t proto, bool inv_proto) |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 564 | { |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 565 | int ret; |
| 566 | |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 567 | if (XT_ALIGN(par->target->targetsize) != size) { |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 568 | pr_err("%s_tables: %s.%u target: invalid size " |
| 569 | "%u (kernel) != (user) %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 570 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 571 | par->target->revision, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 572 | XT_ALIGN(par->target->targetsize), size); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 573 | return -EINVAL; |
| 574 | } |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 575 | if (par->target->table != NULL && |
| 576 | strcmp(par->target->table, par->table) != 0) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 577 | pr_err("%s_tables: %s target: only valid in %s table, not %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 578 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 579 | par->target->table, par->table); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 580 | return -EINVAL; |
| 581 | } |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 582 | if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 583 | char used[64], allow[64]; |
| 584 | |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 585 | pr_err("%s_tables: %s target: used from hooks %s, but only " |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 586 | "usable from %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 587 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 588 | textify_hooks(used, sizeof(used), par->hook_mask, |
| 589 | par->family), |
| 590 | textify_hooks(allow, sizeof(allow), par->target->hooks, |
| 591 | par->family)); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 592 | return -EINVAL; |
| 593 | } |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 594 | if (par->target->proto && (par->target->proto != proto || inv_proto)) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 595 | pr_err("%s_tables: %s target: only valid for protocol %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 596 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 597 | par->target->proto); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 598 | return -EINVAL; |
| 599 | } |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 600 | if (par->target->checkentry != NULL) { |
| 601 | ret = par->target->checkentry(par); |
| 602 | if (ret < 0) |
| 603 | return ret; |
| 604 | else if (ret > 0) |
| 605 | /* Flag up potential errors. */ |
| 606 | return -EIO; |
| 607 | } |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 608 | return 0; |
| 609 | } |
| 610 | EXPORT_SYMBOL_GPL(xt_check_target); |
| 611 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 612 | #ifdef CONFIG_COMPAT |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 613 | int xt_compat_target_offset(const struct xt_target *target) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 614 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 615 | u_int16_t csize = target->compatsize ? : target->targetsize; |
| 616 | return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 617 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 618 | EXPORT_SYMBOL_GPL(xt_compat_target_offset); |
| 619 | |
| 620 | 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] | 621 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 622 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 623 | const struct xt_target *target = t->u.kernel.target; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 624 | struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t; |
| 625 | int pad, off = xt_compat_target_offset(target); |
| 626 | u_int16_t tsize = ct->u.user.target_size; |
| 627 | |
| 628 | t = *dstptr; |
| 629 | memcpy(t, ct, sizeof(*ct)); |
| 630 | if (target->compat_from_user) |
| 631 | target->compat_from_user(t->data, ct->data); |
| 632 | else |
| 633 | memcpy(t->data, ct->data, tsize - sizeof(*ct)); |
| 634 | pad = XT_ALIGN(target->targetsize) - target->targetsize; |
| 635 | if (pad > 0) |
| 636 | memset(t->data + target->targetsize, 0, pad); |
| 637 | |
| 638 | tsize += off; |
| 639 | t->u.user.target_size = tsize; |
| 640 | |
| 641 | *size += off; |
| 642 | *dstptr += tsize; |
| 643 | } |
| 644 | EXPORT_SYMBOL_GPL(xt_compat_target_from_user); |
| 645 | |
Jan Engelhardt | 739674f | 2009-06-26 08:23:19 +0200 | [diff] [blame] | 646 | int xt_compat_target_to_user(const struct xt_entry_target *t, |
| 647 | void __user **dstptr, unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 648 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 649 | const struct xt_target *target = t->u.kernel.target; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 650 | struct compat_xt_entry_target __user *ct = *dstptr; |
| 651 | int off = xt_compat_target_offset(target); |
| 652 | u_int16_t tsize = t->u.user.target_size - off; |
| 653 | |
| 654 | if (copy_to_user(ct, t, sizeof(*ct)) || |
Patrick McHardy | a18aa31 | 2007-12-12 10:35:16 -0800 | [diff] [blame] | 655 | put_user(tsize, &ct->u.user.target_size) || |
| 656 | copy_to_user(ct->u.user.name, t->u.kernel.target->name, |
| 657 | strlen(t->u.kernel.target->name) + 1)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 658 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 659 | |
| 660 | if (target->compat_to_user) { |
| 661 | if (target->compat_to_user((void __user *)ct->data, t->data)) |
| 662 | return -EFAULT; |
| 663 | } else { |
| 664 | if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct))) |
| 665 | return -EFAULT; |
| 666 | } |
| 667 | |
| 668 | *size -= off; |
| 669 | *dstptr += tsize; |
| 670 | return 0; |
| 671 | } |
| 672 | EXPORT_SYMBOL_GPL(xt_compat_target_to_user); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 673 | #endif |
| 674 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 675 | struct xt_table_info *xt_alloc_table_info(unsigned int size) |
| 676 | { |
| 677 | struct xt_table_info *newinfo; |
| 678 | int cpu; |
| 679 | |
| 680 | /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */ |
Jan Beulich | 4481374 | 2009-09-21 17:03:05 -0700 | [diff] [blame] | 681 | if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 682 | return NULL; |
| 683 | |
Eric Dumazet | 259d4e4 | 2007-12-04 23:24:56 -0800 | [diff] [blame] | 684 | newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 685 | if (!newinfo) |
| 686 | return NULL; |
| 687 | |
| 688 | newinfo->size = size; |
| 689 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 690 | for_each_possible_cpu(cpu) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 691 | if (size <= PAGE_SIZE) |
| 692 | newinfo->entries[cpu] = kmalloc_node(size, |
| 693 | GFP_KERNEL, |
| 694 | cpu_to_node(cpu)); |
| 695 | else |
| 696 | newinfo->entries[cpu] = vmalloc_node(size, |
| 697 | cpu_to_node(cpu)); |
| 698 | |
| 699 | if (newinfo->entries[cpu] == NULL) { |
| 700 | xt_free_table_info(newinfo); |
| 701 | return NULL; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return newinfo; |
| 706 | } |
| 707 | EXPORT_SYMBOL(xt_alloc_table_info); |
| 708 | |
| 709 | void xt_free_table_info(struct xt_table_info *info) |
| 710 | { |
| 711 | int cpu; |
| 712 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 713 | for_each_possible_cpu(cpu) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 714 | if (info->size <= PAGE_SIZE) |
| 715 | kfree(info->entries[cpu]); |
| 716 | else |
| 717 | vfree(info->entries[cpu]); |
| 718 | } |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 719 | |
| 720 | if (info->jumpstack != NULL) { |
| 721 | if (sizeof(void *) * info->stacksize > PAGE_SIZE) { |
| 722 | for_each_possible_cpu(cpu) |
| 723 | vfree(info->jumpstack[cpu]); |
| 724 | } else { |
| 725 | for_each_possible_cpu(cpu) |
| 726 | kfree(info->jumpstack[cpu]); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | if (sizeof(void **) * nr_cpu_ids > PAGE_SIZE) |
| 731 | vfree(info->jumpstack); |
| 732 | else |
| 733 | kfree(info->jumpstack); |
Eric Dumazet | 7489aec | 2010-05-31 16:41:35 +0200 | [diff] [blame] | 734 | |
| 735 | free_percpu(info->stackptr); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 736 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 737 | kfree(info); |
| 738 | } |
| 739 | EXPORT_SYMBOL(xt_free_table_info); |
| 740 | |
| 741 | /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 742 | struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af, |
| 743 | const char *name) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 744 | { |
| 745 | struct xt_table *t; |
| 746 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 747 | if (mutex_lock_interruptible(&xt[af].mutex) != 0) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 748 | return ERR_PTR(-EINTR); |
| 749 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 750 | list_for_each_entry(t, &net->xt.tables[af], list) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 751 | if (strcmp(t->name, name) == 0 && try_module_get(t->me)) |
| 752 | return t; |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 753 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 754 | return NULL; |
| 755 | } |
| 756 | EXPORT_SYMBOL_GPL(xt_find_table_lock); |
| 757 | |
| 758 | void xt_table_unlock(struct xt_table *table) |
| 759 | { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 760 | mutex_unlock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 761 | } |
| 762 | EXPORT_SYMBOL_GPL(xt_table_unlock); |
| 763 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 764 | #ifdef CONFIG_COMPAT |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 765 | void xt_compat_lock(u_int8_t af) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 766 | { |
| 767 | mutex_lock(&xt[af].compat_mutex); |
| 768 | } |
| 769 | EXPORT_SYMBOL_GPL(xt_compat_lock); |
| 770 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 771 | void xt_compat_unlock(u_int8_t af) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 772 | { |
| 773 | mutex_unlock(&xt[af].compat_mutex); |
| 774 | } |
| 775 | EXPORT_SYMBOL_GPL(xt_compat_unlock); |
| 776 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 777 | |
Eric Dumazet | 7f5c6d4 | 2011-04-04 17:04:03 +0200 | [diff] [blame] | 778 | DEFINE_PER_CPU(seqcount_t, xt_recseq); |
| 779 | EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq); |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 780 | |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 781 | static int xt_jumpstack_alloc(struct xt_table_info *i) |
| 782 | { |
| 783 | unsigned int size; |
| 784 | int cpu; |
| 785 | |
Eric Dumazet | 7489aec | 2010-05-31 16:41:35 +0200 | [diff] [blame] | 786 | i->stackptr = alloc_percpu(unsigned int); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 787 | if (i->stackptr == NULL) |
| 788 | return -ENOMEM; |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 789 | |
| 790 | size = sizeof(void **) * nr_cpu_ids; |
| 791 | if (size > PAGE_SIZE) |
Joe Perches | 3dbd443 | 2011-05-28 10:36:35 -0700 | [diff] [blame] | 792 | i->jumpstack = vzalloc(size); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 793 | else |
Joe Perches | 3dbd443 | 2011-05-28 10:36:35 -0700 | [diff] [blame] | 794 | i->jumpstack = kzalloc(size, GFP_KERNEL); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 795 | if (i->jumpstack == NULL) |
| 796 | return -ENOMEM; |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 797 | |
| 798 | i->stacksize *= xt_jumpstack_multiplier; |
| 799 | size = sizeof(void *) * i->stacksize; |
| 800 | for_each_possible_cpu(cpu) { |
| 801 | if (size > PAGE_SIZE) |
| 802 | i->jumpstack[cpu] = vmalloc_node(size, |
| 803 | cpu_to_node(cpu)); |
| 804 | else |
| 805 | i->jumpstack[cpu] = kmalloc_node(size, |
| 806 | GFP_KERNEL, cpu_to_node(cpu)); |
| 807 | if (i->jumpstack[cpu] == NULL) |
| 808 | /* |
| 809 | * Freeing will be done later on by the callers. The |
| 810 | * chain is: xt_replace_table -> __do_replace -> |
| 811 | * do_replace -> xt_free_table_info. |
| 812 | */ |
| 813 | return -ENOMEM; |
| 814 | } |
| 815 | |
| 816 | return 0; |
| 817 | } |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 818 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 819 | struct xt_table_info * |
| 820 | xt_replace_table(struct xt_table *table, |
| 821 | unsigned int num_counters, |
| 822 | struct xt_table_info *newinfo, |
| 823 | int *error) |
| 824 | { |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 825 | struct xt_table_info *private; |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 826 | int ret; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 827 | |
Jan Engelhardt | d97a9e4 | 2010-04-21 14:45:51 +0200 | [diff] [blame] | 828 | ret = xt_jumpstack_alloc(newinfo); |
| 829 | if (ret < 0) { |
| 830 | *error = ret; |
| 831 | return NULL; |
| 832 | } |
| 833 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 834 | /* Do the substitution. */ |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 835 | local_bh_disable(); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 836 | private = table->private; |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 837 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 838 | /* Check inside lock: is the old number correct? */ |
| 839 | if (num_counters != private->number) { |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 840 | pr_debug("num_counters != table->private->number (%u/%u)\n", |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 841 | num_counters, private->number); |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 842 | local_bh_enable(); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 843 | *error = -EAGAIN; |
| 844 | return NULL; |
| 845 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 846 | |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 847 | table->private = newinfo; |
| 848 | newinfo->initial_entries = private->initial_entries; |
| 849 | |
| 850 | /* |
| 851 | * Even though table entries have now been swapped, other CPU's |
| 852 | * may still be using the old entries. This is okay, because |
| 853 | * resynchronization happens because of the locking done |
| 854 | * during the get_counters() routine. |
| 855 | */ |
| 856 | local_bh_enable(); |
| 857 | |
Thomas Graf | fbabf31 | 2011-01-16 18:12:59 +0100 | [diff] [blame] | 858 | #ifdef CONFIG_AUDIT |
| 859 | if (audit_enabled) { |
| 860 | struct audit_buffer *ab; |
| 861 | |
| 862 | ab = audit_log_start(current->audit_context, GFP_KERNEL, |
| 863 | AUDIT_NETFILTER_CFG); |
| 864 | if (ab) { |
| 865 | audit_log_format(ab, "table=%s family=%u entries=%u", |
| 866 | table->name, table->af, |
| 867 | private->number); |
| 868 | audit_log_end(ab); |
| 869 | } |
| 870 | } |
| 871 | #endif |
| 872 | |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 873 | return private; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 874 | } |
| 875 | EXPORT_SYMBOL_GPL(xt_replace_table); |
| 876 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 877 | struct xt_table *xt_register_table(struct net *net, |
| 878 | const struct xt_table *input_table, |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 879 | struct xt_table_info *bootstrap, |
| 880 | struct xt_table_info *newinfo) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 881 | { |
| 882 | int ret; |
| 883 | struct xt_table_info *private; |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 884 | struct xt_table *t, *table; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 885 | |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 886 | /* Don't add one object to multiple lists. */ |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 887 | table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 888 | if (!table) { |
| 889 | ret = -ENOMEM; |
| 890 | goto out; |
| 891 | } |
| 892 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 893 | ret = mutex_lock_interruptible(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 894 | if (ret != 0) |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 895 | goto out_free; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 896 | |
| 897 | /* Don't autoload: we'd eat our tail... */ |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 898 | list_for_each_entry(t, &net->xt.tables[table->af], list) { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 899 | if (strcmp(t->name, table->name) == 0) { |
| 900 | ret = -EEXIST; |
| 901 | goto unlock; |
| 902 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* Simplifies replace_table code. */ |
| 906 | table->private = bootstrap; |
Stephen Hemminger | 7845447 | 2009-02-20 10:35:32 +0100 | [diff] [blame] | 907 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 908 | if (!xt_replace_table(table, 0, newinfo, &ret)) |
| 909 | goto unlock; |
| 910 | |
| 911 | private = table->private; |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 912 | pr_debug("table->private->number = %u\n", private->number); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 913 | |
| 914 | /* save number of initial entries */ |
| 915 | private->initial_entries = private->number; |
| 916 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 917 | list_add(&table->list, &net->xt.tables[table->af]); |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 918 | mutex_unlock(&xt[table->af].mutex); |
| 919 | return table; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 920 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 921 | unlock: |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 922 | mutex_unlock(&xt[table->af].mutex); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 923 | out_free: |
| 924 | kfree(table); |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 925 | out: |
| 926 | return ERR_PTR(ret); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 927 | } |
| 928 | EXPORT_SYMBOL_GPL(xt_register_table); |
| 929 | |
| 930 | void *xt_unregister_table(struct xt_table *table) |
| 931 | { |
| 932 | struct xt_table_info *private; |
| 933 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 934 | mutex_lock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 935 | private = table->private; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 936 | list_del(&table->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 937 | mutex_unlock(&xt[table->af].mutex); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 938 | kfree(table); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 939 | |
| 940 | return private; |
| 941 | } |
| 942 | EXPORT_SYMBOL_GPL(xt_unregister_table); |
| 943 | |
| 944 | #ifdef CONFIG_PROC_FS |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 945 | struct xt_names_priv { |
| 946 | struct seq_net_private p; |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 947 | u_int8_t af; |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 948 | }; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 949 | 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] | 950 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 951 | struct xt_names_priv *priv = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 952 | struct net *net = seq_file_net(seq); |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 953 | u_int8_t af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 954 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 955 | mutex_lock(&xt[af].mutex); |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 956 | return seq_list_start(&net->xt.tables[af], *pos); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 959 | 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] | 960 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 961 | struct xt_names_priv *priv = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 962 | struct net *net = seq_file_net(seq); |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 963 | u_int8_t af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 964 | |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 965 | return seq_list_next(v, &net->xt.tables[af], pos); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 966 | } |
| 967 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 968 | static void xt_table_seq_stop(struct seq_file *seq, void *v) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 969 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 970 | struct xt_names_priv *priv = seq->private; |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 971 | u_int8_t af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 972 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 973 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 974 | } |
| 975 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 976 | static int xt_table_seq_show(struct seq_file *seq, void *v) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 977 | { |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 978 | struct xt_table *table = list_entry(v, struct xt_table, list); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 979 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 980 | if (strlen(table->name)) |
| 981 | return seq_printf(seq, "%s\n", table->name); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 982 | else |
| 983 | return 0; |
| 984 | } |
| 985 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 986 | static const struct seq_operations xt_table_seq_ops = { |
| 987 | .start = xt_table_seq_start, |
| 988 | .next = xt_table_seq_next, |
| 989 | .stop = xt_table_seq_stop, |
| 990 | .show = xt_table_seq_show, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 991 | }; |
| 992 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 993 | static int xt_table_open(struct inode *inode, struct file *file) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 994 | { |
| 995 | int ret; |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 996 | struct xt_names_priv *priv; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 997 | |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 998 | ret = seq_open_net(inode, file, &xt_table_seq_ops, |
| 999 | sizeof(struct xt_names_priv)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1000 | if (!ret) { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1001 | priv = ((struct seq_file *)file->private_data)->private; |
| 1002 | priv->af = (unsigned long)PDE(inode)->data; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1003 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1004 | return ret; |
| 1005 | } |
| 1006 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1007 | static const struct file_operations xt_table_ops = { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1008 | .owner = THIS_MODULE, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1009 | .open = xt_table_open, |
| 1010 | .read = seq_read, |
| 1011 | .llseek = seq_lseek, |
Pavel Emelyanov | 0e93bb94 | 2008-04-29 03:15:35 -0700 | [diff] [blame] | 1012 | .release = seq_release_net, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1013 | }; |
| 1014 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1015 | /* |
| 1016 | * Traverse state for ip{,6}_{tables,matches} for helping crossing |
| 1017 | * the multi-AF mutexes. |
| 1018 | */ |
| 1019 | struct nf_mttg_trav { |
| 1020 | struct list_head *head, *curr; |
| 1021 | uint8_t class, nfproto; |
| 1022 | }; |
| 1023 | |
| 1024 | enum { |
| 1025 | MTTG_TRAV_INIT, |
| 1026 | MTTG_TRAV_NFP_UNSPEC, |
| 1027 | MTTG_TRAV_NFP_SPEC, |
| 1028 | MTTG_TRAV_DONE, |
| 1029 | }; |
| 1030 | |
| 1031 | static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos, |
| 1032 | bool is_target) |
| 1033 | { |
| 1034 | static const uint8_t next_class[] = { |
| 1035 | [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC, |
| 1036 | [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE, |
| 1037 | }; |
| 1038 | struct nf_mttg_trav *trav = seq->private; |
| 1039 | |
| 1040 | switch (trav->class) { |
| 1041 | case MTTG_TRAV_INIT: |
| 1042 | trav->class = MTTG_TRAV_NFP_UNSPEC; |
| 1043 | mutex_lock(&xt[NFPROTO_UNSPEC].mutex); |
| 1044 | trav->head = trav->curr = is_target ? |
| 1045 | &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match; |
| 1046 | break; |
| 1047 | case MTTG_TRAV_NFP_UNSPEC: |
| 1048 | trav->curr = trav->curr->next; |
| 1049 | if (trav->curr != trav->head) |
| 1050 | break; |
| 1051 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); |
| 1052 | mutex_lock(&xt[trav->nfproto].mutex); |
| 1053 | trav->head = trav->curr = is_target ? |
| 1054 | &xt[trav->nfproto].target : &xt[trav->nfproto].match; |
| 1055 | trav->class = next_class[trav->class]; |
| 1056 | break; |
| 1057 | case MTTG_TRAV_NFP_SPEC: |
| 1058 | trav->curr = trav->curr->next; |
| 1059 | if (trav->curr != trav->head) |
| 1060 | break; |
| 1061 | /* fallthru, _stop will unlock */ |
| 1062 | default: |
| 1063 | return NULL; |
| 1064 | } |
| 1065 | |
| 1066 | if (ppos != NULL) |
| 1067 | ++*ppos; |
| 1068 | return trav; |
| 1069 | } |
| 1070 | |
| 1071 | static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos, |
| 1072 | bool is_target) |
| 1073 | { |
| 1074 | struct nf_mttg_trav *trav = seq->private; |
| 1075 | unsigned int j; |
| 1076 | |
| 1077 | trav->class = MTTG_TRAV_INIT; |
| 1078 | for (j = 0; j < *pos; ++j) |
| 1079 | if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL) |
| 1080 | return NULL; |
| 1081 | return trav; |
| 1082 | } |
| 1083 | |
| 1084 | static void xt_mttg_seq_stop(struct seq_file *seq, void *v) |
| 1085 | { |
| 1086 | struct nf_mttg_trav *trav = seq->private; |
| 1087 | |
| 1088 | switch (trav->class) { |
| 1089 | case MTTG_TRAV_NFP_UNSPEC: |
| 1090 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); |
| 1091 | break; |
| 1092 | case MTTG_TRAV_NFP_SPEC: |
| 1093 | mutex_unlock(&xt[trav->nfproto].mutex); |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1098 | static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos) |
| 1099 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1100 | return xt_mttg_seq_start(seq, pos, false); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1103 | static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1104 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1105 | return xt_mttg_seq_next(seq, v, ppos, false); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | static int xt_match_seq_show(struct seq_file *seq, void *v) |
| 1109 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1110 | const struct nf_mttg_trav *trav = seq->private; |
| 1111 | const struct xt_match *match; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1112 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1113 | switch (trav->class) { |
| 1114 | case MTTG_TRAV_NFP_UNSPEC: |
| 1115 | case MTTG_TRAV_NFP_SPEC: |
| 1116 | if (trav->curr == trav->head) |
| 1117 | return 0; |
| 1118 | match = list_entry(trav->curr, struct xt_match, list); |
| 1119 | return (*match->name == '\0') ? 0 : |
| 1120 | seq_printf(seq, "%s\n", match->name); |
| 1121 | } |
| 1122 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | static const struct seq_operations xt_match_seq_ops = { |
| 1126 | .start = xt_match_seq_start, |
| 1127 | .next = xt_match_seq_next, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1128 | .stop = xt_mttg_seq_stop, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1129 | .show = xt_match_seq_show, |
| 1130 | }; |
| 1131 | |
| 1132 | static int xt_match_open(struct inode *inode, struct file *file) |
| 1133 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1134 | struct seq_file *seq; |
| 1135 | struct nf_mttg_trav *trav; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1136 | int ret; |
| 1137 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1138 | trav = kmalloc(sizeof(*trav), GFP_KERNEL); |
| 1139 | if (trav == NULL) |
| 1140 | return -ENOMEM; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1141 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1142 | ret = seq_open(file, &xt_match_seq_ops); |
| 1143 | if (ret < 0) { |
| 1144 | kfree(trav); |
| 1145 | return ret; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1146 | } |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1147 | |
| 1148 | seq = file->private_data; |
| 1149 | seq->private = trav; |
| 1150 | trav->nfproto = (unsigned long)PDE(inode)->data; |
| 1151 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | static const struct file_operations xt_match_ops = { |
| 1155 | .owner = THIS_MODULE, |
| 1156 | .open = xt_match_open, |
| 1157 | .read = seq_read, |
| 1158 | .llseek = seq_lseek, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1159 | .release = seq_release_private, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1160 | }; |
| 1161 | |
| 1162 | static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos) |
| 1163 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1164 | return xt_mttg_seq_start(seq, pos, true); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1167 | static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos) |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1168 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1169 | return xt_mttg_seq_next(seq, v, ppos, true); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | static int xt_target_seq_show(struct seq_file *seq, void *v) |
| 1173 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1174 | const struct nf_mttg_trav *trav = seq->private; |
| 1175 | const struct xt_target *target; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1176 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1177 | switch (trav->class) { |
| 1178 | case MTTG_TRAV_NFP_UNSPEC: |
| 1179 | case MTTG_TRAV_NFP_SPEC: |
| 1180 | if (trav->curr == trav->head) |
| 1181 | return 0; |
| 1182 | target = list_entry(trav->curr, struct xt_target, list); |
| 1183 | return (*target->name == '\0') ? 0 : |
| 1184 | seq_printf(seq, "%s\n", target->name); |
| 1185 | } |
| 1186 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | static const struct seq_operations xt_target_seq_ops = { |
| 1190 | .start = xt_target_seq_start, |
| 1191 | .next = xt_target_seq_next, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1192 | .stop = xt_mttg_seq_stop, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1193 | .show = xt_target_seq_show, |
| 1194 | }; |
| 1195 | |
| 1196 | static int xt_target_open(struct inode *inode, struct file *file) |
| 1197 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1198 | struct seq_file *seq; |
| 1199 | struct nf_mttg_trav *trav; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1200 | int ret; |
| 1201 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1202 | trav = kmalloc(sizeof(*trav), GFP_KERNEL); |
| 1203 | if (trav == NULL) |
| 1204 | return -ENOMEM; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1205 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1206 | ret = seq_open(file, &xt_target_seq_ops); |
| 1207 | if (ret < 0) { |
| 1208 | kfree(trav); |
| 1209 | return ret; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1210 | } |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1211 | |
| 1212 | seq = file->private_data; |
| 1213 | seq->private = trav; |
| 1214 | trav->nfproto = (unsigned long)PDE(inode)->data; |
| 1215 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | static const struct file_operations xt_target_ops = { |
| 1219 | .owner = THIS_MODULE, |
| 1220 | .open = xt_target_open, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1221 | .read = seq_read, |
| 1222 | .llseek = seq_lseek, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1223 | .release = seq_release_private, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1224 | }; |
| 1225 | |
| 1226 | #define FORMAT_TABLES "_tables_names" |
| 1227 | #define FORMAT_MATCHES "_tables_matches" |
| 1228 | #define FORMAT_TARGETS "_tables_targets" |
| 1229 | |
| 1230 | #endif /* CONFIG_PROC_FS */ |
| 1231 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1232 | /** |
| 1233 | * xt_hook_link - set up hooks for a new table |
| 1234 | * @table: table with metadata needed to set up hooks |
| 1235 | * @fn: Hook function |
| 1236 | * |
| 1237 | * This function will take care of creating and registering the necessary |
| 1238 | * Netfilter hooks for XT tables. |
| 1239 | */ |
| 1240 | struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn) |
| 1241 | { |
| 1242 | unsigned int hook_mask = table->valid_hooks; |
| 1243 | uint8_t i, num_hooks = hweight32(hook_mask); |
| 1244 | uint8_t hooknum; |
| 1245 | struct nf_hook_ops *ops; |
| 1246 | int ret; |
| 1247 | |
| 1248 | ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL); |
| 1249 | if (ops == NULL) |
| 1250 | return ERR_PTR(-ENOMEM); |
| 1251 | |
| 1252 | for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0; |
| 1253 | hook_mask >>= 1, ++hooknum) { |
| 1254 | if (!(hook_mask & 1)) |
| 1255 | continue; |
| 1256 | ops[i].hook = fn; |
| 1257 | ops[i].owner = table->me; |
| 1258 | ops[i].pf = table->af; |
| 1259 | ops[i].hooknum = hooknum; |
| 1260 | ops[i].priority = table->priority; |
| 1261 | ++i; |
| 1262 | } |
| 1263 | |
| 1264 | ret = nf_register_hooks(ops, num_hooks); |
| 1265 | if (ret < 0) { |
| 1266 | kfree(ops); |
| 1267 | return ERR_PTR(ret); |
| 1268 | } |
| 1269 | |
| 1270 | return ops; |
| 1271 | } |
| 1272 | EXPORT_SYMBOL_GPL(xt_hook_link); |
| 1273 | |
| 1274 | /** |
| 1275 | * xt_hook_unlink - remove hooks for a table |
| 1276 | * @ops: nf_hook_ops array as returned by nf_hook_link |
| 1277 | * @hook_mask: the very same mask that was passed to nf_hook_link |
| 1278 | */ |
| 1279 | void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops) |
| 1280 | { |
| 1281 | nf_unregister_hooks(ops, hweight32(table->valid_hooks)); |
| 1282 | kfree(ops); |
| 1283 | } |
| 1284 | EXPORT_SYMBOL_GPL(xt_hook_unlink); |
| 1285 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1286 | int xt_proto_init(struct net *net, u_int8_t af) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1287 | { |
| 1288 | #ifdef CONFIG_PROC_FS |
| 1289 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 1290 | struct proc_dir_entry *proc; |
| 1291 | #endif |
| 1292 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1293 | if (af >= ARRAY_SIZE(xt_prefix)) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1294 | return -EINVAL; |
| 1295 | |
| 1296 | |
| 1297 | #ifdef CONFIG_PROC_FS |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1298 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1299 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Denis V. Lunev | 8b16924 | 2008-05-02 04:11:52 -0700 | [diff] [blame] | 1300 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops, |
| 1301 | (void *)(unsigned long)af); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1302 | if (!proc) |
| 1303 | goto out; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1304 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1305 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1306 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Denis V. Lunev | 8b16924 | 2008-05-02 04:11:52 -0700 | [diff] [blame] | 1307 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops, |
| 1308 | (void *)(unsigned long)af); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1309 | if (!proc) |
| 1310 | goto out_remove_tables; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1311 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1312 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1313 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
Denis V. Lunev | 8b16924 | 2008-05-02 04:11:52 -0700 | [diff] [blame] | 1314 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops, |
| 1315 | (void *)(unsigned long)af); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1316 | if (!proc) |
| 1317 | goto out_remove_matches; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1318 | #endif |
| 1319 | |
| 1320 | return 0; |
| 1321 | |
| 1322 | #ifdef CONFIG_PROC_FS |
| 1323 | out_remove_matches: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1324 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1325 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame^] | 1326 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1327 | |
| 1328 | out_remove_tables: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1329 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1330 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame^] | 1331 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1332 | out: |
| 1333 | return -1; |
| 1334 | #endif |
| 1335 | } |
| 1336 | EXPORT_SYMBOL_GPL(xt_proto_init); |
| 1337 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1338 | void xt_proto_fini(struct net *net, u_int8_t af) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1339 | { |
| 1340 | #ifdef CONFIG_PROC_FS |
| 1341 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 1342 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1343 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1344 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame^] | 1345 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1346 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1347 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1348 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame^] | 1349 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1350 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1351 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1352 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame^] | 1353 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1354 | #endif /*CONFIG_PROC_FS*/ |
| 1355 | } |
| 1356 | EXPORT_SYMBOL_GPL(xt_proto_fini); |
| 1357 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1358 | static int __net_init xt_net_init(struct net *net) |
| 1359 | { |
| 1360 | int i; |
| 1361 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1362 | for (i = 0; i < NFPROTO_NUMPROTO; i++) |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1363 | INIT_LIST_HEAD(&net->xt.tables[i]); |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | static struct pernet_operations xt_net_ops = { |
| 1368 | .init = xt_net_init, |
| 1369 | }; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1370 | |
| 1371 | static int __init xt_init(void) |
| 1372 | { |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1373 | unsigned int i; |
| 1374 | int rv; |
| 1375 | |
| 1376 | for_each_possible_cpu(i) { |
Eric Dumazet | 7f5c6d4 | 2011-04-04 17:04:03 +0200 | [diff] [blame] | 1377 | seqcount_init(&per_cpu(xt_recseq, i)); |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1378 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1379 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1380 | xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1381 | if (!xt) |
| 1382 | return -ENOMEM; |
| 1383 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1384 | for (i = 0; i < NFPROTO_NUMPROTO; i++) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1385 | mutex_init(&xt[i].mutex); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1386 | #ifdef CONFIG_COMPAT |
| 1387 | mutex_init(&xt[i].compat_mutex); |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 1388 | xt[i].compat_tab = NULL; |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1389 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1390 | INIT_LIST_HEAD(&xt[i].target); |
| 1391 | INIT_LIST_HEAD(&xt[i].match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1392 | } |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1393 | rv = register_pernet_subsys(&xt_net_ops); |
| 1394 | if (rv < 0) |
| 1395 | kfree(xt); |
| 1396 | return rv; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static void __exit xt_fini(void) |
| 1400 | { |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1401 | unregister_pernet_subsys(&xt_net_ops); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1402 | kfree(xt); |
| 1403 | } |
| 1404 | |
| 1405 | module_init(xt_init); |
| 1406 | module_exit(xt_fini); |
| 1407 | |