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