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> |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 29 | #include <linux/user_namespace.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 30 | #include <net/net_namespace.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 31 | |
| 32 | #include <linux/netfilter/x_tables.h> |
| 33 | #include <linux/netfilter_arp.h> |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 34 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 35 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 36 | #include <linux/netfilter_arp/arp_tables.h> |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 37 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 38 | MODULE_LICENSE("GPL"); |
| 39 | MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>"); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 40 | MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 41 | |
Florian Westphal | db6a0cb | 2016-11-22 14:44:19 +0100 | [diff] [blame] | 42 | #define XT_PCPU_BLOCK_SIZE 4096 |
| 43 | |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 44 | struct compat_delta { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 45 | unsigned int offset; /* offset in kernel */ |
| 46 | int delta; /* delta in 32bit user land */ |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 47 | }; |
| 48 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 49 | struct xt_af { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 50 | struct mutex mutex; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 51 | struct list_head match; |
| 52 | struct list_head target; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 53 | #ifdef CONFIG_COMPAT |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 54 | struct mutex compat_mutex; |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 55 | struct compat_delta *compat_tab; |
| 56 | unsigned int number; /* number of slots in compat_tab[] */ |
| 57 | unsigned int cur; /* number of used slots in compat_tab[] */ |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 58 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static struct xt_af *xt; |
| 62 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 63 | static const char *const xt_prefix[NFPROTO_NUMPROTO] = { |
| 64 | [NFPROTO_UNSPEC] = "x", |
| 65 | [NFPROTO_IPV4] = "ip", |
| 66 | [NFPROTO_ARP] = "arp", |
| 67 | [NFPROTO_BRIDGE] = "eb", |
| 68 | [NFPROTO_IPV6] = "ip6", |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 69 | }; |
| 70 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 71 | /* Registration hooks for targets. */ |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 72 | int xt_register_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 73 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 74 | u_int8_t af = target->family; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 75 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 76 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 77 | list_add(&target->list, &xt[af].target); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 78 | mutex_unlock(&xt[af].mutex); |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 79 | return 0; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 80 | } |
| 81 | EXPORT_SYMBOL(xt_register_target); |
| 82 | |
| 83 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 84 | xt_unregister_target(struct xt_target *target) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 85 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 86 | u_int8_t af = target->family; |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 87 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 88 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 89 | list_del(&target->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 90 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 91 | } |
| 92 | EXPORT_SYMBOL(xt_unregister_target); |
| 93 | |
| 94 | int |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 95 | xt_register_targets(struct xt_target *target, unsigned int n) |
| 96 | { |
| 97 | unsigned int i; |
| 98 | int err = 0; |
| 99 | |
| 100 | for (i = 0; i < n; i++) { |
| 101 | err = xt_register_target(&target[i]); |
| 102 | if (err) |
| 103 | goto err; |
| 104 | } |
| 105 | return err; |
| 106 | |
| 107 | err: |
| 108 | if (i > 0) |
| 109 | xt_unregister_targets(target, i); |
| 110 | return err; |
| 111 | } |
| 112 | EXPORT_SYMBOL(xt_register_targets); |
| 113 | |
| 114 | void |
| 115 | xt_unregister_targets(struct xt_target *target, unsigned int n) |
| 116 | { |
Changli Gao | f68c530 | 2010-10-04 22:24:12 +0200 | [diff] [blame] | 117 | while (n-- > 0) |
| 118 | xt_unregister_target(&target[n]); |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 119 | } |
| 120 | EXPORT_SYMBOL(xt_unregister_targets); |
| 121 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 122 | int xt_register_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 123 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 124 | u_int8_t af = match->family; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 125 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 126 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 127 | list_add(&match->list, &xt[af].match); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 128 | mutex_unlock(&xt[af].mutex); |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 129 | return 0; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 130 | } |
| 131 | EXPORT_SYMBOL(xt_register_match); |
| 132 | |
| 133 | void |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 134 | xt_unregister_match(struct xt_match *match) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 135 | { |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 136 | u_int8_t af = match->family; |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 137 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 138 | mutex_lock(&xt[af].mutex); |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 139 | list_del(&match->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 140 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 141 | } |
| 142 | EXPORT_SYMBOL(xt_unregister_match); |
| 143 | |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 144 | int |
| 145 | xt_register_matches(struct xt_match *match, unsigned int n) |
| 146 | { |
| 147 | unsigned int i; |
| 148 | int err = 0; |
| 149 | |
| 150 | for (i = 0; i < n; i++) { |
| 151 | err = xt_register_match(&match[i]); |
| 152 | if (err) |
| 153 | goto err; |
| 154 | } |
| 155 | return err; |
| 156 | |
| 157 | err: |
| 158 | if (i > 0) |
| 159 | xt_unregister_matches(match, i); |
| 160 | return err; |
| 161 | } |
| 162 | EXPORT_SYMBOL(xt_register_matches); |
| 163 | |
| 164 | void |
| 165 | xt_unregister_matches(struct xt_match *match, unsigned int n) |
| 166 | { |
Changli Gao | f68c530 | 2010-10-04 22:24:12 +0200 | [diff] [blame] | 167 | while (n-- > 0) |
| 168 | xt_unregister_match(&match[n]); |
Patrick McHardy | 52d9c42 | 2006-08-22 00:33:45 -0700 | [diff] [blame] | 169 | } |
| 170 | EXPORT_SYMBOL(xt_unregister_matches); |
| 171 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * These are weird, but module loading must not be done with mutex |
| 175 | * held (since they will register), and we have to have a single |
Stephen Hemminger | adb00ae | 2011-03-09 14:14:26 +0100 | [diff] [blame] | 176 | * function to use. |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 177 | */ |
| 178 | |
| 179 | /* Find match, grabs ref. Returns ERR_PTR() on error. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 180 | 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] | 181 | { |
| 182 | struct xt_match *m; |
Patrick McHardy | 42046e2 | 2011-03-14 19:11:44 +0100 | [diff] [blame] | 183 | int err = -ENOENT; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 184 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 185 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 186 | list_for_each_entry(m, &xt[af].match, list) { |
| 187 | if (strcmp(m->name, name) == 0) { |
| 188 | if (m->revision == revision) { |
| 189 | if (try_module_get(m->me)) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 190 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 191 | return m; |
| 192 | } |
| 193 | } else |
| 194 | err = -EPROTOTYPE; /* Found something. */ |
| 195 | } |
| 196 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 197 | mutex_unlock(&xt[af].mutex); |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 198 | |
| 199 | if (af != NFPROTO_UNSPEC) |
| 200 | /* Try searching again in the family-independent list */ |
| 201 | return xt_find_match(NFPROTO_UNSPEC, name, revision); |
| 202 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 203 | return ERR_PTR(err); |
| 204 | } |
| 205 | EXPORT_SYMBOL(xt_find_match); |
| 206 | |
Jan Engelhardt | fd0ec0e | 2009-07-10 19:27:47 +0200 | [diff] [blame] | 207 | struct xt_match * |
| 208 | xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision) |
| 209 | { |
| 210 | struct xt_match *match; |
| 211 | |
Eric Dumazet | b39f3f3 | 2018-01-24 17:16:09 -0800 | [diff] [blame] | 212 | if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN) |
| 213 | return ERR_PTR(-EINVAL); |
| 214 | |
Stephen Hemminger | adb00ae | 2011-03-09 14:14:26 +0100 | [diff] [blame] | 215 | match = xt_find_match(nfproto, name, revision); |
| 216 | if (IS_ERR(match)) { |
| 217 | request_module("%st_%s", xt_prefix[nfproto], name); |
| 218 | match = xt_find_match(nfproto, name, revision); |
| 219 | } |
| 220 | |
| 221 | return match; |
Jan Engelhardt | fd0ec0e | 2009-07-10 19:27:47 +0200 | [diff] [blame] | 222 | } |
| 223 | EXPORT_SYMBOL_GPL(xt_request_find_match); |
| 224 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 225 | /* Find target, grabs ref. Returns ERR_PTR() on error. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 226 | 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] | 227 | { |
| 228 | struct xt_target *t; |
Patrick McHardy | 42046e2 | 2011-03-14 19:11:44 +0100 | [diff] [blame] | 229 | int err = -ENOENT; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 230 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 231 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 232 | list_for_each_entry(t, &xt[af].target, list) { |
| 233 | if (strcmp(t->name, name) == 0) { |
| 234 | if (t->revision == revision) { |
| 235 | if (try_module_get(t->me)) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 236 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 237 | return t; |
| 238 | } |
| 239 | } else |
| 240 | err = -EPROTOTYPE; /* Found something. */ |
| 241 | } |
| 242 | } |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 243 | mutex_unlock(&xt[af].mutex); |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 244 | |
| 245 | if (af != NFPROTO_UNSPEC) |
| 246 | /* Try searching again in the family-independent list */ |
| 247 | return xt_find_target(NFPROTO_UNSPEC, name, revision); |
| 248 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 249 | return ERR_PTR(err); |
| 250 | } |
| 251 | EXPORT_SYMBOL(xt_find_target); |
| 252 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 253 | 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] | 254 | { |
| 255 | struct xt_target *target; |
| 256 | |
Eric Dumazet | b39f3f3 | 2018-01-24 17:16:09 -0800 | [diff] [blame] | 257 | if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN) |
| 258 | return ERR_PTR(-EINVAL); |
| 259 | |
Stephen Hemminger | adb00ae | 2011-03-09 14:14:26 +0100 | [diff] [blame] | 260 | target = xt_find_target(af, name, revision); |
| 261 | if (IS_ERR(target)) { |
| 262 | request_module("%st_%s", xt_prefix[af], name); |
| 263 | target = xt_find_target(af, name, revision); |
| 264 | } |
| 265 | |
| 266 | return target; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 267 | } |
| 268 | EXPORT_SYMBOL_GPL(xt_request_find_target); |
| 269 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 270 | 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] | 271 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 272 | const struct xt_match *m; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 273 | int have_rev = 0; |
| 274 | |
| 275 | list_for_each_entry(m, &xt[af].match, list) { |
| 276 | if (strcmp(m->name, name) == 0) { |
| 277 | if (m->revision > *bestp) |
| 278 | *bestp = m->revision; |
| 279 | if (m->revision == revision) |
| 280 | have_rev = 1; |
| 281 | } |
| 282 | } |
Patrick McHardy | 656caff | 2009-01-12 00:06:04 +0000 | [diff] [blame] | 283 | |
| 284 | if (af != NFPROTO_UNSPEC && !have_rev) |
| 285 | return match_revfn(NFPROTO_UNSPEC, name, revision, bestp); |
| 286 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 287 | return have_rev; |
| 288 | } |
| 289 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 290 | 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] | 291 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 292 | const struct xt_target *t; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 293 | int have_rev = 0; |
| 294 | |
| 295 | list_for_each_entry(t, &xt[af].target, list) { |
| 296 | if (strcmp(t->name, name) == 0) { |
| 297 | if (t->revision > *bestp) |
| 298 | *bestp = t->revision; |
| 299 | if (t->revision == revision) |
| 300 | have_rev = 1; |
| 301 | } |
| 302 | } |
Patrick McHardy | 656caff | 2009-01-12 00:06:04 +0000 | [diff] [blame] | 303 | |
| 304 | if (af != NFPROTO_UNSPEC && !have_rev) |
| 305 | return target_revfn(NFPROTO_UNSPEC, name, revision, bestp); |
| 306 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 307 | return have_rev; |
| 308 | } |
| 309 | |
| 310 | /* Returns true or false (if no such extension at all) */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 311 | 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] | 312 | int *err) |
| 313 | { |
| 314 | int have_rev, best = -1; |
| 315 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 316 | mutex_lock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 317 | if (target == 1) |
| 318 | have_rev = target_revfn(af, name, revision, &best); |
| 319 | else |
| 320 | have_rev = match_revfn(af, name, revision, &best); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 321 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 322 | |
| 323 | /* Nothing at all? Return 0 to try loading module. */ |
| 324 | if (best == -1) { |
| 325 | *err = -ENOENT; |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | *err = best; |
| 330 | if (!have_rev) |
| 331 | *err = -EPROTONOSUPPORT; |
| 332 | return 1; |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(xt_find_revision); |
| 335 | |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 336 | static char * |
| 337 | 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] | 338 | { |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 339 | static const char *const inetbr_names[] = { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 340 | "PREROUTING", "INPUT", "FORWARD", |
| 341 | "OUTPUT", "POSTROUTING", "BROUTING", |
| 342 | }; |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 343 | static const char *const arp_names[] = { |
| 344 | "INPUT", "FORWARD", "OUTPUT", |
| 345 | }; |
| 346 | const char *const *names; |
| 347 | unsigned int i, max; |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 348 | char *p = buf; |
| 349 | bool np = false; |
| 350 | int res; |
| 351 | |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 352 | names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names; |
| 353 | max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) : |
| 354 | ARRAY_SIZE(inetbr_names); |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 355 | *p = '\0'; |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 356 | for (i = 0; i < max; ++i) { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 357 | if (!(mask & (1 << i))) |
| 358 | continue; |
| 359 | res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]); |
| 360 | if (res > 0) { |
| 361 | size -= res; |
| 362 | p += res; |
| 363 | } |
| 364 | np = true; |
| 365 | } |
| 366 | |
| 367 | return buf; |
| 368 | } |
| 369 | |
Florian Westphal | c6ab7c6 | 2018-03-10 01:15:45 +0100 | [diff] [blame] | 370 | /** |
| 371 | * xt_check_proc_name - check that name is suitable for /proc file creation |
| 372 | * |
| 373 | * @name: file name candidate |
| 374 | * @size: length of buffer |
| 375 | * |
| 376 | * some x_tables modules wish to create a file in /proc. |
| 377 | * This function makes sure that the name is suitable for this |
| 378 | * purpose, it checks that name is NUL terminated and isn't a 'special' |
| 379 | * name, like "..". |
| 380 | * |
| 381 | * returns negative number on error or 0 if name is useable. |
| 382 | */ |
| 383 | int xt_check_proc_name(const char *name, unsigned int size) |
| 384 | { |
| 385 | if (name[0] == '\0') |
| 386 | return -EINVAL; |
| 387 | |
| 388 | if (strnlen(name, size) == size) |
| 389 | return -ENAMETOOLONG; |
| 390 | |
| 391 | if (strcmp(name, ".") == 0 || |
| 392 | strcmp(name, "..") == 0 || |
| 393 | strchr(name, '/')) |
| 394 | return -EINVAL; |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | EXPORT_SYMBOL(xt_check_proc_name); |
| 399 | |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 400 | int xt_check_match(struct xt_mtchk_param *par, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 401 | unsigned int size, u_int8_t proto, bool inv_proto) |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 402 | { |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 403 | int ret; |
| 404 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 405 | if (XT_ALIGN(par->match->matchsize) != size && |
| 406 | par->match->matchsize != -1) { |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 407 | /* |
| 408 | * ebt_among is exempt from centralized matchsize checking |
| 409 | * because it uses a dynamic-size data set. |
| 410 | */ |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 411 | pr_err("%s_tables: %s.%u match: invalid size " |
| 412 | "%u (kernel) != (user) %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 413 | xt_prefix[par->family], par->match->name, |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 414 | par->match->revision, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 415 | XT_ALIGN(par->match->matchsize), size); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 416 | return -EINVAL; |
| 417 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 418 | if (par->match->table != NULL && |
| 419 | strcmp(par->match->table, par->table) != 0) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 420 | 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] | 421 | xt_prefix[par->family], par->match->name, |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 422 | par->match->table, par->table); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 423 | return -EINVAL; |
| 424 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 425 | if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 426 | char used[64], allow[64]; |
| 427 | |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 428 | pr_err("%s_tables: %s match: used from hooks %s, but only " |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 429 | "valid from %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 430 | xt_prefix[par->family], par->match->name, |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 431 | textify_hooks(used, sizeof(used), par->hook_mask, |
| 432 | par->family), |
| 433 | textify_hooks(allow, sizeof(allow), par->match->hooks, |
| 434 | par->family)); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 435 | return -EINVAL; |
| 436 | } |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 437 | if (par->match->proto && (par->match->proto != proto || inv_proto)) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 438 | pr_err("%s_tables: %s match: only valid for protocol %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 439 | xt_prefix[par->family], par->match->name, |
| 440 | par->match->proto); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 441 | return -EINVAL; |
| 442 | } |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 443 | if (par->match->checkentry != NULL) { |
| 444 | ret = par->match->checkentry(par); |
| 445 | if (ret < 0) |
| 446 | return ret; |
| 447 | else if (ret > 0) |
| 448 | /* Flag up potential errors. */ |
| 449 | return -EIO; |
| 450 | } |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | EXPORT_SYMBOL_GPL(xt_check_match); |
| 454 | |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 455 | /** xt_check_entry_match - check that matches end before start of target |
| 456 | * |
| 457 | * @match: beginning of xt_entry_match |
| 458 | * @target: beginning of this rules target (alleged end of matches) |
| 459 | * @alignment: alignment requirement of match structures |
| 460 | * |
| 461 | * Validates that all matches add up to the beginning of the target, |
| 462 | * and that each match covers at least the base structure size. |
| 463 | * |
| 464 | * Return: 0 on success, negative errno on failure. |
| 465 | */ |
| 466 | static int xt_check_entry_match(const char *match, const char *target, |
| 467 | const size_t alignment) |
| 468 | { |
| 469 | const struct xt_entry_match *pos; |
| 470 | int length = target - match; |
| 471 | |
| 472 | if (length == 0) /* no matches */ |
| 473 | return 0; |
| 474 | |
| 475 | pos = (struct xt_entry_match *)match; |
| 476 | do { |
| 477 | if ((unsigned long)pos % alignment) |
| 478 | return -EINVAL; |
| 479 | |
| 480 | if (length < (int)sizeof(struct xt_entry_match)) |
| 481 | return -EINVAL; |
| 482 | |
| 483 | if (pos->u.match_size < sizeof(struct xt_entry_match)) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | if (pos->u.match_size > length) |
| 487 | return -EINVAL; |
| 488 | |
| 489 | length -= pos->u.match_size; |
| 490 | pos = ((void *)((char *)(pos) + (pos)->u.match_size)); |
| 491 | } while (length > 0); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 496 | #ifdef CONFIG_COMPAT |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 497 | 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] | 498 | { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 499 | struct xt_af *xp = &xt[af]; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 500 | |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 501 | if (!xp->compat_tab) { |
| 502 | if (!xp->number) |
| 503 | return -EINVAL; |
| 504 | xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number); |
| 505 | if (!xp->compat_tab) |
| 506 | return -ENOMEM; |
| 507 | xp->cur = 0; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 508 | } |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 509 | |
| 510 | if (xp->cur >= xp->number) |
| 511 | return -EINVAL; |
| 512 | |
| 513 | if (xp->cur) |
| 514 | delta += xp->compat_tab[xp->cur - 1].delta; |
| 515 | xp->compat_tab[xp->cur].offset = offset; |
| 516 | xp->compat_tab[xp->cur].delta = delta; |
| 517 | xp->cur++; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | EXPORT_SYMBOL_GPL(xt_compat_add_offset); |
| 521 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 522 | void xt_compat_flush_offsets(u_int8_t af) |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 523 | { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 524 | if (xt[af].compat_tab) { |
| 525 | vfree(xt[af].compat_tab); |
| 526 | xt[af].compat_tab = NULL; |
| 527 | xt[af].number = 0; |
Eric Dumazet | 5a6351e | 2011-04-21 10:57:21 +0200 | [diff] [blame] | 528 | xt[af].cur = 0; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | EXPORT_SYMBOL_GPL(xt_compat_flush_offsets); |
| 532 | |
Florian Westphal | 3e5e524 | 2010-02-15 18:17:10 +0100 | [diff] [blame] | 533 | int xt_compat_calc_jump(u_int8_t af, unsigned int offset) |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 534 | { |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 535 | struct compat_delta *tmp = xt[af].compat_tab; |
| 536 | int mid, left = 0, right = xt[af].cur - 1; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 537 | |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 538 | while (left <= right) { |
| 539 | mid = (left + right) >> 1; |
| 540 | if (offset > tmp[mid].offset) |
| 541 | left = mid + 1; |
| 542 | else if (offset < tmp[mid].offset) |
| 543 | right = mid - 1; |
| 544 | else |
| 545 | return mid ? tmp[mid - 1].delta : 0; |
| 546 | } |
Eric Dumazet | 5a6351e | 2011-04-21 10:57:21 +0200 | [diff] [blame] | 547 | return left ? tmp[left - 1].delta : 0; |
Patrick McHardy | b386d9f | 2007-12-17 21:47:48 -0800 | [diff] [blame] | 548 | } |
| 549 | EXPORT_SYMBOL_GPL(xt_compat_calc_jump); |
| 550 | |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 551 | void xt_compat_init_offsets(u_int8_t af, unsigned int number) |
| 552 | { |
| 553 | xt[af].number = number; |
| 554 | xt[af].cur = 0; |
| 555 | } |
| 556 | EXPORT_SYMBOL(xt_compat_init_offsets); |
| 557 | |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 558 | int xt_compat_match_offset(const struct xt_match *match) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 559 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 560 | u_int16_t csize = match->compatsize ? : match->matchsize; |
| 561 | return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 562 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 563 | EXPORT_SYMBOL_GPL(xt_compat_match_offset); |
| 564 | |
Florian Westphal | 0188346 | 2016-04-01 14:17:33 +0200 | [diff] [blame] | 565 | void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr, |
| 566 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 567 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 568 | const struct xt_match *match = m->u.kernel.match; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 569 | struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m; |
| 570 | int pad, off = xt_compat_match_offset(match); |
| 571 | u_int16_t msize = cm->u.user.match_size; |
Florian Westphal | 09d9686 | 2016-04-01 14:17:34 +0200 | [diff] [blame] | 572 | char name[sizeof(m->u.user.name)]; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 573 | |
| 574 | m = *dstptr; |
| 575 | memcpy(m, cm, sizeof(*cm)); |
| 576 | if (match->compat_from_user) |
| 577 | match->compat_from_user(m->data, cm->data); |
| 578 | else |
| 579 | memcpy(m->data, cm->data, msize - sizeof(*cm)); |
| 580 | pad = XT_ALIGN(match->matchsize) - match->matchsize; |
| 581 | if (pad > 0) |
| 582 | memset(m->data + match->matchsize, 0, pad); |
| 583 | |
| 584 | msize += off; |
| 585 | m->u.user.match_size = msize; |
Florian Westphal | 09d9686 | 2016-04-01 14:17:34 +0200 | [diff] [blame] | 586 | strlcpy(name, match->name, sizeof(name)); |
| 587 | module_put(match->me); |
| 588 | strncpy(m->u.user.name, name, sizeof(m->u.user.name)); |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 589 | |
| 590 | *size += off; |
| 591 | *dstptr += msize; |
| 592 | } |
| 593 | EXPORT_SYMBOL_GPL(xt_compat_match_from_user); |
| 594 | |
Jan Engelhardt | 739674f | 2009-06-26 08:23:19 +0200 | [diff] [blame] | 595 | int xt_compat_match_to_user(const struct xt_entry_match *m, |
| 596 | void __user **dstptr, unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 597 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 598 | const struct xt_match *match = m->u.kernel.match; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 599 | struct compat_xt_entry_match __user *cm = *dstptr; |
| 600 | int off = xt_compat_match_offset(match); |
| 601 | u_int16_t msize = m->u.user.match_size - off; |
| 602 | |
| 603 | if (copy_to_user(cm, m, sizeof(*cm)) || |
Patrick McHardy | a18aa31 | 2007-12-12 10:35:16 -0800 | [diff] [blame] | 604 | put_user(msize, &cm->u.user.match_size) || |
| 605 | copy_to_user(cm->u.user.name, m->u.kernel.match->name, |
| 606 | strlen(m->u.kernel.match->name) + 1)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 607 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 608 | |
| 609 | if (match->compat_to_user) { |
| 610 | if (match->compat_to_user((void __user *)cm->data, m->data)) |
| 611 | return -EFAULT; |
| 612 | } else { |
| 613 | if (copy_to_user(cm->data, m->data, msize - sizeof(*cm))) |
| 614 | return -EFAULT; |
| 615 | } |
| 616 | |
| 617 | *size -= off; |
| 618 | *dstptr += msize; |
| 619 | return 0; |
| 620 | } |
| 621 | EXPORT_SYMBOL_GPL(xt_compat_match_to_user); |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 622 | |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 623 | /* non-compat version may have padding after verdict */ |
| 624 | struct compat_xt_standard_target { |
| 625 | struct compat_xt_entry_target t; |
| 626 | compat_uint_t verdict; |
| 627 | }; |
| 628 | |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 629 | int xt_compat_check_entry_offsets(const void *base, const char *elems, |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 630 | unsigned int target_offset, |
| 631 | unsigned int next_offset) |
| 632 | { |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 633 | long size_of_base_struct = elems - (const char *)base; |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 634 | const struct compat_xt_entry_target *t; |
| 635 | const char *e = base; |
| 636 | |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 637 | if (target_offset < size_of_base_struct) |
| 638 | return -EINVAL; |
| 639 | |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 640 | if (target_offset + sizeof(*t) > next_offset) |
| 641 | return -EINVAL; |
| 642 | |
| 643 | t = (void *)(e + target_offset); |
| 644 | if (t->u.target_size < sizeof(*t)) |
| 645 | return -EINVAL; |
| 646 | |
| 647 | if (target_offset + t->u.target_size > next_offset) |
| 648 | return -EINVAL; |
| 649 | |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 650 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 && |
Florian Westphal | 7b7eba0 | 2016-06-01 02:04:44 +0200 | [diff] [blame] | 651 | COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset) |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 652 | return -EINVAL; |
| 653 | |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 654 | /* compat_xt_entry match has less strict aligment requirements, |
| 655 | * otherwise they are identical. In case of padding differences |
| 656 | * we need to add compat version of xt_check_entry_match. |
| 657 | */ |
| 658 | BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match)); |
| 659 | |
| 660 | return xt_check_entry_match(elems, base + target_offset, |
| 661 | __alignof__(struct compat_xt_entry_match)); |
Florian Westphal | fc1221b | 2016-04-01 14:17:26 +0200 | [diff] [blame] | 662 | } |
| 663 | EXPORT_SYMBOL(xt_compat_check_entry_offsets); |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 664 | #endif /* CONFIG_COMPAT */ |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 665 | |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 666 | /** |
| 667 | * xt_check_entry_offsets - validate arp/ip/ip6t_entry |
| 668 | * |
| 669 | * @base: pointer to arp/ip/ip6t_entry |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 670 | * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 671 | * @target_offset: the arp/ip/ip6_t->target_offset |
| 672 | * @next_offset: the arp/ip/ip6_t->next_offset |
| 673 | * |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 674 | * validates that target_offset and next_offset are sane and that all |
| 675 | * match sizes (if any) align with the target offset. |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 676 | * |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 677 | * This function does not validate the targets or matches themselves, it |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 678 | * only tests that all the offsets and sizes are correct, that all |
| 679 | * match structures are aligned, and that the last structure ends where |
| 680 | * the target structure begins. |
| 681 | * |
| 682 | * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version. |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 683 | * |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 684 | * The arp/ip/ip6t_entry structure @base must have passed following tests: |
| 685 | * - it must point to a valid memory location |
| 686 | * - base to base + next_offset must be accessible, i.e. not exceed allocated |
| 687 | * length. |
| 688 | * |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 689 | * A well-formed entry looks like this: |
| 690 | * |
| 691 | * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry |
| 692 | * e->elems[]-----' | | |
| 693 | * matchsize | | |
| 694 | * matchsize | | |
| 695 | * | | |
| 696 | * target_offset---------------------------------' | |
| 697 | * next_offset---------------------------------------------------' |
| 698 | * |
| 699 | * elems[]: flexible array member at end of ip(6)/arpt_entry struct. |
| 700 | * This is where matches (if any) and the target reside. |
| 701 | * target_offset: beginning of target. |
| 702 | * next_offset: start of the next rule; also: size of this rule. |
| 703 | * Since targets have a minimum size, target_offset + minlen <= next_offset. |
| 704 | * |
| 705 | * Every match stores its size, sum of sizes must not exceed target_offset. |
| 706 | * |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 707 | * Return: 0 on success, negative errno on failure. |
| 708 | */ |
| 709 | int xt_check_entry_offsets(const void *base, |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 710 | const char *elems, |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 711 | unsigned int target_offset, |
| 712 | unsigned int next_offset) |
| 713 | { |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 714 | long size_of_base_struct = elems - (const char *)base; |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 715 | const struct xt_entry_target *t; |
| 716 | const char *e = base; |
| 717 | |
Florian Westphal | ce683e5 | 2016-04-01 14:17:28 +0200 | [diff] [blame] | 718 | /* target start is within the ip/ip6/arpt_entry struct */ |
| 719 | if (target_offset < size_of_base_struct) |
| 720 | return -EINVAL; |
| 721 | |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 722 | if (target_offset + sizeof(*t) > next_offset) |
| 723 | return -EINVAL; |
| 724 | |
| 725 | t = (void *)(e + target_offset); |
Florian Westphal | a08e4e1 | 2016-04-01 14:17:25 +0200 | [diff] [blame] | 726 | if (t->u.target_size < sizeof(*t)) |
| 727 | return -EINVAL; |
| 728 | |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 729 | if (target_offset + t->u.target_size > next_offset) |
| 730 | return -EINVAL; |
| 731 | |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 732 | if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 && |
Florian Westphal | 7b7eba0 | 2016-06-01 02:04:44 +0200 | [diff] [blame] | 733 | XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset) |
Florian Westphal | 7ed2abd | 2016-04-01 14:17:27 +0200 | [diff] [blame] | 734 | return -EINVAL; |
| 735 | |
Florian Westphal | 13631bf | 2016-04-01 14:17:29 +0200 | [diff] [blame] | 736 | return xt_check_entry_match(elems, base + target_offset, |
| 737 | __alignof__(struct xt_entry_match)); |
Florian Westphal | 7d35812 | 2016-04-01 14:17:23 +0200 | [diff] [blame] | 738 | } |
| 739 | EXPORT_SYMBOL(xt_check_entry_offsets); |
| 740 | |
Florian Westphal | f4dc777 | 2016-07-14 17:51:26 +0200 | [diff] [blame] | 741 | /** |
| 742 | * xt_alloc_entry_offsets - allocate array to store rule head offsets |
| 743 | * |
| 744 | * @size: number of entries |
| 745 | * |
| 746 | * Return: NULL or kmalloc'd or vmalloc'd array |
| 747 | */ |
| 748 | unsigned int *xt_alloc_entry_offsets(unsigned int size) |
| 749 | { |
| 750 | unsigned int *off; |
| 751 | |
| 752 | off = kcalloc(size, sizeof(unsigned int), GFP_KERNEL | __GFP_NOWARN); |
| 753 | |
| 754 | if (off) |
| 755 | return off; |
| 756 | |
| 757 | if (size < (SIZE_MAX / sizeof(unsigned int))) |
| 758 | off = vmalloc(size * sizeof(unsigned int)); |
| 759 | |
| 760 | return off; |
| 761 | } |
| 762 | EXPORT_SYMBOL(xt_alloc_entry_offsets); |
| 763 | |
| 764 | /** |
| 765 | * xt_find_jump_offset - check if target is a valid jump offset |
| 766 | * |
| 767 | * @offsets: array containing all valid rule start offsets of a rule blob |
| 768 | * @target: the jump target to search for |
| 769 | * @size: entries in @offset |
| 770 | */ |
| 771 | bool xt_find_jump_offset(const unsigned int *offsets, |
| 772 | unsigned int target, unsigned int size) |
| 773 | { |
| 774 | int m, low = 0, hi = size; |
| 775 | |
| 776 | while (hi > low) { |
| 777 | m = (low + hi) / 2u; |
| 778 | |
| 779 | if (offsets[m] > target) |
| 780 | hi = m; |
| 781 | else if (offsets[m] < target) |
| 782 | low = m + 1; |
| 783 | else |
| 784 | return true; |
| 785 | } |
| 786 | |
| 787 | return false; |
| 788 | } |
| 789 | EXPORT_SYMBOL(xt_find_jump_offset); |
| 790 | |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 791 | int xt_check_target(struct xt_tgchk_param *par, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 792 | unsigned int size, u_int8_t proto, bool inv_proto) |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 793 | { |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 794 | int ret; |
| 795 | |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 796 | if (XT_ALIGN(par->target->targetsize) != size) { |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 797 | pr_err("%s_tables: %s.%u target: invalid size " |
| 798 | "%u (kernel) != (user) %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 799 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | b402405 | 2009-06-25 18:32:12 +0200 | [diff] [blame] | 800 | par->target->revision, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 801 | XT_ALIGN(par->target->targetsize), size); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 802 | return -EINVAL; |
| 803 | } |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 804 | if (par->target->table != NULL && |
| 805 | strcmp(par->target->table, par->table) != 0) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 806 | 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] | 807 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 808 | par->target->table, par->table); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 809 | return -EINVAL; |
| 810 | } |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 811 | if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) { |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 812 | char used[64], allow[64]; |
| 813 | |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 814 | pr_err("%s_tables: %s target: used from hooks %s, but only " |
Jan Engelhardt | 4518536 | 2009-04-05 10:43:09 +0200 | [diff] [blame] | 815 | "usable from %s\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 816 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | 5b76c49 | 2013-01-10 12:30:05 +0000 | [diff] [blame] | 817 | textify_hooks(used, sizeof(used), par->hook_mask, |
| 818 | par->family), |
| 819 | textify_hooks(allow, sizeof(allow), par->target->hooks, |
| 820 | par->family)); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 821 | return -EINVAL; |
| 822 | } |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 823 | if (par->target->proto && (par->target->proto != proto || inv_proto)) { |
Joe Perches | 3dd5d7e | 2009-06-13 12:32:39 +0200 | [diff] [blame] | 824 | pr_err("%s_tables: %s target: only valid for protocol %u\n", |
Jan Engelhardt | 916a917 | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 825 | xt_prefix[par->family], par->target->name, |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 826 | par->target->proto); |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 827 | return -EINVAL; |
| 828 | } |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 829 | if (par->target->checkentry != NULL) { |
| 830 | ret = par->target->checkentry(par); |
| 831 | if (ret < 0) |
| 832 | return ret; |
| 833 | else if (ret > 0) |
| 834 | /* Flag up potential errors. */ |
| 835 | return -EIO; |
| 836 | } |
Patrick McHardy | 37f9f73 | 2006-03-20 17:59:06 -0800 | [diff] [blame] | 837 | return 0; |
| 838 | } |
| 839 | EXPORT_SYMBOL_GPL(xt_check_target); |
| 840 | |
Florian Westphal | d7591f0 | 2016-04-01 15:37:59 +0200 | [diff] [blame] | 841 | /** |
| 842 | * xt_copy_counters_from_user - copy counters and metadata from userspace |
| 843 | * |
| 844 | * @user: src pointer to userspace memory |
| 845 | * @len: alleged size of userspace memory |
| 846 | * @info: where to store the xt_counters_info metadata |
| 847 | * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel |
| 848 | * |
| 849 | * Copies counter meta data from @user and stores it in @info. |
| 850 | * |
| 851 | * vmallocs memory to hold the counters, then copies the counter data |
| 852 | * from @user to the new memory and returns a pointer to it. |
| 853 | * |
| 854 | * If @compat is true, @info gets converted automatically to the 64bit |
| 855 | * representation. |
| 856 | * |
| 857 | * The metadata associated with the counters is stored in @info. |
| 858 | * |
| 859 | * Return: returns pointer that caller has to test via IS_ERR(). |
| 860 | * If IS_ERR is false, caller has to vfree the pointer. |
| 861 | */ |
| 862 | void *xt_copy_counters_from_user(const void __user *user, unsigned int len, |
| 863 | struct xt_counters_info *info, bool compat) |
| 864 | { |
| 865 | void *mem; |
| 866 | u64 size; |
| 867 | |
| 868 | #ifdef CONFIG_COMPAT |
| 869 | if (compat) { |
| 870 | /* structures only differ in size due to alignment */ |
| 871 | struct compat_xt_counters_info compat_tmp; |
| 872 | |
| 873 | if (len <= sizeof(compat_tmp)) |
| 874 | return ERR_PTR(-EINVAL); |
| 875 | |
| 876 | len -= sizeof(compat_tmp); |
| 877 | if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0) |
| 878 | return ERR_PTR(-EFAULT); |
| 879 | |
Eric Dumazet | 3ea051b | 2017-10-05 02:50:07 -0700 | [diff] [blame] | 880 | memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1); |
Florian Westphal | d7591f0 | 2016-04-01 15:37:59 +0200 | [diff] [blame] | 881 | info->num_counters = compat_tmp.num_counters; |
| 882 | user += sizeof(compat_tmp); |
| 883 | } else |
| 884 | #endif |
| 885 | { |
| 886 | if (len <= sizeof(*info)) |
| 887 | return ERR_PTR(-EINVAL); |
| 888 | |
| 889 | len -= sizeof(*info); |
| 890 | if (copy_from_user(info, user, sizeof(*info)) != 0) |
| 891 | return ERR_PTR(-EFAULT); |
| 892 | |
Florian Westphal | d7591f0 | 2016-04-01 15:37:59 +0200 | [diff] [blame] | 893 | user += sizeof(*info); |
| 894 | } |
Eric Dumazet | 3ea051b | 2017-10-05 02:50:07 -0700 | [diff] [blame] | 895 | info->name[sizeof(info->name) - 1] = '\0'; |
Florian Westphal | d7591f0 | 2016-04-01 15:37:59 +0200 | [diff] [blame] | 896 | |
| 897 | size = sizeof(struct xt_counters); |
| 898 | size *= info->num_counters; |
| 899 | |
| 900 | if (size != (u64)len) |
| 901 | return ERR_PTR(-EINVAL); |
| 902 | |
| 903 | mem = vmalloc(len); |
| 904 | if (!mem) |
| 905 | return ERR_PTR(-ENOMEM); |
| 906 | |
| 907 | if (copy_from_user(mem, user, len) == 0) |
| 908 | return mem; |
| 909 | |
| 910 | vfree(mem); |
| 911 | return ERR_PTR(-EFAULT); |
| 912 | } |
| 913 | EXPORT_SYMBOL_GPL(xt_copy_counters_from_user); |
| 914 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 915 | #ifdef CONFIG_COMPAT |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 916 | int xt_compat_target_offset(const struct xt_target *target) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 917 | { |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 918 | u_int16_t csize = target->compatsize ? : target->targetsize; |
| 919 | return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 920 | } |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 921 | EXPORT_SYMBOL_GPL(xt_compat_target_offset); |
| 922 | |
| 923 | 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] | 924 | unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 925 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 926 | const struct xt_target *target = t->u.kernel.target; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 927 | struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t; |
| 928 | int pad, off = xt_compat_target_offset(target); |
| 929 | u_int16_t tsize = ct->u.user.target_size; |
Florian Westphal | 09d9686 | 2016-04-01 14:17:34 +0200 | [diff] [blame] | 930 | char name[sizeof(t->u.user.name)]; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 931 | |
| 932 | t = *dstptr; |
| 933 | memcpy(t, ct, sizeof(*ct)); |
| 934 | if (target->compat_from_user) |
| 935 | target->compat_from_user(t->data, ct->data); |
| 936 | else |
| 937 | memcpy(t->data, ct->data, tsize - sizeof(*ct)); |
| 938 | pad = XT_ALIGN(target->targetsize) - target->targetsize; |
| 939 | if (pad > 0) |
| 940 | memset(t->data + target->targetsize, 0, pad); |
| 941 | |
| 942 | tsize += off; |
| 943 | t->u.user.target_size = tsize; |
Florian Westphal | 09d9686 | 2016-04-01 14:17:34 +0200 | [diff] [blame] | 944 | strlcpy(name, target->name, sizeof(name)); |
| 945 | module_put(target->me); |
| 946 | strncpy(t->u.user.name, name, sizeof(t->u.user.name)); |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 947 | |
| 948 | *size += off; |
| 949 | *dstptr += tsize; |
| 950 | } |
| 951 | EXPORT_SYMBOL_GPL(xt_compat_target_from_user); |
| 952 | |
Jan Engelhardt | 739674f | 2009-06-26 08:23:19 +0200 | [diff] [blame] | 953 | int xt_compat_target_to_user(const struct xt_entry_target *t, |
| 954 | void __user **dstptr, unsigned int *size) |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 955 | { |
Jan Engelhardt | 5452e42 | 2008-04-14 11:15:35 +0200 | [diff] [blame] | 956 | const struct xt_target *target = t->u.kernel.target; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 957 | struct compat_xt_entry_target __user *ct = *dstptr; |
| 958 | int off = xt_compat_target_offset(target); |
| 959 | u_int16_t tsize = t->u.user.target_size - off; |
| 960 | |
| 961 | if (copy_to_user(ct, t, sizeof(*ct)) || |
Patrick McHardy | a18aa31 | 2007-12-12 10:35:16 -0800 | [diff] [blame] | 962 | put_user(tsize, &ct->u.user.target_size) || |
| 963 | copy_to_user(ct->u.user.name, t->u.kernel.target->name, |
| 964 | strlen(t->u.kernel.target->name) + 1)) |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 965 | return -EFAULT; |
Patrick McHardy | 9fa492c | 2006-09-20 12:05:37 -0700 | [diff] [blame] | 966 | |
| 967 | if (target->compat_to_user) { |
| 968 | if (target->compat_to_user((void __user *)ct->data, t->data)) |
| 969 | return -EFAULT; |
| 970 | } else { |
| 971 | if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct))) |
| 972 | return -EFAULT; |
| 973 | } |
| 974 | |
| 975 | *size -= off; |
| 976 | *dstptr += tsize; |
| 977 | return 0; |
| 978 | } |
| 979 | EXPORT_SYMBOL_GPL(xt_compat_target_to_user); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 980 | #endif |
| 981 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 982 | struct xt_table_info *xt_alloc_table_info(unsigned int size) |
| 983 | { |
Eric Dumazet | 711bdde | 2015-06-15 09:57:30 -0700 | [diff] [blame] | 984 | struct xt_table_info *info = NULL; |
| 985 | size_t sz = sizeof(*info) + size; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 986 | |
Florian Westphal | d157bd7 | 2016-03-10 01:56:23 +0100 | [diff] [blame] | 987 | if (sz < sizeof(*info)) |
| 988 | return NULL; |
| 989 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 990 | /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */ |
Dmitry Vyukov | 1099c70 | 2017-12-28 09:48:54 +0100 | [diff] [blame] | 991 | if ((size >> PAGE_SHIFT) + 2 > totalram_pages) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 992 | return NULL; |
| 993 | |
Eric Dumazet | 711bdde | 2015-06-15 09:57:30 -0700 | [diff] [blame] | 994 | if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) |
| 995 | info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
| 996 | if (!info) { |
| 997 | info = vmalloc(sz); |
| 998 | if (!info) |
| 999 | return NULL; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1000 | } |
Eric Dumazet | 711bdde | 2015-06-15 09:57:30 -0700 | [diff] [blame] | 1001 | memset(info, 0, sizeof(*info)); |
| 1002 | info->size = size; |
| 1003 | return info; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1004 | } |
| 1005 | EXPORT_SYMBOL(xt_alloc_table_info); |
| 1006 | |
| 1007 | void xt_free_table_info(struct xt_table_info *info) |
| 1008 | { |
| 1009 | int cpu; |
| 1010 | |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1011 | if (info->jumpstack != NULL) { |
Eric Dumazet | f6b5082 | 2014-06-24 02:15:35 -0700 | [diff] [blame] | 1012 | for_each_possible_cpu(cpu) |
| 1013 | kvfree(info->jumpstack[cpu]); |
| 1014 | kvfree(info->jumpstack); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1015 | } |
| 1016 | |
Eric Dumazet | 711bdde | 2015-06-15 09:57:30 -0700 | [diff] [blame] | 1017 | kvfree(info); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1018 | } |
| 1019 | EXPORT_SYMBOL(xt_free_table_info); |
| 1020 | |
| 1021 | /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */ |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1022 | struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af, |
| 1023 | const char *name) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1024 | { |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1025 | struct xt_table *t, *found = NULL; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1026 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 1027 | mutex_lock(&xt[af].mutex); |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1028 | list_for_each_entry(t, &net->xt.tables[af], list) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1029 | if (strcmp(t->name, name) == 0 && try_module_get(t->me)) |
| 1030 | return t; |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1031 | |
| 1032 | if (net == &init_net) |
| 1033 | goto out; |
| 1034 | |
| 1035 | /* Table doesn't exist in this netns, re-try init */ |
| 1036 | list_for_each_entry(t, &init_net.xt.tables[af], list) { |
| 1037 | if (strcmp(t->name, name)) |
| 1038 | continue; |
Dan Carpenter | c70c7be | 2017-04-28 15:57:56 +0300 | [diff] [blame] | 1039 | if (!try_module_get(t->me)) { |
| 1040 | mutex_unlock(&xt[af].mutex); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1041 | return NULL; |
Dan Carpenter | c70c7be | 2017-04-28 15:57:56 +0300 | [diff] [blame] | 1042 | } |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1043 | |
| 1044 | mutex_unlock(&xt[af].mutex); |
| 1045 | if (t->table_init(net) != 0) { |
| 1046 | module_put(t->me); |
| 1047 | return NULL; |
| 1048 | } |
| 1049 | |
| 1050 | found = t; |
| 1051 | |
| 1052 | mutex_lock(&xt[af].mutex); |
| 1053 | break; |
| 1054 | } |
| 1055 | |
| 1056 | if (!found) |
| 1057 | goto out; |
| 1058 | |
| 1059 | /* and once again: */ |
| 1060 | list_for_each_entry(t, &net->xt.tables[af], list) |
| 1061 | if (strcmp(t->name, name) == 0) |
| 1062 | return t; |
| 1063 | |
| 1064 | module_put(found->me); |
| 1065 | out: |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1066 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1067 | return NULL; |
| 1068 | } |
| 1069 | EXPORT_SYMBOL_GPL(xt_find_table_lock); |
| 1070 | |
| 1071 | void xt_table_unlock(struct xt_table *table) |
| 1072 | { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1073 | mutex_unlock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1074 | } |
| 1075 | EXPORT_SYMBOL_GPL(xt_table_unlock); |
| 1076 | |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1077 | #ifdef CONFIG_COMPAT |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1078 | void xt_compat_lock(u_int8_t af) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1079 | { |
| 1080 | mutex_lock(&xt[af].compat_mutex); |
| 1081 | } |
| 1082 | EXPORT_SYMBOL_GPL(xt_compat_lock); |
| 1083 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1084 | void xt_compat_unlock(u_int8_t af) |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1085 | { |
| 1086 | mutex_unlock(&xt[af].compat_mutex); |
| 1087 | } |
| 1088 | EXPORT_SYMBOL_GPL(xt_compat_unlock); |
| 1089 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1090 | |
Eric Dumazet | 7f5c6d4 | 2011-04-04 17:04:03 +0200 | [diff] [blame] | 1091 | DEFINE_PER_CPU(seqcount_t, xt_recseq); |
| 1092 | EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq); |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1093 | |
Florian Westphal | dcebd31 | 2015-07-14 17:51:09 +0200 | [diff] [blame] | 1094 | struct static_key xt_tee_enabled __read_mostly; |
| 1095 | EXPORT_SYMBOL_GPL(xt_tee_enabled); |
| 1096 | |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1097 | static int xt_jumpstack_alloc(struct xt_table_info *i) |
| 1098 | { |
| 1099 | unsigned int size; |
| 1100 | int cpu; |
| 1101 | |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1102 | size = sizeof(void **) * nr_cpu_ids; |
| 1103 | if (size > PAGE_SIZE) |
Joe Perches | 3dbd443 | 2011-05-28 10:36:35 -0700 | [diff] [blame] | 1104 | i->jumpstack = vzalloc(size); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1105 | else |
Joe Perches | 3dbd443 | 2011-05-28 10:36:35 -0700 | [diff] [blame] | 1106 | i->jumpstack = kzalloc(size, GFP_KERNEL); |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1107 | if (i->jumpstack == NULL) |
| 1108 | return -ENOMEM; |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1109 | |
Florian Westphal | 98d1bd8 | 2015-07-14 17:51:06 +0200 | [diff] [blame] | 1110 | /* ruleset without jumps -- no stack needed */ |
| 1111 | if (i->stacksize == 0) |
| 1112 | return 0; |
| 1113 | |
Florian Westphal | 7814b6e | 2015-07-14 17:51:08 +0200 | [diff] [blame] | 1114 | /* Jumpstack needs to be able to record two full callchains, one |
| 1115 | * from the first rule set traversal, plus one table reentrancy |
| 1116 | * via -j TEE without clobbering the callchain that brought us to |
| 1117 | * TEE target. |
| 1118 | * |
| 1119 | * This is done by allocating two jumpstacks per cpu, on reentry |
| 1120 | * the upper half of the stack is used. |
| 1121 | * |
| 1122 | * see the jumpstack setup in ipt_do_table() for more details. |
| 1123 | */ |
| 1124 | size = sizeof(void *) * i->stacksize * 2u; |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1125 | for_each_possible_cpu(cpu) { |
| 1126 | if (size > PAGE_SIZE) |
| 1127 | i->jumpstack[cpu] = vmalloc_node(size, |
| 1128 | cpu_to_node(cpu)); |
| 1129 | else |
| 1130 | i->jumpstack[cpu] = kmalloc_node(size, |
| 1131 | GFP_KERNEL, cpu_to_node(cpu)); |
| 1132 | if (i->jumpstack[cpu] == NULL) |
| 1133 | /* |
| 1134 | * Freeing will be done later on by the callers. The |
| 1135 | * chain is: xt_replace_table -> __do_replace -> |
| 1136 | * do_replace -> xt_free_table_info. |
| 1137 | */ |
| 1138 | return -ENOMEM; |
| 1139 | } |
| 1140 | |
| 1141 | return 0; |
| 1142 | } |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1143 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1144 | struct xt_table_info * |
| 1145 | xt_replace_table(struct xt_table *table, |
| 1146 | unsigned int num_counters, |
| 1147 | struct xt_table_info *newinfo, |
| 1148 | int *error) |
| 1149 | { |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1150 | struct xt_table_info *private; |
Jan Engelhardt | f3c5c1b | 2010-04-19 16:05:10 +0200 | [diff] [blame] | 1151 | int ret; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1152 | |
Jan Engelhardt | d97a9e4 | 2010-04-21 14:45:51 +0200 | [diff] [blame] | 1153 | ret = xt_jumpstack_alloc(newinfo); |
| 1154 | if (ret < 0) { |
| 1155 | *error = ret; |
| 1156 | return NULL; |
| 1157 | } |
| 1158 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1159 | /* Do the substitution. */ |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1160 | local_bh_disable(); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1161 | private = table->private; |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1162 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1163 | /* Check inside lock: is the old number correct? */ |
| 1164 | if (num_counters != private->number) { |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 1165 | pr_debug("num_counters != table->private->number (%u/%u)\n", |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1166 | num_counters, private->number); |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1167 | local_bh_enable(); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1168 | *error = -EAGAIN; |
| 1169 | return NULL; |
| 1170 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1171 | |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1172 | newinfo->initial_entries = private->initial_entries; |
Will Deacon | b416c14 | 2013-10-21 13:14:53 +0100 | [diff] [blame] | 1173 | /* |
| 1174 | * Ensure contents of newinfo are visible before assigning to |
| 1175 | * private. |
| 1176 | */ |
| 1177 | smp_wmb(); |
| 1178 | table->private = newinfo; |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1179 | |
| 1180 | /* |
| 1181 | * Even though table entries have now been swapped, other CPU's |
| 1182 | * may still be using the old entries. This is okay, because |
| 1183 | * resynchronization happens because of the locking done |
| 1184 | * during the get_counters() routine. |
| 1185 | */ |
| 1186 | local_bh_enable(); |
| 1187 | |
Thomas Graf | fbabf31 | 2011-01-16 18:12:59 +0100 | [diff] [blame] | 1188 | #ifdef CONFIG_AUDIT |
| 1189 | if (audit_enabled) { |
| 1190 | struct audit_buffer *ab; |
| 1191 | |
| 1192 | ab = audit_log_start(current->audit_context, GFP_KERNEL, |
| 1193 | AUDIT_NETFILTER_CFG); |
| 1194 | if (ab) { |
| 1195 | audit_log_format(ab, "table=%s family=%u entries=%u", |
| 1196 | table->name, table->af, |
| 1197 | private->number); |
| 1198 | audit_log_end(ab); |
| 1199 | } |
| 1200 | } |
| 1201 | #endif |
| 1202 | |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1203 | return private; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1204 | } |
| 1205 | EXPORT_SYMBOL_GPL(xt_replace_table); |
| 1206 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1207 | struct xt_table *xt_register_table(struct net *net, |
| 1208 | const struct xt_table *input_table, |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 1209 | struct xt_table_info *bootstrap, |
| 1210 | struct xt_table_info *newinfo) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1211 | { |
| 1212 | int ret; |
| 1213 | struct xt_table_info *private; |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1214 | struct xt_table *t, *table; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1215 | |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 1216 | /* Don't add one object to multiple lists. */ |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 1217 | table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 1218 | if (!table) { |
| 1219 | ret = -ENOMEM; |
| 1220 | goto out; |
| 1221 | } |
| 1222 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 1223 | mutex_lock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1224 | /* Don't autoload: we'd eat our tail... */ |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1225 | list_for_each_entry(t, &net->xt.tables[table->af], list) { |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1226 | if (strcmp(t->name, table->name) == 0) { |
| 1227 | ret = -EEXIST; |
| 1228 | goto unlock; |
| 1229 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* Simplifies replace_table code. */ |
| 1233 | table->private = bootstrap; |
Stephen Hemminger | 7845447 | 2009-02-20 10:35:32 +0100 | [diff] [blame] | 1234 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1235 | if (!xt_replace_table(table, 0, newinfo, &ret)) |
| 1236 | goto unlock; |
| 1237 | |
| 1238 | private = table->private; |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 1239 | pr_debug("table->private->number = %u\n", private->number); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1240 | |
| 1241 | /* save number of initial entries */ |
| 1242 | private->initial_entries = private->number; |
| 1243 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1244 | list_add(&table->list, &net->xt.tables[table->af]); |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 1245 | mutex_unlock(&xt[table->af].mutex); |
| 1246 | return table; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1247 | |
Pablo Neira Ayuso | 7926dbf | 2014-07-31 20:38:46 +0200 | [diff] [blame] | 1248 | unlock: |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1249 | mutex_unlock(&xt[table->af].mutex); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 1250 | kfree(table); |
Alexey Dobriyan | a98da11 | 2008-01-31 04:01:49 -0800 | [diff] [blame] | 1251 | out: |
| 1252 | return ERR_PTR(ret); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1253 | } |
| 1254 | EXPORT_SYMBOL_GPL(xt_register_table); |
| 1255 | |
| 1256 | void *xt_unregister_table(struct xt_table *table) |
| 1257 | { |
| 1258 | struct xt_table_info *private; |
| 1259 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1260 | mutex_lock(&xt[table->af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1261 | private = table->private; |
Patrick McHardy | df0933d | 2006-09-20 11:57:53 -0700 | [diff] [blame] | 1262 | list_del(&table->list); |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1263 | mutex_unlock(&xt[table->af].mutex); |
Alexey Dobriyan | 44d34e7 | 2008-01-31 04:02:44 -0800 | [diff] [blame] | 1264 | kfree(table); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1265 | |
| 1266 | return private; |
| 1267 | } |
| 1268 | EXPORT_SYMBOL_GPL(xt_unregister_table); |
| 1269 | |
| 1270 | #ifdef CONFIG_PROC_FS |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1271 | struct xt_names_priv { |
| 1272 | struct seq_net_private p; |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1273 | u_int8_t af; |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1274 | }; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1275 | 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] | 1276 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1277 | struct xt_names_priv *priv = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 1278 | struct net *net = seq_file_net(seq); |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1279 | u_int8_t af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1280 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1281 | mutex_lock(&xt[af].mutex); |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1282 | return seq_list_start(&net->xt.tables[af], *pos); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1285 | 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] | 1286 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1287 | struct xt_names_priv *priv = seq->private; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 1288 | struct net *net = seq_file_net(seq); |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1289 | u_int8_t af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1290 | |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1291 | return seq_list_next(v, &net->xt.tables[af], pos); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1294 | static void xt_table_seq_stop(struct seq_file *seq, void *v) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1295 | { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1296 | struct xt_names_priv *priv = seq->private; |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1297 | u_int8_t af = priv->af; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1298 | |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1299 | mutex_unlock(&xt[af].mutex); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1302 | static int xt_table_seq_show(struct seq_file *seq, void *v) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1303 | { |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1304 | struct xt_table *table = list_entry(v, struct xt_table, list); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1305 | |
Joe Perches | 861fb10 | 2015-05-12 18:28:23 -0700 | [diff] [blame] | 1306 | if (*table->name) |
Steven Rostedt (Red Hat) | e71456a | 2014-10-27 17:43:45 -0400 | [diff] [blame] | 1307 | seq_printf(seq, "%s\n", table->name); |
Joe Perches | 861fb10 | 2015-05-12 18:28:23 -0700 | [diff] [blame] | 1308 | return 0; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1311 | static const struct seq_operations xt_table_seq_ops = { |
| 1312 | .start = xt_table_seq_start, |
| 1313 | .next = xt_table_seq_next, |
| 1314 | .stop = xt_table_seq_stop, |
| 1315 | .show = xt_table_seq_show, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1316 | }; |
| 1317 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1318 | static int xt_table_open(struct inode *inode, struct file *file) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1319 | { |
| 1320 | int ret; |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1321 | struct xt_names_priv *priv; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1322 | |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1323 | ret = seq_open_net(inode, file, &xt_table_seq_ops, |
| 1324 | sizeof(struct xt_names_priv)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1325 | if (!ret) { |
Alexey Dobriyan | 715cf35 | 2008-01-31 04:49:16 -0800 | [diff] [blame] | 1326 | priv = ((struct seq_file *)file->private_data)->private; |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 1327 | priv->af = (unsigned long)PDE_DATA(inode); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1328 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1329 | return ret; |
| 1330 | } |
| 1331 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1332 | static const struct file_operations xt_table_ops = { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1333 | .owner = THIS_MODULE, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1334 | .open = xt_table_open, |
| 1335 | .read = seq_read, |
| 1336 | .llseek = seq_lseek, |
Pavel Emelyanov | 0e93bb9 | 2008-04-29 03:15:35 -0700 | [diff] [blame] | 1337 | .release = seq_release_net, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1338 | }; |
| 1339 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1340 | /* |
| 1341 | * Traverse state for ip{,6}_{tables,matches} for helping crossing |
| 1342 | * the multi-AF mutexes. |
| 1343 | */ |
| 1344 | struct nf_mttg_trav { |
| 1345 | struct list_head *head, *curr; |
| 1346 | uint8_t class, nfproto; |
| 1347 | }; |
| 1348 | |
| 1349 | enum { |
| 1350 | MTTG_TRAV_INIT, |
| 1351 | MTTG_TRAV_NFP_UNSPEC, |
| 1352 | MTTG_TRAV_NFP_SPEC, |
| 1353 | MTTG_TRAV_DONE, |
| 1354 | }; |
| 1355 | |
| 1356 | static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos, |
| 1357 | bool is_target) |
| 1358 | { |
| 1359 | static const uint8_t next_class[] = { |
| 1360 | [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC, |
| 1361 | [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE, |
| 1362 | }; |
| 1363 | struct nf_mttg_trav *trav = seq->private; |
| 1364 | |
| 1365 | switch (trav->class) { |
| 1366 | case MTTG_TRAV_INIT: |
| 1367 | trav->class = MTTG_TRAV_NFP_UNSPEC; |
| 1368 | mutex_lock(&xt[NFPROTO_UNSPEC].mutex); |
| 1369 | trav->head = trav->curr = is_target ? |
| 1370 | &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match; |
| 1371 | break; |
| 1372 | case MTTG_TRAV_NFP_UNSPEC: |
| 1373 | trav->curr = trav->curr->next; |
| 1374 | if (trav->curr != trav->head) |
| 1375 | break; |
| 1376 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); |
| 1377 | mutex_lock(&xt[trav->nfproto].mutex); |
| 1378 | trav->head = trav->curr = is_target ? |
| 1379 | &xt[trav->nfproto].target : &xt[trav->nfproto].match; |
| 1380 | trav->class = next_class[trav->class]; |
| 1381 | break; |
| 1382 | case MTTG_TRAV_NFP_SPEC: |
| 1383 | trav->curr = trav->curr->next; |
| 1384 | if (trav->curr != trav->head) |
| 1385 | break; |
| 1386 | /* fallthru, _stop will unlock */ |
| 1387 | default: |
| 1388 | return NULL; |
| 1389 | } |
| 1390 | |
| 1391 | if (ppos != NULL) |
| 1392 | ++*ppos; |
| 1393 | return trav; |
| 1394 | } |
| 1395 | |
| 1396 | static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos, |
| 1397 | bool is_target) |
| 1398 | { |
| 1399 | struct nf_mttg_trav *trav = seq->private; |
| 1400 | unsigned int j; |
| 1401 | |
| 1402 | trav->class = MTTG_TRAV_INIT; |
| 1403 | for (j = 0; j < *pos; ++j) |
| 1404 | if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL) |
| 1405 | return NULL; |
| 1406 | return trav; |
| 1407 | } |
| 1408 | |
| 1409 | static void xt_mttg_seq_stop(struct seq_file *seq, void *v) |
| 1410 | { |
| 1411 | struct nf_mttg_trav *trav = seq->private; |
| 1412 | |
| 1413 | switch (trav->class) { |
| 1414 | case MTTG_TRAV_NFP_UNSPEC: |
| 1415 | mutex_unlock(&xt[NFPROTO_UNSPEC].mutex); |
| 1416 | break; |
| 1417 | case MTTG_TRAV_NFP_SPEC: |
| 1418 | mutex_unlock(&xt[trav->nfproto].mutex); |
| 1419 | break; |
| 1420 | } |
| 1421 | } |
| 1422 | |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1423 | static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos) |
| 1424 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1425 | return xt_mttg_seq_start(seq, pos, false); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1426 | } |
| 1427 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1428 | 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] | 1429 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1430 | return xt_mttg_seq_next(seq, v, ppos, false); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | static int xt_match_seq_show(struct seq_file *seq, void *v) |
| 1434 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1435 | const struct nf_mttg_trav *trav = seq->private; |
| 1436 | const struct xt_match *match; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1437 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1438 | switch (trav->class) { |
| 1439 | case MTTG_TRAV_NFP_UNSPEC: |
| 1440 | case MTTG_TRAV_NFP_SPEC: |
| 1441 | if (trav->curr == trav->head) |
| 1442 | return 0; |
| 1443 | match = list_entry(trav->curr, struct xt_match, list); |
Joe Perches | 861fb10 | 2015-05-12 18:28:23 -0700 | [diff] [blame] | 1444 | if (*match->name) |
| 1445 | seq_printf(seq, "%s\n", match->name); |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1446 | } |
| 1447 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | static const struct seq_operations xt_match_seq_ops = { |
| 1451 | .start = xt_match_seq_start, |
| 1452 | .next = xt_match_seq_next, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1453 | .stop = xt_mttg_seq_stop, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1454 | .show = xt_match_seq_show, |
| 1455 | }; |
| 1456 | |
| 1457 | static int xt_match_open(struct inode *inode, struct file *file) |
| 1458 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1459 | struct nf_mttg_trav *trav; |
Rob Jones | 772476d | 2014-09-19 11:27:51 +0100 | [diff] [blame] | 1460 | trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav)); |
| 1461 | if (!trav) |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1462 | return -ENOMEM; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1463 | |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 1464 | trav->nfproto = (unsigned long)PDE_DATA(inode); |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1465 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | static const struct file_operations xt_match_ops = { |
| 1469 | .owner = THIS_MODULE, |
| 1470 | .open = xt_match_open, |
| 1471 | .read = seq_read, |
| 1472 | .llseek = seq_lseek, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1473 | .release = seq_release_private, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1474 | }; |
| 1475 | |
| 1476 | static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos) |
| 1477 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1478 | return xt_mttg_seq_start(seq, pos, true); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1481 | 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] | 1482 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1483 | return xt_mttg_seq_next(seq, v, ppos, true); |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | static int xt_target_seq_show(struct seq_file *seq, void *v) |
| 1487 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1488 | const struct nf_mttg_trav *trav = seq->private; |
| 1489 | const struct xt_target *target; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1490 | |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1491 | switch (trav->class) { |
| 1492 | case MTTG_TRAV_NFP_UNSPEC: |
| 1493 | case MTTG_TRAV_NFP_SPEC: |
| 1494 | if (trav->curr == trav->head) |
| 1495 | return 0; |
| 1496 | target = list_entry(trav->curr, struct xt_target, list); |
Joe Perches | 861fb10 | 2015-05-12 18:28:23 -0700 | [diff] [blame] | 1497 | if (*target->name) |
| 1498 | seq_printf(seq, "%s\n", target->name); |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1499 | } |
| 1500 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | static const struct seq_operations xt_target_seq_ops = { |
| 1504 | .start = xt_target_seq_start, |
| 1505 | .next = xt_target_seq_next, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1506 | .stop = xt_mttg_seq_stop, |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1507 | .show = xt_target_seq_show, |
| 1508 | }; |
| 1509 | |
| 1510 | static int xt_target_open(struct inode *inode, struct file *file) |
| 1511 | { |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1512 | struct nf_mttg_trav *trav; |
Rob Jones | 772476d | 2014-09-19 11:27:51 +0100 | [diff] [blame] | 1513 | trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav)); |
| 1514 | if (!trav) |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1515 | return -ENOMEM; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1516 | |
Al Viro | d9dda78 | 2013-03-31 18:16:14 -0400 | [diff] [blame] | 1517 | trav->nfproto = (unsigned long)PDE_DATA(inode); |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1518 | return 0; |
Alexey Dobriyan | 025d93d | 2008-01-31 04:48:54 -0800 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | static const struct file_operations xt_target_ops = { |
| 1522 | .owner = THIS_MODULE, |
| 1523 | .open = xt_target_open, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1524 | .read = seq_read, |
| 1525 | .llseek = seq_lseek, |
Jan Engelhardt | eb13220 | 2009-02-18 16:42:19 +0100 | [diff] [blame] | 1526 | .release = seq_release_private, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1527 | }; |
| 1528 | |
| 1529 | #define FORMAT_TABLES "_tables_names" |
| 1530 | #define FORMAT_MATCHES "_tables_matches" |
| 1531 | #define FORMAT_TARGETS "_tables_targets" |
| 1532 | |
| 1533 | #endif /* CONFIG_PROC_FS */ |
| 1534 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1535 | /** |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1536 | * xt_hook_ops_alloc - set up hooks for a new table |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1537 | * @table: table with metadata needed to set up hooks |
| 1538 | * @fn: Hook function |
| 1539 | * |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1540 | * This function will create the nf_hook_ops that the x_table needs |
| 1541 | * to hand to xt_hook_link_net(). |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1542 | */ |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1543 | struct nf_hook_ops * |
| 1544 | xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn) |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1545 | { |
| 1546 | unsigned int hook_mask = table->valid_hooks; |
| 1547 | uint8_t i, num_hooks = hweight32(hook_mask); |
| 1548 | uint8_t hooknum; |
| 1549 | struct nf_hook_ops *ops; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1550 | |
Xiubo Li | a6d0bae | 2016-06-02 10:59:56 +0800 | [diff] [blame] | 1551 | if (!num_hooks) |
| 1552 | return ERR_PTR(-EINVAL); |
| 1553 | |
Florian Westphal | 1ecc281 | 2016-10-17 21:50:23 +0200 | [diff] [blame] | 1554 | ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL); |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1555 | if (ops == NULL) |
| 1556 | return ERR_PTR(-ENOMEM); |
| 1557 | |
| 1558 | for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0; |
| 1559 | hook_mask >>= 1, ++hooknum) { |
| 1560 | if (!(hook_mask & 1)) |
| 1561 | continue; |
| 1562 | ops[i].hook = fn; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1563 | ops[i].pf = table->af; |
| 1564 | ops[i].hooknum = hooknum; |
| 1565 | ops[i].priority = table->priority; |
| 1566 | ++i; |
| 1567 | } |
| 1568 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1569 | return ops; |
| 1570 | } |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 1571 | EXPORT_SYMBOL_GPL(xt_hook_ops_alloc); |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 1572 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1573 | int xt_proto_init(struct net *net, u_int8_t af) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1574 | { |
| 1575 | #ifdef CONFIG_PROC_FS |
| 1576 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 1577 | struct proc_dir_entry *proc; |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1578 | kuid_t root_uid; |
| 1579 | kgid_t root_gid; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1580 | #endif |
| 1581 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1582 | if (af >= ARRAY_SIZE(xt_prefix)) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1583 | return -EINVAL; |
| 1584 | |
| 1585 | |
| 1586 | #ifdef CONFIG_PROC_FS |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1587 | root_uid = make_kuid(net->user_ns, 0); |
| 1588 | root_gid = make_kgid(net->user_ns, 0); |
| 1589 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1590 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1591 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Denis V. Lunev | 8b16924 | 2008-05-02 04:11:52 -0700 | [diff] [blame] | 1592 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops, |
| 1593 | (void *)(unsigned long)af); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1594 | if (!proc) |
| 1595 | goto out; |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1596 | if (uid_valid(root_uid) && gid_valid(root_gid)) |
| 1597 | proc_set_user(proc, root_uid, root_gid); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1598 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1599 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1600 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Denis V. Lunev | 8b16924 | 2008-05-02 04:11:52 -0700 | [diff] [blame] | 1601 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops, |
| 1602 | (void *)(unsigned long)af); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1603 | if (!proc) |
| 1604 | goto out_remove_tables; |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1605 | if (uid_valid(root_uid) && gid_valid(root_gid)) |
| 1606 | proc_set_user(proc, root_uid, root_gid); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1607 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1608 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1609 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
Denis V. Lunev | 8b16924 | 2008-05-02 04:11:52 -0700 | [diff] [blame] | 1610 | proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops, |
| 1611 | (void *)(unsigned long)af); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1612 | if (!proc) |
| 1613 | goto out_remove_matches; |
Philip Whineray | f13f2ae | 2015-11-22 11:35:07 +0000 | [diff] [blame] | 1614 | if (uid_valid(root_uid) && gid_valid(root_gid)) |
| 1615 | proc_set_user(proc, root_uid, root_gid); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1616 | #endif |
| 1617 | |
| 1618 | return 0; |
| 1619 | |
| 1620 | #ifdef CONFIG_PROC_FS |
| 1621 | out_remove_matches: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1622 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1623 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1624 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1625 | |
| 1626 | out_remove_tables: |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1627 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1628 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1629 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1630 | out: |
| 1631 | return -1; |
| 1632 | #endif |
| 1633 | } |
| 1634 | EXPORT_SYMBOL_GPL(xt_proto_init); |
| 1635 | |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1636 | void xt_proto_fini(struct net *net, u_int8_t af) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1637 | { |
| 1638 | #ifdef CONFIG_PROC_FS |
| 1639 | char buf[XT_FUNCTION_MAXNAMELEN]; |
| 1640 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1641 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1642 | strlcat(buf, FORMAT_TABLES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1643 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1644 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1645 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1646 | strlcat(buf, FORMAT_TARGETS, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1647 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1648 | |
Tobias Klauser | ce18afe | 2007-03-14 16:36:16 -0700 | [diff] [blame] | 1649 | strlcpy(buf, xt_prefix[af], sizeof(buf)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1650 | strlcat(buf, FORMAT_MATCHES, sizeof(buf)); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1651 | remove_proc_entry(buf, net->proc_net); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1652 | #endif /*CONFIG_PROC_FS*/ |
| 1653 | } |
| 1654 | EXPORT_SYMBOL_GPL(xt_proto_fini); |
| 1655 | |
Florian Westphal | dac4448 | 2016-11-22 14:44:18 +0100 | [diff] [blame] | 1656 | /** |
| 1657 | * xt_percpu_counter_alloc - allocate x_tables rule counter |
| 1658 | * |
Florian Westphal | db6a0cb | 2016-11-22 14:44:19 +0100 | [diff] [blame] | 1659 | * @state: pointer to xt_percpu allocation state |
Florian Westphal | dac4448 | 2016-11-22 14:44:18 +0100 | [diff] [blame] | 1660 | * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct |
| 1661 | * |
| 1662 | * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then |
| 1663 | * contain the address of the real (percpu) counter. |
| 1664 | * |
| 1665 | * Rule evaluation needs to use xt_get_this_cpu_counter() helper |
| 1666 | * to fetch the real percpu counter. |
| 1667 | * |
Florian Westphal | db6a0cb | 2016-11-22 14:44:19 +0100 | [diff] [blame] | 1668 | * To speed up allocation and improve data locality, a 4kb block is |
| 1669 | * allocated. |
| 1670 | * |
| 1671 | * xt_percpu_counter_alloc_state contains the base address of the |
| 1672 | * allocated page and the current sub-offset. |
| 1673 | * |
Florian Westphal | dac4448 | 2016-11-22 14:44:18 +0100 | [diff] [blame] | 1674 | * returns false on error. |
| 1675 | */ |
Florian Westphal | db6a0cb | 2016-11-22 14:44:19 +0100 | [diff] [blame] | 1676 | bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state, |
| 1677 | struct xt_counters *counter) |
Florian Westphal | dac4448 | 2016-11-22 14:44:18 +0100 | [diff] [blame] | 1678 | { |
Florian Westphal | db6a0cb | 2016-11-22 14:44:19 +0100 | [diff] [blame] | 1679 | BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2)); |
Florian Westphal | dac4448 | 2016-11-22 14:44:18 +0100 | [diff] [blame] | 1680 | |
| 1681 | if (nr_cpu_ids <= 1) |
| 1682 | return true; |
| 1683 | |
Florian Westphal | db6a0cb | 2016-11-22 14:44:19 +0100 | [diff] [blame] | 1684 | if (!state->mem) { |
| 1685 | state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE, |
| 1686 | XT_PCPU_BLOCK_SIZE); |
| 1687 | if (!state->mem) |
| 1688 | return false; |
| 1689 | } |
| 1690 | counter->pcnt = (__force unsigned long)(state->mem + state->off); |
| 1691 | state->off += sizeof(*counter); |
| 1692 | if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) { |
| 1693 | state->mem = NULL; |
| 1694 | state->off = 0; |
| 1695 | } |
Florian Westphal | dac4448 | 2016-11-22 14:44:18 +0100 | [diff] [blame] | 1696 | return true; |
| 1697 | } |
| 1698 | EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc); |
| 1699 | |
Florian Westphal | 61346e2 | 2016-11-22 14:44:17 +0100 | [diff] [blame] | 1700 | void xt_percpu_counter_free(struct xt_counters *counters) |
| 1701 | { |
| 1702 | unsigned long pcnt = counters->pcnt; |
| 1703 | |
Florian Westphal | db6a0cb | 2016-11-22 14:44:19 +0100 | [diff] [blame] | 1704 | if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0) |
Florian Westphal | 61346e2 | 2016-11-22 14:44:17 +0100 | [diff] [blame] | 1705 | free_percpu((void __percpu *)pcnt); |
| 1706 | } |
| 1707 | EXPORT_SYMBOL_GPL(xt_percpu_counter_free); |
| 1708 | |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1709 | static int __net_init xt_net_init(struct net *net) |
| 1710 | { |
| 1711 | int i; |
| 1712 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1713 | for (i = 0; i < NFPROTO_NUMPROTO; i++) |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1714 | INIT_LIST_HEAD(&net->xt.tables[i]); |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | static struct pernet_operations xt_net_ops = { |
| 1719 | .init = xt_net_init, |
| 1720 | }; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1721 | |
| 1722 | static int __init xt_init(void) |
| 1723 | { |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1724 | unsigned int i; |
| 1725 | int rv; |
| 1726 | |
| 1727 | for_each_possible_cpu(i) { |
Eric Dumazet | 7f5c6d4 | 2011-04-04 17:04:03 +0200 | [diff] [blame] | 1728 | seqcount_init(&per_cpu(xt_recseq, i)); |
Stephen Hemminger | 942e4a2 | 2009-04-28 22:36:33 -0700 | [diff] [blame] | 1729 | } |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1730 | |
Francesco Ruggeri | 9b04b51 | 2019-05-10 09:19:30 -0700 | [diff] [blame] | 1731 | xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1732 | if (!xt) |
| 1733 | return -ENOMEM; |
| 1734 | |
Jan Engelhardt | 7e9c6ee | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 1735 | for (i = 0; i < NFPROTO_NUMPROTO; i++) { |
Ingo Molnar | 9e19bb6 | 2006-03-25 01:41:10 -0800 | [diff] [blame] | 1736 | mutex_init(&xt[i].mutex); |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1737 | #ifdef CONFIG_COMPAT |
| 1738 | mutex_init(&xt[i].compat_mutex); |
Eric Dumazet | 255d0dc | 2010-12-18 18:35:15 +0100 | [diff] [blame] | 1739 | xt[i].compat_tab = NULL; |
Dmitry Mishin | 2722971 | 2006-04-01 02:25:19 -0800 | [diff] [blame] | 1740 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1741 | INIT_LIST_HEAD(&xt[i].target); |
| 1742 | INIT_LIST_HEAD(&xt[i].match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1743 | } |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1744 | rv = register_pernet_subsys(&xt_net_ops); |
| 1745 | if (rv < 0) |
| 1746 | kfree(xt); |
| 1747 | return rv; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | static void __exit xt_fini(void) |
| 1751 | { |
Alexey Dobriyan | 8d87005 | 2008-01-31 04:02:13 -0800 | [diff] [blame] | 1752 | unregister_pernet_subsys(&xt_net_ops); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1753 | kfree(xt); |
| 1754 | } |
| 1755 | |
| 1756 | module_init(xt_init); |
| 1757 | module_exit(xt_fini); |
| 1758 | |