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