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