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