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