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