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