blob: 54a489f16b171220aaffef3ea43355fd89cd91ce [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Harald Welte2e4e6a12006-01-12 13:30:04 -08002/*
3 * x_tables core - Backend for {ip,ip6,arp}_tables
4 *
5 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02006 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
Harald Welte2e4e6a12006-01-12 13:30:04 -08007 *
8 * Based on existing ip_tables code which is
9 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
10 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
Harald Welte2e4e6a12006-01-12 13:30:04 -080011 */
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Harald Welte2e4e6a12006-01-12 13:30:04 -080013#include <linux/kernel.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040014#include <linux/module.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080015#include <linux/socket.h>
16#include <linux/net.h>
17#include <linux/proc_fs.h>
18#include <linux/seq_file.h>
19#include <linux/string.h>
20#include <linux/vmalloc.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080021#include <linux/mutex.h>
Al Virod7fe0f22006-12-03 23:15:30 -050022#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Thomas Graffbabf312011-01-16 18:12:59 +010024#include <linux/audit.h>
Philip Whinerayf13f2ae2015-11-22 11:35:07 +000025#include <linux/user_namespace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020026#include <net/net_namespace.h>
Florian Westphal1d610d42021-04-01 16:11:11 +020027#include <net/netns/generic.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080028
29#include <linux/netfilter/x_tables.h>
30#include <linux/netfilter_arp.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020031#include <linux/netfilter_ipv4/ip_tables.h>
32#include <linux/netfilter_ipv6/ip6_tables.h>
33#include <linux/netfilter_arp/arp_tables.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080034
Harald Welte2e4e6a12006-01-12 13:30:04 -080035MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt043ef462008-10-08 11:35:15 +020037MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080038
Florian Westphalae0ac0e2016-11-22 14:44:19 +010039#define XT_PCPU_BLOCK_SIZE 4096
Florian Westphal19926962018-02-27 19:42:31 +010040#define XT_MAX_TABLE_SIZE (512 * 1024 * 1024)
Harald Welte2e4e6a12006-01-12 13:30:04 -080041
Florian Westphalfdacd57c2021-08-03 16:47:19 +020042struct xt_template {
43 struct list_head list;
44
45 /* called when table is needed in the given netns */
46 int (*table_init)(struct net *net);
47
48 struct module *me;
49
50 /* A unique name... */
51 char name[XT_TABLE_MAXNAMELEN];
52};
53
54static struct list_head xt_templates[NFPROTO_NUMPROTO];
55
Florian Westphal1d610d42021-04-01 16:11:11 +020056struct xt_pernet {
57 struct list_head tables[NFPROTO_NUMPROTO];
58};
59
Patrick McHardyb386d9f2007-12-17 21:47:48 -080060struct compat_delta {
Eric Dumazet255d0dc2010-12-18 18:35:15 +010061 unsigned int offset; /* offset in kernel */
62 int delta; /* delta in 32bit user land */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080063};
64
Harald Welte2e4e6a12006-01-12 13:30:04 -080065struct xt_af {
Ingo Molnar9e19bb62006-03-25 01:41:10 -080066 struct mutex mutex;
Harald Welte2e4e6a12006-01-12 13:30:04 -080067 struct list_head match;
68 struct list_head target;
Florian Westphal47a69592021-04-26 12:14:40 +020069#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080070 struct mutex compat_mutex;
Eric Dumazet255d0dc2010-12-18 18:35:15 +010071 struct compat_delta *compat_tab;
72 unsigned int number; /* number of slots in compat_tab[] */
73 unsigned int cur; /* number of used slots in compat_tab[] */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080074#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080075};
76
Florian Westphal1d610d42021-04-01 16:11:11 +020077static unsigned int xt_pernet_id __read_mostly;
78static struct xt_af *xt __read_mostly;
Harald Welte2e4e6a12006-01-12 13:30:04 -080079
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020080static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
81 [NFPROTO_UNSPEC] = "x",
82 [NFPROTO_IPV4] = "ip",
83 [NFPROTO_ARP] = "arp",
84 [NFPROTO_BRIDGE] = "eb",
85 [NFPROTO_IPV6] = "ip6",
Patrick McHardy37f9f732006-03-20 17:59:06 -080086};
87
Harald Welte2e4e6a12006-01-12 13:30:04 -080088/* Registration hooks for targets. */
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020089int xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080090{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020091 u_int8_t af = target->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -080092
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020093 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080094 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080095 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020096 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -080097}
98EXPORT_SYMBOL(xt_register_target);
99
100void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800101xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800102{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200103 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800104
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800105 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700106 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800107 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800108}
109EXPORT_SYMBOL(xt_unregister_target);
110
111int
Patrick McHardy52d9c422006-08-22 00:33:45 -0700112xt_register_targets(struct xt_target *target, unsigned int n)
113{
114 unsigned int i;
115 int err = 0;
116
117 for (i = 0; i < n; i++) {
118 err = xt_register_target(&target[i]);
119 if (err)
120 goto err;
121 }
122 return err;
123
124err:
125 if (i > 0)
126 xt_unregister_targets(target, i);
127 return err;
128}
129EXPORT_SYMBOL(xt_register_targets);
130
131void
132xt_unregister_targets(struct xt_target *target, unsigned int n)
133{
Changli Gaof68c5302010-10-04 22:24:12 +0200134 while (n-- > 0)
135 xt_unregister_target(&target[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700136}
137EXPORT_SYMBOL(xt_unregister_targets);
138
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200139int xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800140{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200141 u_int8_t af = match->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800142
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200143 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800144 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800145 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200146 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800147}
148EXPORT_SYMBOL(xt_register_match);
149
150void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800151xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800152{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200153 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800154
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800155 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700156 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800157 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800158}
159EXPORT_SYMBOL(xt_unregister_match);
160
Patrick McHardy52d9c422006-08-22 00:33:45 -0700161int
162xt_register_matches(struct xt_match *match, unsigned int n)
163{
164 unsigned int i;
165 int err = 0;
166
167 for (i = 0; i < n; i++) {
168 err = xt_register_match(&match[i]);
169 if (err)
170 goto err;
171 }
172 return err;
173
174err:
175 if (i > 0)
176 xt_unregister_matches(match, i);
177 return err;
178}
179EXPORT_SYMBOL(xt_register_matches);
180
181void
182xt_unregister_matches(struct xt_match *match, unsigned int n)
183{
Changli Gaof68c5302010-10-04 22:24:12 +0200184 while (n-- > 0)
185 xt_unregister_match(&match[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700186}
187EXPORT_SYMBOL(xt_unregister_matches);
188
Harald Welte2e4e6a12006-01-12 13:30:04 -0800189
190/*
191 * These are weird, but module loading must not be done with mutex
192 * held (since they will register), and we have to have a single
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100193 * function to use.
Harald Welte2e4e6a12006-01-12 13:30:04 -0800194 */
195
196/* Find match, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200197struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800198{
199 struct xt_match *m;
Patrick McHardy42046e22011-03-14 19:11:44 +0100200 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800201
Florian Westphaldceb48d2018-04-25 13:38:47 +0200202 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
203 return ERR_PTR(-EINVAL);
204
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200205 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800206 list_for_each_entry(m, &xt[af].match, list) {
207 if (strcmp(m->name, name) == 0) {
208 if (m->revision == revision) {
209 if (try_module_get(m->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800210 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800211 return m;
212 }
213 } else
214 err = -EPROTOTYPE; /* Found something. */
215 }
216 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800217 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200218
219 if (af != NFPROTO_UNSPEC)
220 /* Try searching again in the family-independent list */
221 return xt_find_match(NFPROTO_UNSPEC, name, revision);
222
Harald Welte2e4e6a12006-01-12 13:30:04 -0800223 return ERR_PTR(err);
224}
225EXPORT_SYMBOL(xt_find_match);
226
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200227struct xt_match *
228xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
229{
230 struct xt_match *match;
231
Eric Dumazetda17c732018-01-24 17:16:09 -0800232 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
233 return ERR_PTR(-EINVAL);
234
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100235 match = xt_find_match(nfproto, name, revision);
236 if (IS_ERR(match)) {
237 request_module("%st_%s", xt_prefix[nfproto], name);
238 match = xt_find_match(nfproto, name, revision);
239 }
240
241 return match;
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200242}
243EXPORT_SYMBOL_GPL(xt_request_find_match);
244
Harald Welte2e4e6a12006-01-12 13:30:04 -0800245/* Find target, grabs ref. Returns ERR_PTR() on error. */
Florian Westphal3b0a0812019-04-04 10:58:20 +0200246static struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800247{
248 struct xt_target *t;
Patrick McHardy42046e22011-03-14 19:11:44 +0100249 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800250
Florian Westphaldceb48d2018-04-25 13:38:47 +0200251 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
252 return ERR_PTR(-EINVAL);
253
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200254 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800255 list_for_each_entry(t, &xt[af].target, list) {
256 if (strcmp(t->name, name) == 0) {
257 if (t->revision == revision) {
258 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800259 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800260 return t;
261 }
262 } else
263 err = -EPROTOTYPE; /* Found something. */
264 }
265 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800266 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200267
268 if (af != NFPROTO_UNSPEC)
269 /* Try searching again in the family-independent list */
270 return xt_find_target(NFPROTO_UNSPEC, name, revision);
271
Harald Welte2e4e6a12006-01-12 13:30:04 -0800272 return ERR_PTR(err);
273}
Harald Welte2e4e6a12006-01-12 13:30:04 -0800274
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200275struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800276{
277 struct xt_target *target;
278
Eric Dumazetda17c732018-01-24 17:16:09 -0800279 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
280 return ERR_PTR(-EINVAL);
281
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100282 target = xt_find_target(af, name, revision);
283 if (IS_ERR(target)) {
284 request_module("%st_%s", xt_prefix[af], name);
285 target = xt_find_target(af, name, revision);
286 }
287
288 return target;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800289}
290EXPORT_SYMBOL_GPL(xt_request_find_target);
291
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500292
293static int xt_obj_to_user(u16 __user *psize, u16 size,
294 void __user *pname, const char *name,
295 u8 __user *prev, u8 rev)
296{
297 if (put_user(size, psize))
298 return -EFAULT;
299 if (copy_to_user(pname, name, strlen(name) + 1))
300 return -EFAULT;
301 if (put_user(rev, prev))
302 return -EFAULT;
303
304 return 0;
305}
306
307#define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE) \
308 xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size, \
309 U->u.user.name, K->u.kernel.TYPE->name, \
310 &U->u.user.revision, K->u.kernel.TYPE->revision)
311
312int xt_data_to_user(void __user *dst, const void *src,
Willem de Bruijn324318f2017-05-09 16:17:37 -0400313 int usersize, int size, int aligned_size)
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500314{
315 usersize = usersize ? : size;
316 if (copy_to_user(dst, src, usersize))
317 return -EFAULT;
Willem de Bruijn324318f2017-05-09 16:17:37 -0400318 if (usersize != aligned_size &&
319 clear_user(dst + usersize, aligned_size - usersize))
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500320 return -EFAULT;
321
322 return 0;
323}
324EXPORT_SYMBOL_GPL(xt_data_to_user);
325
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400326#define XT_DATA_TO_USER(U, K, TYPE) \
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500327 xt_data_to_user(U->data, K->data, \
328 K->u.kernel.TYPE->usersize, \
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400329 K->u.kernel.TYPE->TYPE##size, \
330 XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500331
332int xt_match_to_user(const struct xt_entry_match *m,
333 struct xt_entry_match __user *u)
334{
335 return XT_OBJ_TO_USER(u, m, match, 0) ||
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400336 XT_DATA_TO_USER(u, m, match);
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500337}
338EXPORT_SYMBOL_GPL(xt_match_to_user);
339
340int xt_target_to_user(const struct xt_entry_target *t,
341 struct xt_entry_target __user *u)
342{
343 return XT_OBJ_TO_USER(u, t, target, 0) ||
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400344 XT_DATA_TO_USER(u, t, target);
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500345}
346EXPORT_SYMBOL_GPL(xt_target_to_user);
347
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200348static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800349{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200350 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800351 int have_rev = 0;
352
Vasily Averin8e24edd2021-02-27 11:27:45 +0300353 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800354 list_for_each_entry(m, &xt[af].match, list) {
355 if (strcmp(m->name, name) == 0) {
356 if (m->revision > *bestp)
357 *bestp = m->revision;
358 if (m->revision == revision)
359 have_rev = 1;
360 }
361 }
Vasily Averin8e24edd2021-02-27 11:27:45 +0300362 mutex_unlock(&xt[af].mutex);
Patrick McHardy656caff2009-01-12 00:06:04 +0000363
364 if (af != NFPROTO_UNSPEC && !have_rev)
365 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
366
Harald Welte2e4e6a12006-01-12 13:30:04 -0800367 return have_rev;
368}
369
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200370static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800371{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200372 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800373 int have_rev = 0;
374
Vasily Averin8e24edd2021-02-27 11:27:45 +0300375 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800376 list_for_each_entry(t, &xt[af].target, list) {
377 if (strcmp(t->name, name) == 0) {
378 if (t->revision > *bestp)
379 *bestp = t->revision;
380 if (t->revision == revision)
381 have_rev = 1;
382 }
383 }
Vasily Averin8e24edd2021-02-27 11:27:45 +0300384 mutex_unlock(&xt[af].mutex);
Patrick McHardy656caff2009-01-12 00:06:04 +0000385
386 if (af != NFPROTO_UNSPEC && !have_rev)
387 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
388
Harald Welte2e4e6a12006-01-12 13:30:04 -0800389 return have_rev;
390}
391
392/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200393int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800394 int *err)
395{
396 int have_rev, best = -1;
397
Harald Welte2e4e6a12006-01-12 13:30:04 -0800398 if (target == 1)
399 have_rev = target_revfn(af, name, revision, &best);
400 else
401 have_rev = match_revfn(af, name, revision, &best);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800402
403 /* Nothing at all? Return 0 to try loading module. */
404 if (best == -1) {
405 *err = -ENOENT;
406 return 0;
407 }
408
409 *err = best;
410 if (!have_rev)
411 *err = -EPROTONOSUPPORT;
412 return 1;
413}
414EXPORT_SYMBOL_GPL(xt_find_revision);
415
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000416static char *
417textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
Jan Engelhardt45185362009-04-05 10:43:09 +0200418{
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000419 static const char *const inetbr_names[] = {
Jan Engelhardt45185362009-04-05 10:43:09 +0200420 "PREROUTING", "INPUT", "FORWARD",
421 "OUTPUT", "POSTROUTING", "BROUTING",
422 };
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000423 static const char *const arp_names[] = {
424 "INPUT", "FORWARD", "OUTPUT",
425 };
426 const char *const *names;
427 unsigned int i, max;
Jan Engelhardt45185362009-04-05 10:43:09 +0200428 char *p = buf;
429 bool np = false;
430 int res;
431
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000432 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
433 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
434 ARRAY_SIZE(inetbr_names);
Jan Engelhardt45185362009-04-05 10:43:09 +0200435 *p = '\0';
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000436 for (i = 0; i < max; ++i) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200437 if (!(mask & (1 << i)))
438 continue;
439 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
440 if (res > 0) {
441 size -= res;
442 p += res;
443 }
444 np = true;
445 }
446
447 return buf;
448}
449
Florian Westphalb1d0a5d2018-03-10 01:15:45 +0100450/**
451 * xt_check_proc_name - check that name is suitable for /proc file creation
452 *
453 * @name: file name candidate
454 * @size: length of buffer
455 *
456 * some x_tables modules wish to create a file in /proc.
457 * This function makes sure that the name is suitable for this
458 * purpose, it checks that name is NUL terminated and isn't a 'special'
459 * name, like "..".
460 *
461 * returns negative number on error or 0 if name is useable.
462 */
463int xt_check_proc_name(const char *name, unsigned int size)
464{
465 if (name[0] == '\0')
466 return -EINVAL;
467
468 if (strnlen(name, size) == size)
469 return -ENAMETOOLONG;
470
471 if (strcmp(name, ".") == 0 ||
472 strcmp(name, "..") == 0 ||
473 strchr(name, '/'))
474 return -EINVAL;
475
476 return 0;
477}
478EXPORT_SYMBOL(xt_check_proc_name);
479
Jan Engelhardt916a9172008-10-08 11:35:20 +0200480int xt_check_match(struct xt_mtchk_param *par,
Li RongQing11d4dd02019-02-22 21:45:52 +0800481 unsigned int size, u16 proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800482{
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100483 int ret;
484
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200485 if (XT_ALIGN(par->match->matchsize) != size &&
486 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200487 /*
488 * ebt_among is exempt from centralized matchsize checking
489 * because it uses a dynamic-size data set.
490 */
Florian Westphal1b6cd672018-02-09 15:52:00 +0100491 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
492 xt_prefix[par->family], par->match->name,
493 par->match->revision,
494 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800495 return -EINVAL;
496 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200497 if (par->match->table != NULL &&
498 strcmp(par->match->table, par->table) != 0) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100499 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
500 xt_prefix[par->family], par->match->name,
501 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800502 return -EINVAL;
503 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200504 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200505 char used[64], allow[64];
506
Florian Westphal1b6cd672018-02-09 15:52:00 +0100507 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
508 xt_prefix[par->family], par->match->name,
509 textify_hooks(used, sizeof(used),
510 par->hook_mask, par->family),
511 textify_hooks(allow, sizeof(allow),
512 par->match->hooks,
513 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800514 return -EINVAL;
515 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200516 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100517 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
518 xt_prefix[par->family], par->match->name,
519 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800520 return -EINVAL;
521 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100522 if (par->match->checkentry != NULL) {
523 ret = par->match->checkentry(par);
524 if (ret < 0)
525 return ret;
526 else if (ret > 0)
527 /* Flag up potential errors. */
528 return -EIO;
529 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800530 return 0;
531}
532EXPORT_SYMBOL_GPL(xt_check_match);
533
Florian Westphal13631bf2016-04-01 14:17:29 +0200534/** xt_check_entry_match - check that matches end before start of target
535 *
536 * @match: beginning of xt_entry_match
537 * @target: beginning of this rules target (alleged end of matches)
538 * @alignment: alignment requirement of match structures
539 *
540 * Validates that all matches add up to the beginning of the target,
541 * and that each match covers at least the base structure size.
542 *
543 * Return: 0 on success, negative errno on failure.
544 */
545static int xt_check_entry_match(const char *match, const char *target,
546 const size_t alignment)
547{
548 const struct xt_entry_match *pos;
549 int length = target - match;
550
551 if (length == 0) /* no matches */
552 return 0;
553
554 pos = (struct xt_entry_match *)match;
555 do {
556 if ((unsigned long)pos % alignment)
557 return -EINVAL;
558
559 if (length < (int)sizeof(struct xt_entry_match))
560 return -EINVAL;
561
562 if (pos->u.match_size < sizeof(struct xt_entry_match))
563 return -EINVAL;
564
565 if (pos->u.match_size > length)
566 return -EINVAL;
567
568 length -= pos->u.match_size;
569 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
570 } while (length > 0);
571
572 return 0;
573}
574
Florian Westphal1b293e32018-02-27 19:42:29 +0100575/** xt_check_table_hooks - check hook entry points are sane
576 *
577 * @info xt_table_info to check
578 * @valid_hooks - hook entry points that we can enter from
579 *
580 * Validates that the hook entry and underflows points are set up.
581 *
582 * Return: 0 on success, negative errno on failure.
583 */
584int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
585{
Florian Westphale816a2c2018-02-27 19:42:30 +0100586 const char *err = "unsorted underflow";
587 unsigned int i, max_uflow, max_entry;
588 bool check_hooks = false;
Florian Westphal1b293e32018-02-27 19:42:29 +0100589
590 BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
591
Florian Westphale816a2c2018-02-27 19:42:30 +0100592 max_entry = 0;
593 max_uflow = 0;
594
Florian Westphal1b293e32018-02-27 19:42:29 +0100595 for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
596 if (!(valid_hooks & (1 << i)))
597 continue;
598
599 if (info->hook_entry[i] == 0xFFFFFFFF)
600 return -EINVAL;
601 if (info->underflow[i] == 0xFFFFFFFF)
602 return -EINVAL;
Florian Westphale816a2c2018-02-27 19:42:30 +0100603
604 if (check_hooks) {
605 if (max_uflow > info->underflow[i])
606 goto error;
607
608 if (max_uflow == info->underflow[i]) {
609 err = "duplicate underflow";
610 goto error;
611 }
612 if (max_entry > info->hook_entry[i]) {
613 err = "unsorted entry";
614 goto error;
615 }
616 if (max_entry == info->hook_entry[i]) {
617 err = "duplicate entry";
618 goto error;
619 }
620 }
621 max_entry = info->hook_entry[i];
622 max_uflow = info->underflow[i];
623 check_hooks = true;
Florian Westphal1b293e32018-02-27 19:42:29 +0100624 }
625
626 return 0;
Florian Westphale816a2c2018-02-27 19:42:30 +0100627error:
628 pr_err_ratelimited("%s at hook %d\n", err, i);
629 return -EINVAL;
Florian Westphal1b293e32018-02-27 19:42:29 +0100630}
631EXPORT_SYMBOL(xt_check_table_hooks);
632
Florian Westphal72597132018-03-06 08:26:00 +0100633static bool verdict_ok(int verdict)
634{
635 if (verdict > 0)
636 return true;
637
638 if (verdict < 0) {
639 int v = -verdict - 1;
640
641 if (verdict == XT_RETURN)
642 return true;
643
644 switch (v) {
645 case NF_ACCEPT: return true;
646 case NF_DROP: return true;
647 case NF_QUEUE: return true;
648 default:
649 break;
650 }
651
652 return false;
653 }
654
655 return false;
656}
657
658static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
659 const char *msg, unsigned int msglen)
660{
661 return usersize == kernsize && strnlen(msg, msglen) < msglen;
662}
663
Florian Westphal47a69592021-04-26 12:14:40 +0200664#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100665int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800666{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100667 struct xt_af *xp = &xt[af];
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800668
Florian Westphal89370862018-02-27 19:42:36 +0100669 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
670
Florian Westphal7d7d7e02018-02-27 19:42:35 +0100671 if (WARN_ON(!xp->compat_tab))
672 return -ENOMEM;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100673
674 if (xp->cur >= xp->number)
675 return -EINVAL;
676
677 if (xp->cur)
678 delta += xp->compat_tab[xp->cur - 1].delta;
679 xp->compat_tab[xp->cur].offset = offset;
680 xp->compat_tab[xp->cur].delta = delta;
681 xp->cur++;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800682 return 0;
683}
684EXPORT_SYMBOL_GPL(xt_compat_add_offset);
685
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200686void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800687{
Florian Westphal89370862018-02-27 19:42:36 +0100688 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
689
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100690 if (xt[af].compat_tab) {
691 vfree(xt[af].compat_tab);
692 xt[af].compat_tab = NULL;
693 xt[af].number = 0;
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200694 xt[af].cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800695 }
696}
697EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
698
Florian Westphal3e5e5242010-02-15 18:17:10 +0100699int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800700{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100701 struct compat_delta *tmp = xt[af].compat_tab;
702 int mid, left = 0, right = xt[af].cur - 1;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800703
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100704 while (left <= right) {
705 mid = (left + right) >> 1;
706 if (offset > tmp[mid].offset)
707 left = mid + 1;
708 else if (offset < tmp[mid].offset)
709 right = mid - 1;
710 else
711 return mid ? tmp[mid - 1].delta : 0;
712 }
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200713 return left ? tmp[left - 1].delta : 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800714}
715EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
716
Florian Westphal9782a112018-02-27 19:42:34 +0100717int xt_compat_init_offsets(u8 af, unsigned int number)
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100718{
Florian Westphal7d7d7e02018-02-27 19:42:35 +0100719 size_t mem;
720
Florian Westphal89370862018-02-27 19:42:36 +0100721 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
722
Florian Westphal7d7d7e02018-02-27 19:42:35 +0100723 if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
724 return -EINVAL;
725
726 if (WARN_ON(xt[af].compat_tab))
727 return -EINVAL;
728
729 mem = sizeof(struct compat_delta) * number;
730 if (mem > XT_MAX_TABLE_SIZE)
731 return -ENOMEM;
732
733 xt[af].compat_tab = vmalloc(mem);
734 if (!xt[af].compat_tab)
735 return -ENOMEM;
736
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100737 xt[af].number = number;
738 xt[af].cur = 0;
Florian Westphal9782a112018-02-27 19:42:34 +0100739
740 return 0;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100741}
742EXPORT_SYMBOL(xt_compat_init_offsets);
743
Jan Engelhardt5452e422008-04-14 11:15:35 +0200744int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800745{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700746 u_int16_t csize = match->compatsize ? : match->matchsize;
747 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800748}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700749EXPORT_SYMBOL_GPL(xt_compat_match_offset);
750
Florian Westphal01883462016-04-01 14:17:33 +0200751void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
752 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700753{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200754 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700755 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
Florian Westphalb29c4572021-04-07 21:38:57 +0200756 int off = xt_compat_match_offset(match);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700757 u_int16_t msize = cm->u.user.match_size;
Florian Westphal09d96862016-04-01 14:17:34 +0200758 char name[sizeof(m->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700759
760 m = *dstptr;
761 memcpy(m, cm, sizeof(*cm));
762 if (match->compat_from_user)
763 match->compat_from_user(m->data, cm->data);
764 else
765 memcpy(m->data, cm->data, msize - sizeof(*cm));
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700766
767 msize += off;
768 m->u.user.match_size = msize;
Florian Westphal09d96862016-04-01 14:17:34 +0200769 strlcpy(name, match->name, sizeof(name));
770 module_put(match->me);
771 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700772
773 *size += off;
774 *dstptr += msize;
775}
776EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
777
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400778#define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
779 xt_data_to_user(U->data, K->data, \
780 K->u.kernel.TYPE->usersize, \
781 C_SIZE, \
782 COMPAT_XT_ALIGN(C_SIZE))
783
Jan Engelhardt739674f2009-06-26 08:23:19 +0200784int xt_compat_match_to_user(const struct xt_entry_match *m,
785 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700786{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200787 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700788 struct compat_xt_entry_match __user *cm = *dstptr;
789 int off = xt_compat_match_offset(match);
790 u_int16_t msize = m->u.user.match_size - off;
791
Willem de Bruijn4915f7b2017-01-02 17:19:45 -0500792 if (XT_OBJ_TO_USER(cm, m, match, msize))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800793 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700794
795 if (match->compat_to_user) {
796 if (match->compat_to_user((void __user *)cm->data, m->data))
797 return -EFAULT;
798 } else {
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400799 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700800 return -EFAULT;
801 }
802
803 *size -= off;
804 *dstptr += msize;
805 return 0;
806}
807EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
Florian Westphalfc1221b2016-04-01 14:17:26 +0200808
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200809/* non-compat version may have padding after verdict */
810struct compat_xt_standard_target {
811 struct compat_xt_entry_target t;
812 compat_uint_t verdict;
813};
814
Florian Westphal472ebdc2018-02-27 19:42:28 +0100815struct compat_xt_error_target {
816 struct compat_xt_entry_target t;
817 char errorname[XT_FUNCTION_MAXNAMELEN];
818};
819
Florian Westphalce683e52016-04-01 14:17:28 +0200820int xt_compat_check_entry_offsets(const void *base, const char *elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +0200821 unsigned int target_offset,
822 unsigned int next_offset)
823{
Florian Westphalce683e52016-04-01 14:17:28 +0200824 long size_of_base_struct = elems - (const char *)base;
Florian Westphalfc1221b2016-04-01 14:17:26 +0200825 const struct compat_xt_entry_target *t;
826 const char *e = base;
827
Florian Westphalce683e52016-04-01 14:17:28 +0200828 if (target_offset < size_of_base_struct)
829 return -EINVAL;
830
Florian Westphalfc1221b2016-04-01 14:17:26 +0200831 if (target_offset + sizeof(*t) > next_offset)
832 return -EINVAL;
833
834 t = (void *)(e + target_offset);
835 if (t->u.target_size < sizeof(*t))
836 return -EINVAL;
837
838 if (target_offset + t->u.target_size > next_offset)
839 return -EINVAL;
840
Florian Westphal07a9da52018-02-27 19:42:27 +0100841 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
842 const struct compat_xt_standard_target *st = (const void *)t;
843
844 if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
845 return -EINVAL;
846
847 if (!verdict_ok(st->verdict))
848 return -EINVAL;
Florian Westphal472ebdc2018-02-27 19:42:28 +0100849 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
850 const struct compat_xt_error_target *et = (const void *)t;
851
852 if (!error_tg_ok(t->u.target_size, sizeof(*et),
853 et->errorname, sizeof(et->errorname)))
854 return -EINVAL;
Florian Westphal07a9da52018-02-27 19:42:27 +0100855 }
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200856
Masahiro Yamada550116d2017-02-27 14:28:58 -0800857 /* compat_xt_entry match has less strict alignment requirements,
Florian Westphal13631bf2016-04-01 14:17:29 +0200858 * otherwise they are identical. In case of padding differences
859 * we need to add compat version of xt_check_entry_match.
860 */
861 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
862
863 return xt_check_entry_match(elems, base + target_offset,
864 __alignof__(struct compat_xt_entry_match));
Florian Westphalfc1221b2016-04-01 14:17:26 +0200865}
866EXPORT_SYMBOL(xt_compat_check_entry_offsets);
Florian Westphal47a69592021-04-26 12:14:40 +0200867#endif /* CONFIG_NETFILTER_XTABLES_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800868
Florian Westphal7d358122016-04-01 14:17:23 +0200869/**
870 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
871 *
872 * @base: pointer to arp/ip/ip6t_entry
Florian Westphalce683e52016-04-01 14:17:28 +0200873 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
Florian Westphal7d358122016-04-01 14:17:23 +0200874 * @target_offset: the arp/ip/ip6_t->target_offset
875 * @next_offset: the arp/ip/ip6_t->next_offset
876 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200877 * validates that target_offset and next_offset are sane and that all
878 * match sizes (if any) align with the target offset.
Florian Westphal7d358122016-04-01 14:17:23 +0200879 *
Florian Westphalce683e52016-04-01 14:17:28 +0200880 * This function does not validate the targets or matches themselves, it
Florian Westphal13631bf2016-04-01 14:17:29 +0200881 * only tests that all the offsets and sizes are correct, that all
882 * match structures are aligned, and that the last structure ends where
883 * the target structure begins.
884 *
Florian Westphal47a69592021-04-26 12:14:40 +0200885 * Also see xt_compat_check_entry_offsets for CONFIG_NETFILTER_XTABLES_COMPAT version.
Florian Westphalce683e52016-04-01 14:17:28 +0200886 *
Florian Westphal7d358122016-04-01 14:17:23 +0200887 * The arp/ip/ip6t_entry structure @base must have passed following tests:
888 * - it must point to a valid memory location
889 * - base to base + next_offset must be accessible, i.e. not exceed allocated
890 * length.
891 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200892 * A well-formed entry looks like this:
893 *
894 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
895 * e->elems[]-----' | |
896 * matchsize | |
897 * matchsize | |
898 * | |
899 * target_offset---------------------------------' |
900 * next_offset---------------------------------------------------'
901 *
902 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
903 * This is where matches (if any) and the target reside.
904 * target_offset: beginning of target.
905 * next_offset: start of the next rule; also: size of this rule.
906 * Since targets have a minimum size, target_offset + minlen <= next_offset.
907 *
908 * Every match stores its size, sum of sizes must not exceed target_offset.
909 *
Florian Westphal7d358122016-04-01 14:17:23 +0200910 * Return: 0 on success, negative errno on failure.
911 */
912int xt_check_entry_offsets(const void *base,
Florian Westphalce683e52016-04-01 14:17:28 +0200913 const char *elems,
Florian Westphal7d358122016-04-01 14:17:23 +0200914 unsigned int target_offset,
915 unsigned int next_offset)
916{
Florian Westphalce683e52016-04-01 14:17:28 +0200917 long size_of_base_struct = elems - (const char *)base;
Florian Westphal7d358122016-04-01 14:17:23 +0200918 const struct xt_entry_target *t;
919 const char *e = base;
920
Florian Westphalce683e52016-04-01 14:17:28 +0200921 /* target start is within the ip/ip6/arpt_entry struct */
922 if (target_offset < size_of_base_struct)
923 return -EINVAL;
924
Florian Westphal7d358122016-04-01 14:17:23 +0200925 if (target_offset + sizeof(*t) > next_offset)
926 return -EINVAL;
927
928 t = (void *)(e + target_offset);
Florian Westphala08e4e192016-04-01 14:17:25 +0200929 if (t->u.target_size < sizeof(*t))
930 return -EINVAL;
931
Florian Westphal7d358122016-04-01 14:17:23 +0200932 if (target_offset + t->u.target_size > next_offset)
933 return -EINVAL;
934
Florian Westphal07a9da52018-02-27 19:42:27 +0100935 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
936 const struct xt_standard_target *st = (const void *)t;
937
938 if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
939 return -EINVAL;
940
941 if (!verdict_ok(st->verdict))
942 return -EINVAL;
Florian Westphal472ebdc2018-02-27 19:42:28 +0100943 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
944 const struct xt_error_target *et = (const void *)t;
945
946 if (!error_tg_ok(t->u.target_size, sizeof(*et),
947 et->errorname, sizeof(et->errorname)))
948 return -EINVAL;
Florian Westphal07a9da52018-02-27 19:42:27 +0100949 }
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200950
Florian Westphal13631bf2016-04-01 14:17:29 +0200951 return xt_check_entry_match(elems, base + target_offset,
952 __alignof__(struct xt_entry_match));
Florian Westphal7d358122016-04-01 14:17:23 +0200953}
954EXPORT_SYMBOL(xt_check_entry_offsets);
955
Florian Westphalf4dc7772016-07-14 17:51:26 +0200956/**
957 * xt_alloc_entry_offsets - allocate array to store rule head offsets
958 *
959 * @size: number of entries
960 *
Joe Perchesb9e01022020-01-28 11:07:27 -0800961 * Return: NULL or zeroed kmalloc'd or vmalloc'd array
Florian Westphalf4dc7772016-07-14 17:51:26 +0200962 */
963unsigned int *xt_alloc_entry_offsets(unsigned int size)
964{
Florian Westphal9d5c12a2018-02-27 19:42:32 +0100965 if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
966 return NULL;
967
Joe Perchesb9e01022020-01-28 11:07:27 -0800968 return kvcalloc(size, sizeof(unsigned int), GFP_KERNEL);
Florian Westphalf4dc7772016-07-14 17:51:26 +0200969
Florian Westphalf4dc7772016-07-14 17:51:26 +0200970}
971EXPORT_SYMBOL(xt_alloc_entry_offsets);
972
973/**
974 * xt_find_jump_offset - check if target is a valid jump offset
975 *
976 * @offsets: array containing all valid rule start offsets of a rule blob
977 * @target: the jump target to search for
978 * @size: entries in @offset
979 */
980bool xt_find_jump_offset(const unsigned int *offsets,
981 unsigned int target, unsigned int size)
982{
983 int m, low = 0, hi = size;
984
985 while (hi > low) {
986 m = (low + hi) / 2u;
987
988 if (offsets[m] > target)
989 hi = m;
990 else if (offsets[m] < target)
991 low = m + 1;
992 else
993 return true;
994 }
995
996 return false;
997}
998EXPORT_SYMBOL(xt_find_jump_offset);
999
Jan Engelhardt916a9172008-10-08 11:35:20 +02001000int xt_check_target(struct xt_tgchk_param *par,
Li RongQing11d4dd02019-02-22 21:45:52 +08001001 unsigned int size, u16 proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -08001002{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +01001003 int ret;
1004
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +02001005 if (XT_ALIGN(par->target->targetsize) != size) {
Florian Westphal1b6cd672018-02-09 15:52:00 +01001006 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
1007 xt_prefix[par->family], par->target->name,
1008 par->target->revision,
1009 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -08001010 return -EINVAL;
1011 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +02001012 if (par->target->table != NULL &&
1013 strcmp(par->target->table, par->table) != 0) {
Florian Westphal1b6cd672018-02-09 15:52:00 +01001014 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
1015 xt_prefix[par->family], par->target->name,
1016 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -08001017 return -EINVAL;
1018 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +02001019 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +02001020 char used[64], allow[64];
1021
Florian Westphal1b6cd672018-02-09 15:52:00 +01001022 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
1023 xt_prefix[par->family], par->target->name,
1024 textify_hooks(used, sizeof(used),
1025 par->hook_mask, par->family),
1026 textify_hooks(allow, sizeof(allow),
1027 par->target->hooks,
1028 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -08001029 return -EINVAL;
1030 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +02001031 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Florian Westphal1b6cd672018-02-09 15:52:00 +01001032 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
1033 xt_prefix[par->family], par->target->name,
1034 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -08001035 return -EINVAL;
1036 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +01001037 if (par->target->checkentry != NULL) {
1038 ret = par->target->checkentry(par);
1039 if (ret < 0)
1040 return ret;
1041 else if (ret > 0)
1042 /* Flag up potential errors. */
1043 return -EIO;
1044 }
Patrick McHardy37f9f732006-03-20 17:59:06 -08001045 return 0;
1046}
1047EXPORT_SYMBOL_GPL(xt_check_target);
1048
Florian Westphald7591f02016-04-01 15:37:59 +02001049/**
Christoph Hellwigab214d12020-07-23 08:08:53 +02001050 * xt_copy_counters - copy counters and metadata from a sockptr_t
Florian Westphald7591f02016-04-01 15:37:59 +02001051 *
Christoph Hellwigab214d12020-07-23 08:08:53 +02001052 * @arg: src sockptr
Florian Westphald7591f02016-04-01 15:37:59 +02001053 * @len: alleged size of userspace memory
1054 * @info: where to store the xt_counters_info metadata
Florian Westphald7591f02016-04-01 15:37:59 +02001055 *
1056 * Copies counter meta data from @user and stores it in @info.
1057 *
1058 * vmallocs memory to hold the counters, then copies the counter data
1059 * from @user to the new memory and returns a pointer to it.
1060 *
Christoph Hellwigc34bc102020-07-17 08:23:21 +02001061 * If called from a compat syscall, @info gets converted automatically to the
1062 * 64bit representation.
Florian Westphald7591f02016-04-01 15:37:59 +02001063 *
1064 * The metadata associated with the counters is stored in @info.
1065 *
1066 * Return: returns pointer that caller has to test via IS_ERR().
1067 * If IS_ERR is false, caller has to vfree the pointer.
1068 */
Christoph Hellwigab214d12020-07-23 08:08:53 +02001069void *xt_copy_counters(sockptr_t arg, unsigned int len,
1070 struct xt_counters_info *info)
Florian Westphald7591f02016-04-01 15:37:59 +02001071{
Christoph Hellwigd3c48152020-07-28 18:38:35 +02001072 size_t offset;
Florian Westphald7591f02016-04-01 15:37:59 +02001073 void *mem;
1074 u64 size;
1075
Florian Westphal47a69592021-04-26 12:14:40 +02001076#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
Christoph Hellwigc34bc102020-07-17 08:23:21 +02001077 if (in_compat_syscall()) {
Florian Westphald7591f02016-04-01 15:37:59 +02001078 /* structures only differ in size due to alignment */
1079 struct compat_xt_counters_info compat_tmp;
1080
1081 if (len <= sizeof(compat_tmp))
1082 return ERR_PTR(-EINVAL);
1083
1084 len -= sizeof(compat_tmp);
Christoph Hellwigab214d12020-07-23 08:08:53 +02001085 if (copy_from_sockptr(&compat_tmp, arg, sizeof(compat_tmp)) != 0)
Florian Westphald7591f02016-04-01 15:37:59 +02001086 return ERR_PTR(-EFAULT);
1087
Eric Dumazete466af72017-10-05 02:50:07 -07001088 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
Florian Westphald7591f02016-04-01 15:37:59 +02001089 info->num_counters = compat_tmp.num_counters;
Christoph Hellwigd3c48152020-07-28 18:38:35 +02001090 offset = sizeof(compat_tmp);
Florian Westphald7591f02016-04-01 15:37:59 +02001091 } else
1092#endif
1093 {
1094 if (len <= sizeof(*info))
1095 return ERR_PTR(-EINVAL);
1096
1097 len -= sizeof(*info);
Christoph Hellwigab214d12020-07-23 08:08:53 +02001098 if (copy_from_sockptr(info, arg, sizeof(*info)) != 0)
Florian Westphald7591f02016-04-01 15:37:59 +02001099 return ERR_PTR(-EFAULT);
1100
Christoph Hellwigd3c48152020-07-28 18:38:35 +02001101 offset = sizeof(*info);
Florian Westphald7591f02016-04-01 15:37:59 +02001102 }
Eric Dumazete466af72017-10-05 02:50:07 -07001103 info->name[sizeof(info->name) - 1] = '\0';
Florian Westphald7591f02016-04-01 15:37:59 +02001104
1105 size = sizeof(struct xt_counters);
1106 size *= info->num_counters;
1107
1108 if (size != (u64)len)
1109 return ERR_PTR(-EINVAL);
1110
1111 mem = vmalloc(len);
1112 if (!mem)
1113 return ERR_PTR(-ENOMEM);
1114
Christoph Hellwigd3c48152020-07-28 18:38:35 +02001115 if (copy_from_sockptr_offset(mem, arg, offset, len) == 0)
Florian Westphald7591f02016-04-01 15:37:59 +02001116 return mem;
1117
1118 vfree(mem);
1119 return ERR_PTR(-EFAULT);
1120}
Christoph Hellwigab214d12020-07-23 08:08:53 +02001121EXPORT_SYMBOL_GPL(xt_copy_counters);
Florian Westphald7591f02016-04-01 15:37:59 +02001122
Florian Westphal47a69592021-04-26 12:14:40 +02001123#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +02001124int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -08001125{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001126 u_int16_t csize = target->compatsize ? : target->targetsize;
1127 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -08001128}
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001129EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1130
1131void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001132 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001133{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001134 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001135 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
Florian Westphalb29c4572021-04-07 21:38:57 +02001136 int off = xt_compat_target_offset(target);
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001137 u_int16_t tsize = ct->u.user.target_size;
Florian Westphal09d96862016-04-01 14:17:34 +02001138 char name[sizeof(t->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001139
1140 t = *dstptr;
1141 memcpy(t, ct, sizeof(*ct));
1142 if (target->compat_from_user)
1143 target->compat_from_user(t->data, ct->data);
1144 else
1145 memcpy(t->data, ct->data, tsize - sizeof(*ct));
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001146
1147 tsize += off;
1148 t->u.user.target_size = tsize;
Florian Westphal09d96862016-04-01 14:17:34 +02001149 strlcpy(name, target->name, sizeof(name));
1150 module_put(target->me);
1151 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001152
1153 *size += off;
1154 *dstptr += tsize;
1155}
1156EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1157
Jan Engelhardt739674f2009-06-26 08:23:19 +02001158int xt_compat_target_to_user(const struct xt_entry_target *t,
1159 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001160{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001161 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001162 struct compat_xt_entry_target __user *ct = *dstptr;
1163 int off = xt_compat_target_offset(target);
1164 u_int16_t tsize = t->u.user.target_size - off;
1165
Willem de Bruijn4915f7b2017-01-02 17:19:45 -05001166 if (XT_OBJ_TO_USER(ct, t, target, tsize))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001167 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001168
1169 if (target->compat_to_user) {
1170 if (target->compat_to_user((void __user *)ct->data, t->data))
1171 return -EFAULT;
1172 } else {
Willem de Bruijn751a9c72017-05-17 11:24:47 -04001173 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001174 return -EFAULT;
1175 }
1176
1177 *size -= off;
1178 *dstptr += tsize;
1179 return 0;
1180}
1181EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -08001182#endif
1183
Harald Welte2e4e6a12006-01-12 13:30:04 -08001184struct xt_table_info *xt_alloc_table_info(unsigned int size)
1185{
Eric Dumazet711bdde62015-06-15 09:57:30 -07001186 struct xt_table_info *info = NULL;
1187 size_t sz = sizeof(*info) + size;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001188
Florian Westphal19926962018-02-27 19:42:31 +01001189 if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
Florian Westphald157bd72016-03-10 01:56:23 +01001190 return NULL;
1191
Michal Hockoa148ce12018-08-07 21:54:00 +02001192 info = kvmalloc(sz, GFP_KERNEL_ACCOUNT);
Michal Hockoeacd86c2017-07-12 14:35:37 -07001193 if (!info)
1194 return NULL;
1195
Eric Dumazet711bdde62015-06-15 09:57:30 -07001196 memset(info, 0, sizeof(*info));
1197 info->size = size;
1198 return info;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001199}
1200EXPORT_SYMBOL(xt_alloc_table_info);
1201
1202void xt_free_table_info(struct xt_table_info *info)
1203{
1204 int cpu;
1205
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001206 if (info->jumpstack != NULL) {
Eric Dumazetf6b50822014-06-24 02:15:35 -07001207 for_each_possible_cpu(cpu)
1208 kvfree(info->jumpstack[cpu]);
1209 kvfree(info->jumpstack);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001210 }
1211
Eric Dumazet711bdde62015-06-15 09:57:30 -07001212 kvfree(info);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001213}
1214EXPORT_SYMBOL(xt_free_table_info);
1215
Florian Westphal1ef4d6d2021-04-21 09:51:01 +02001216struct xt_table *xt_find_table(struct net *net, u8 af, const char *name)
1217{
1218 struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
1219 struct xt_table *t;
1220
1221 mutex_lock(&xt[af].mutex);
1222 list_for_each_entry(t, &xt_net->tables[af], list) {
1223 if (strcmp(t->name, name) == 0) {
1224 mutex_unlock(&xt[af].mutex);
1225 return t;
1226 }
1227 }
1228 mutex_unlock(&xt[af].mutex);
1229 return NULL;
1230}
1231EXPORT_SYMBOL(xt_find_table);
1232
Florian Westphal03d13b62017-12-08 17:01:53 +01001233/* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001234struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1235 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001236{
Florian Westphal1d610d42021-04-01 16:11:11 +02001237 struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
Florian Westphalfdacd57c2021-08-03 16:47:19 +02001238 struct module *owner = NULL;
1239 struct xt_template *tmpl;
1240 struct xt_table *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001241
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001242 mutex_lock(&xt[af].mutex);
Florian Westphal1d610d42021-04-01 16:11:11 +02001243 list_for_each_entry(t, &xt_net->tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001244 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1245 return t;
Florian Westphalb9e69e12016-02-25 10:08:36 +01001246
Florian Westphalfdacd57c2021-08-03 16:47:19 +02001247 /* Table doesn't exist in this netns, check larval list */
1248 list_for_each_entry(tmpl, &xt_templates[af], list) {
Florian Westphal03d13b62017-12-08 17:01:53 +01001249 int err;
1250
Florian Westphalfdacd57c2021-08-03 16:47:19 +02001251 if (strcmp(tmpl->name, name))
Florian Westphalb9e69e12016-02-25 10:08:36 +01001252 continue;
Florian Westphalfdacd57c2021-08-03 16:47:19 +02001253 if (!try_module_get(tmpl->me))
Florian Westphal03d13b62017-12-08 17:01:53 +01001254 goto out;
Florian Westphalfdacd57c2021-08-03 16:47:19 +02001255
1256 owner = tmpl->me;
1257
Florian Westphalb9e69e12016-02-25 10:08:36 +01001258 mutex_unlock(&xt[af].mutex);
Florian Westphalfdacd57c2021-08-03 16:47:19 +02001259 err = tmpl->table_init(net);
Florian Westphal03d13b62017-12-08 17:01:53 +01001260 if (err < 0) {
Florian Westphalfdacd57c2021-08-03 16:47:19 +02001261 module_put(owner);
Florian Westphal03d13b62017-12-08 17:01:53 +01001262 return ERR_PTR(err);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001263 }
1264
Florian Westphalb9e69e12016-02-25 10:08:36 +01001265 mutex_lock(&xt[af].mutex);
1266 break;
1267 }
1268
Florian Westphalb9e69e12016-02-25 10:08:36 +01001269 /* and once again: */
Florian Westphal1d610d42021-04-01 16:11:11 +02001270 list_for_each_entry(t, &xt_net->tables[af], list)
Florian Westphalb9e69e12016-02-25 10:08:36 +01001271 if (strcmp(t->name, name) == 0)
1272 return t;
1273
Florian Westphalfdacd57c2021-08-03 16:47:19 +02001274 module_put(owner);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001275 out:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001276 mutex_unlock(&xt[af].mutex);
Florian Westphal03d13b62017-12-08 17:01:53 +01001277 return ERR_PTR(-ENOENT);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001278}
1279EXPORT_SYMBOL_GPL(xt_find_table_lock);
1280
Florian Westphal03d13b62017-12-08 17:01:53 +01001281struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1282 const char *name)
1283{
1284 struct xt_table *t = xt_find_table_lock(net, af, name);
1285
Florian Westphal20651ce2018-01-09 14:30:48 +01001286#ifdef CONFIG_MODULES
Florian Westphal03d13b62017-12-08 17:01:53 +01001287 if (IS_ERR(t)) {
1288 int err = request_module("%stable_%s", xt_prefix[af], name);
Florian Westphale3eeacb2018-01-13 14:06:08 +01001289 if (err < 0)
Florian Westphal03d13b62017-12-08 17:01:53 +01001290 return ERR_PTR(err);
1291 t = xt_find_table_lock(net, af, name);
1292 }
1293#endif
1294
1295 return t;
1296}
1297EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1298
Harald Welte2e4e6a12006-01-12 13:30:04 -08001299void xt_table_unlock(struct xt_table *table)
1300{
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001301 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001302}
1303EXPORT_SYMBOL_GPL(xt_table_unlock);
1304
Florian Westphal47a69592021-04-26 12:14:40 +02001305#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001306void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001307{
1308 mutex_lock(&xt[af].compat_mutex);
1309}
1310EXPORT_SYMBOL_GPL(xt_compat_lock);
1311
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001312void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001313{
1314 mutex_unlock(&xt[af].compat_mutex);
1315}
1316EXPORT_SYMBOL_GPL(xt_compat_unlock);
1317#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001318
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001319DEFINE_PER_CPU(seqcount_t, xt_recseq);
1320EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001321
Florian Westphaldcebd312015-07-14 17:51:09 +02001322struct static_key xt_tee_enabled __read_mostly;
1323EXPORT_SYMBOL_GPL(xt_tee_enabled);
1324
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001325static int xt_jumpstack_alloc(struct xt_table_info *i)
1326{
1327 unsigned int size;
1328 int cpu;
1329
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001330 size = sizeof(void **) * nr_cpu_ids;
1331 if (size > PAGE_SIZE)
Michal Hocko752ade62017-05-08 15:57:27 -07001332 i->jumpstack = kvzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001333 else
Joe Perches3dbd4432011-05-28 10:36:35 -07001334 i->jumpstack = kzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001335 if (i->jumpstack == NULL)
1336 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001337
Florian Westphal98d1bd82015-07-14 17:51:06 +02001338 /* ruleset without jumps -- no stack needed */
1339 if (i->stacksize == 0)
1340 return 0;
1341
Florian Westphal7814b6e2015-07-14 17:51:08 +02001342 /* Jumpstack needs to be able to record two full callchains, one
1343 * from the first rule set traversal, plus one table reentrancy
1344 * via -j TEE without clobbering the callchain that brought us to
1345 * TEE target.
1346 *
1347 * This is done by allocating two jumpstacks per cpu, on reentry
1348 * the upper half of the stack is used.
1349 *
1350 * see the jumpstack setup in ipt_do_table() for more details.
1351 */
1352 size = sizeof(void *) * i->stacksize * 2u;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001353 for_each_possible_cpu(cpu) {
Michal Hocko752ade62017-05-08 15:57:27 -07001354 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1355 cpu_to_node(cpu));
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001356 if (i->jumpstack[cpu] == NULL)
1357 /*
1358 * Freeing will be done later on by the callers. The
1359 * chain is: xt_replace_table -> __do_replace ->
1360 * do_replace -> xt_free_table_info.
1361 */
1362 return -ENOMEM;
1363 }
1364
1365 return 0;
1366}
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001367
Florian Westphalc84ca952018-02-27 19:42:33 +01001368struct xt_counters *xt_counters_alloc(unsigned int counters)
1369{
1370 struct xt_counters *mem;
1371
1372 if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1373 return NULL;
1374
1375 counters *= sizeof(*mem);
1376 if (counters > XT_MAX_TABLE_SIZE)
1377 return NULL;
1378
1379 return vzalloc(counters);
1380}
1381EXPORT_SYMBOL(xt_counters_alloc);
1382
Harald Welte2e4e6a12006-01-12 13:30:04 -08001383struct xt_table_info *
1384xt_replace_table(struct xt_table *table,
1385 unsigned int num_counters,
1386 struct xt_table_info *newinfo,
1387 int *error)
1388{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001389 struct xt_table_info *private;
Mark Tomlinsond3d40f22021-03-08 14:24:12 +13001390 unsigned int cpu;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001391 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001392
Jan Engelhardtd97a9e42010-04-21 14:45:51 +02001393 ret = xt_jumpstack_alloc(newinfo);
1394 if (ret < 0) {
1395 *error = ret;
1396 return NULL;
1397 }
1398
Harald Welte2e4e6a12006-01-12 13:30:04 -08001399 /* Do the substitution. */
Mark Tomlinsond3d40f22021-03-08 14:24:12 +13001400 local_bh_disable();
1401 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001402
Harald Welte2e4e6a12006-01-12 13:30:04 -08001403 /* Check inside lock: is the old number correct? */
1404 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001405 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001406 num_counters, private->number);
Mark Tomlinsond3d40f22021-03-08 14:24:12 +13001407 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001408 *error = -EAGAIN;
1409 return NULL;
1410 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001411
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001412 newinfo->initial_entries = private->initial_entries;
Mark Tomlinsond3d40f22021-03-08 14:24:12 +13001413 /*
1414 * Ensure contents of newinfo are visible before assigning to
1415 * private.
1416 */
1417 smp_wmb();
1418 table->private = newinfo;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001419
Mark Tomlinsond3d40f22021-03-08 14:24:12 +13001420 /* make sure all cpus see new ->private value */
Mark Tomlinson175e4762021-03-08 14:24:13 +13001421 smp_mb();
Mark Tomlinsond3d40f22021-03-08 14:24:12 +13001422
1423 /*
1424 * Even though table entries have now been swapped, other CPU's
1425 * may still be using the old entries...
1426 */
1427 local_bh_enable();
1428
1429 /* ... so wait for even xt_recseq on all cpus */
1430 for_each_possible_cpu(cpu) {
1431 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1432 u32 seq = raw_read_seqcount(s);
1433
1434 if (seq & 1) {
1435 do {
1436 cond_resched();
1437 cpu_relax();
1438 } while (seq == raw_read_seqcount(s));
1439 }
1440 }
Florian Westphal80055da2017-10-12 01:13:50 +02001441
Richard Guy Briggsc4dad0a2020-04-22 17:39:28 -04001442 audit_log_nfcfg(table->name, table->af, private->number,
1443 !private->number ? AUDIT_XT_OP_REGISTER :
Richard Guy Briggs14224032020-06-27 23:24:19 -04001444 AUDIT_XT_OP_REPLACE,
1445 GFP_KERNEL);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001446 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001447}
1448EXPORT_SYMBOL_GPL(xt_replace_table);
1449
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001450struct xt_table *xt_register_table(struct net *net,
1451 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -08001452 struct xt_table_info *bootstrap,
1453 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001454{
Florian Westphal1d610d42021-04-01 16:11:11 +02001455 struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001456 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001457 struct xt_table *t, *table;
Florian Westphal1d610d42021-04-01 16:11:11 +02001458 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001459
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001460 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001461 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001462 if (!table) {
1463 ret = -ENOMEM;
1464 goto out;
1465 }
1466
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001467 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001468 /* Don't autoload: we'd eat our tail... */
Florian Westphal1d610d42021-04-01 16:11:11 +02001469 list_for_each_entry(t, &xt_net->tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -07001470 if (strcmp(t->name, table->name) == 0) {
1471 ret = -EEXIST;
1472 goto unlock;
1473 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001474 }
1475
1476 /* Simplifies replace_table code. */
Mark Tomlinsond3d40f22021-03-08 14:24:12 +13001477 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +01001478
Harald Welte2e4e6a12006-01-12 13:30:04 -08001479 if (!xt_replace_table(table, 0, newinfo, &ret))
1480 goto unlock;
1481
Mark Tomlinsond3d40f22021-03-08 14:24:12 +13001482 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001483 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001484
1485 /* save number of initial entries */
1486 private->initial_entries = private->number;
1487
Florian Westphal1d610d42021-04-01 16:11:11 +02001488 list_add(&table->list, &xt_net->tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001489 mutex_unlock(&xt[table->af].mutex);
1490 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001491
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001492unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001493 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001494 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001495out:
1496 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001497}
1498EXPORT_SYMBOL_GPL(xt_register_table);
1499
1500void *xt_unregister_table(struct xt_table *table)
1501{
1502 struct xt_table_info *private;
1503
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001504 mutex_lock(&xt[table->af].mutex);
Mark Tomlinsond3d40f22021-03-08 14:24:12 +13001505 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -07001506 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001507 mutex_unlock(&xt[table->af].mutex);
Richard Guy Briggsa45d8852020-04-22 17:39:29 -04001508 audit_log_nfcfg(table->name, table->af, private->number,
Richard Guy Briggs14224032020-06-27 23:24:19 -04001509 AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
Florian Westphalae689332021-04-21 09:51:07 +02001510 kfree(table->ops);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001511 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001512
1513 return private;
1514}
1515EXPORT_SYMBOL_GPL(xt_unregister_table);
1516
1517#ifdef CONFIG_PROC_FS
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001518static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001519{
Muchun Song359745d2022-01-21 22:14:23 -08001520 u8 af = (unsigned long)pde_data(file_inode(seq->file));
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001521 struct net *net = seq_file_net(seq);
Florian Westphal1d610d42021-04-01 16:11:11 +02001522 struct xt_pernet *xt_net;
1523
1524 xt_net = net_generic(net, xt_pernet_id);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001525
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001526 mutex_lock(&xt[af].mutex);
Florian Westphal1d610d42021-04-01 16:11:11 +02001527 return seq_list_start(&xt_net->tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001528}
1529
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001530static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001531{
Muchun Song359745d2022-01-21 22:14:23 -08001532 u8 af = (unsigned long)pde_data(file_inode(seq->file));
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001533 struct net *net = seq_file_net(seq);
Florian Westphal1d610d42021-04-01 16:11:11 +02001534 struct xt_pernet *xt_net;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001535
Florian Westphal1d610d42021-04-01 16:11:11 +02001536 xt_net = net_generic(net, xt_pernet_id);
1537
1538 return seq_list_next(v, &xt_net->tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001539}
1540
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001541static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001542{
Muchun Song359745d2022-01-21 22:14:23 -08001543 u_int8_t af = (unsigned long)pde_data(file_inode(seq->file));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001544
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001545 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001546}
1547
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001548static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001549{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001550 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001551
Joe Perches861fb102015-05-12 18:28:23 -07001552 if (*table->name)
Steven Rostedt (Red Hat)e71456ae2014-10-27 17:43:45 -04001553 seq_printf(seq, "%s\n", table->name);
Joe Perches861fb102015-05-12 18:28:23 -07001554 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001555}
1556
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001557static const struct seq_operations xt_table_seq_ops = {
1558 .start = xt_table_seq_start,
1559 .next = xt_table_seq_next,
1560 .stop = xt_table_seq_stop,
1561 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001562};
1563
Jan Engelhardteb132202009-02-18 16:42:19 +01001564/*
1565 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1566 * the multi-AF mutexes.
1567 */
1568struct nf_mttg_trav {
1569 struct list_head *head, *curr;
Christoph Hellwig1d98c162018-04-10 21:59:39 +02001570 uint8_t class;
Jan Engelhardteb132202009-02-18 16:42:19 +01001571};
1572
1573enum {
1574 MTTG_TRAV_INIT,
1575 MTTG_TRAV_NFP_UNSPEC,
1576 MTTG_TRAV_NFP_SPEC,
1577 MTTG_TRAV_DONE,
1578};
1579
1580static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1581 bool is_target)
1582{
1583 static const uint8_t next_class[] = {
1584 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1585 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1586 };
Muchun Song359745d2022-01-21 22:14:23 -08001587 uint8_t nfproto = (unsigned long)pde_data(file_inode(seq->file));
Jan Engelhardteb132202009-02-18 16:42:19 +01001588 struct nf_mttg_trav *trav = seq->private;
1589
Vasily Averinee84f192020-02-25 10:07:12 +03001590 if (ppos != NULL)
1591 ++(*ppos);
1592
Jan Engelhardteb132202009-02-18 16:42:19 +01001593 switch (trav->class) {
1594 case MTTG_TRAV_INIT:
1595 trav->class = MTTG_TRAV_NFP_UNSPEC;
1596 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1597 trav->head = trav->curr = is_target ?
1598 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1599 break;
1600 case MTTG_TRAV_NFP_UNSPEC:
1601 trav->curr = trav->curr->next;
1602 if (trav->curr != trav->head)
1603 break;
1604 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
Christoph Hellwig1d98c162018-04-10 21:59:39 +02001605 mutex_lock(&xt[nfproto].mutex);
Jan Engelhardteb132202009-02-18 16:42:19 +01001606 trav->head = trav->curr = is_target ?
Christoph Hellwig1d98c162018-04-10 21:59:39 +02001607 &xt[nfproto].target : &xt[nfproto].match;
Jan Engelhardteb132202009-02-18 16:42:19 +01001608 trav->class = next_class[trav->class];
1609 break;
1610 case MTTG_TRAV_NFP_SPEC:
1611 trav->curr = trav->curr->next;
1612 if (trav->curr != trav->head)
1613 break;
Gustavo A. R. Silva954d8292020-07-08 15:09:39 -05001614 fallthrough;
Jan Engelhardteb132202009-02-18 16:42:19 +01001615 default:
1616 return NULL;
1617 }
Jan Engelhardteb132202009-02-18 16:42:19 +01001618 return trav;
1619}
1620
1621static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1622 bool is_target)
1623{
1624 struct nf_mttg_trav *trav = seq->private;
1625 unsigned int j;
1626
1627 trav->class = MTTG_TRAV_INIT;
1628 for (j = 0; j < *pos; ++j)
1629 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1630 return NULL;
1631 return trav;
1632}
1633
1634static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1635{
Muchun Song359745d2022-01-21 22:14:23 -08001636 uint8_t nfproto = (unsigned long)pde_data(file_inode(seq->file));
Jan Engelhardteb132202009-02-18 16:42:19 +01001637 struct nf_mttg_trav *trav = seq->private;
1638
1639 switch (trav->class) {
1640 case MTTG_TRAV_NFP_UNSPEC:
1641 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1642 break;
1643 case MTTG_TRAV_NFP_SPEC:
Christoph Hellwig1d98c162018-04-10 21:59:39 +02001644 mutex_unlock(&xt[nfproto].mutex);
Jan Engelhardteb132202009-02-18 16:42:19 +01001645 break;
1646 }
1647}
1648
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001649static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1650{
Jan Engelhardteb132202009-02-18 16:42:19 +01001651 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001652}
1653
Jan Engelhardteb132202009-02-18 16:42:19 +01001654static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001655{
Jan Engelhardteb132202009-02-18 16:42:19 +01001656 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001657}
1658
1659static int xt_match_seq_show(struct seq_file *seq, void *v)
1660{
Jan Engelhardteb132202009-02-18 16:42:19 +01001661 const struct nf_mttg_trav *trav = seq->private;
1662 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001663
Jan Engelhardteb132202009-02-18 16:42:19 +01001664 switch (trav->class) {
1665 case MTTG_TRAV_NFP_UNSPEC:
1666 case MTTG_TRAV_NFP_SPEC:
1667 if (trav->curr == trav->head)
1668 return 0;
1669 match = list_entry(trav->curr, struct xt_match, list);
Joe Perches861fb102015-05-12 18:28:23 -07001670 if (*match->name)
1671 seq_printf(seq, "%s\n", match->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001672 }
1673 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001674}
1675
1676static const struct seq_operations xt_match_seq_ops = {
1677 .start = xt_match_seq_start,
1678 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001679 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001680 .show = xt_match_seq_show,
1681};
1682
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001683static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1684{
Jan Engelhardteb132202009-02-18 16:42:19 +01001685 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001686}
1687
Jan Engelhardteb132202009-02-18 16:42:19 +01001688static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001689{
Jan Engelhardteb132202009-02-18 16:42:19 +01001690 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001691}
1692
1693static int xt_target_seq_show(struct seq_file *seq, void *v)
1694{
Jan Engelhardteb132202009-02-18 16:42:19 +01001695 const struct nf_mttg_trav *trav = seq->private;
1696 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001697
Jan Engelhardteb132202009-02-18 16:42:19 +01001698 switch (trav->class) {
1699 case MTTG_TRAV_NFP_UNSPEC:
1700 case MTTG_TRAV_NFP_SPEC:
1701 if (trav->curr == trav->head)
1702 return 0;
1703 target = list_entry(trav->curr, struct xt_target, list);
Joe Perches861fb102015-05-12 18:28:23 -07001704 if (*target->name)
1705 seq_printf(seq, "%s\n", target->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001706 }
1707 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001708}
1709
1710static const struct seq_operations xt_target_seq_ops = {
1711 .start = xt_target_seq_start,
1712 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001713 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001714 .show = xt_target_seq_show,
1715};
1716
Harald Welte2e4e6a12006-01-12 13:30:04 -08001717#define FORMAT_TABLES "_tables_names"
1718#define FORMAT_MATCHES "_tables_matches"
1719#define FORMAT_TARGETS "_tables_targets"
1720
1721#endif /* CONFIG_PROC_FS */
1722
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001723/**
Florian Westphalb9e69e12016-02-25 10:08:36 +01001724 * xt_hook_ops_alloc - set up hooks for a new table
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001725 * @table: table with metadata needed to set up hooks
1726 * @fn: Hook function
1727 *
Florian Westphalb9e69e12016-02-25 10:08:36 +01001728 * This function will create the nf_hook_ops that the x_table needs
1729 * to hand to xt_hook_link_net().
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001730 */
Florian Westphalb9e69e12016-02-25 10:08:36 +01001731struct nf_hook_ops *
1732xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001733{
1734 unsigned int hook_mask = table->valid_hooks;
1735 uint8_t i, num_hooks = hweight32(hook_mask);
1736 uint8_t hooknum;
1737 struct nf_hook_ops *ops;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001738
Xiubo Lia6d0bae2016-06-02 10:59:56 +08001739 if (!num_hooks)
1740 return ERR_PTR(-EINVAL);
1741
Florian Westphal1ecc2812016-10-17 21:50:23 +02001742 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001743 if (ops == NULL)
1744 return ERR_PTR(-ENOMEM);
1745
1746 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1747 hook_mask >>= 1, ++hooknum) {
1748 if (!(hook_mask & 1))
1749 continue;
1750 ops[i].hook = fn;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001751 ops[i].pf = table->af;
1752 ops[i].hooknum = hooknum;
1753 ops[i].priority = table->priority;
1754 ++i;
1755 }
1756
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001757 return ops;
1758}
Florian Westphalb9e69e12016-02-25 10:08:36 +01001759EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001760
Florian Westphalfdacd57c2021-08-03 16:47:19 +02001761int xt_register_template(const struct xt_table *table,
1762 int (*table_init)(struct net *net))
1763{
1764 int ret = -EEXIST, af = table->af;
1765 struct xt_template *t;
1766
1767 mutex_lock(&xt[af].mutex);
1768
1769 list_for_each_entry(t, &xt_templates[af], list) {
1770 if (WARN_ON_ONCE(strcmp(table->name, t->name) == 0))
1771 goto out_unlock;
1772 }
1773
1774 ret = -ENOMEM;
1775 t = kzalloc(sizeof(*t), GFP_KERNEL);
1776 if (!t)
1777 goto out_unlock;
1778
1779 BUILD_BUG_ON(sizeof(t->name) != sizeof(table->name));
1780
1781 strscpy(t->name, table->name, sizeof(t->name));
1782 t->table_init = table_init;
1783 t->me = table->me;
1784 list_add(&t->list, &xt_templates[af]);
1785 ret = 0;
1786out_unlock:
1787 mutex_unlock(&xt[af].mutex);
1788 return ret;
1789}
1790EXPORT_SYMBOL_GPL(xt_register_template);
1791
1792void xt_unregister_template(const struct xt_table *table)
1793{
1794 struct xt_template *t;
1795 int af = table->af;
1796
1797 mutex_lock(&xt[af].mutex);
1798 list_for_each_entry(t, &xt_templates[af], list) {
1799 if (strcmp(table->name, t->name))
1800 continue;
1801
1802 list_del(&t->list);
1803 mutex_unlock(&xt[af].mutex);
1804 kfree(t);
1805 return;
1806 }
1807
1808 mutex_unlock(&xt[af].mutex);
1809 WARN_ON_ONCE(1);
1810}
1811EXPORT_SYMBOL_GPL(xt_unregister_template);
1812
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001813int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001814{
1815#ifdef CONFIG_PROC_FS
1816 char buf[XT_FUNCTION_MAXNAMELEN];
1817 struct proc_dir_entry *proc;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001818 kuid_t root_uid;
1819 kgid_t root_gid;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001820#endif
1821
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001822 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001823 return -EINVAL;
1824
1825
1826#ifdef CONFIG_PROC_FS
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001827 root_uid = make_kuid(net->user_ns, 0);
1828 root_gid = make_kgid(net->user_ns, 0);
1829
Tobias Klauserce18afe2007-03-14 16:36:16 -07001830 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001831 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Christoph Hellwigc3506372018-04-10 19:42:55 +02001832 proc = proc_create_net_data(buf, 0440, net->proc_net, &xt_table_seq_ops,
1833 sizeof(struct seq_net_private),
1834 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001835 if (!proc)
1836 goto out;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001837 if (uid_valid(root_uid) && gid_valid(root_gid))
1838 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001839
Tobias Klauserce18afe2007-03-14 16:36:16 -07001840 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001841 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Christoph Hellwig1cd67182018-04-15 10:36:56 +02001842 proc = proc_create_seq_private(buf, 0440, net->proc_net,
1843 &xt_match_seq_ops, sizeof(struct nf_mttg_trav),
1844 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001845 if (!proc)
1846 goto out_remove_tables;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001847 if (uid_valid(root_uid) && gid_valid(root_gid))
1848 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001849
Tobias Klauserce18afe2007-03-14 16:36:16 -07001850 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001851 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Christoph Hellwig1cd67182018-04-15 10:36:56 +02001852 proc = proc_create_seq_private(buf, 0440, net->proc_net,
1853 &xt_target_seq_ops, sizeof(struct nf_mttg_trav),
1854 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001855 if (!proc)
1856 goto out_remove_matches;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001857 if (uid_valid(root_uid) && gid_valid(root_gid))
1858 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001859#endif
1860
1861 return 0;
1862
1863#ifdef CONFIG_PROC_FS
1864out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001865 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001866 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001867 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001868
1869out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001870 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001871 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001872 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001873out:
1874 return -1;
1875#endif
1876}
1877EXPORT_SYMBOL_GPL(xt_proto_init);
1878
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001879void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001880{
1881#ifdef CONFIG_PROC_FS
1882 char buf[XT_FUNCTION_MAXNAMELEN];
1883
Tobias Klauserce18afe2007-03-14 16:36:16 -07001884 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001885 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001886 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001887
Tobias Klauserce18afe2007-03-14 16:36:16 -07001888 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001889 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001890 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001891
Tobias Klauserce18afe2007-03-14 16:36:16 -07001892 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001893 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001894 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001895#endif /*CONFIG_PROC_FS*/
1896}
1897EXPORT_SYMBOL_GPL(xt_proto_fini);
1898
Florian Westphalf28e15b2016-11-22 14:44:18 +01001899/**
1900 * xt_percpu_counter_alloc - allocate x_tables rule counter
1901 *
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001902 * @state: pointer to xt_percpu allocation state
Florian Westphalf28e15b2016-11-22 14:44:18 +01001903 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1904 *
1905 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1906 * contain the address of the real (percpu) counter.
1907 *
1908 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1909 * to fetch the real percpu counter.
1910 *
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001911 * To speed up allocation and improve data locality, a 4kb block is
Ben Hutchings9ba5c402018-03-29 15:12:41 +01001912 * allocated. Freeing any counter may free an entire block, so all
1913 * counters allocated using the same state must be freed at the same
1914 * time.
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001915 *
1916 * xt_percpu_counter_alloc_state contains the base address of the
1917 * allocated page and the current sub-offset.
1918 *
Florian Westphalf28e15b2016-11-22 14:44:18 +01001919 * returns false on error.
1920 */
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001921bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1922 struct xt_counters *counter)
Florian Westphalf28e15b2016-11-22 14:44:18 +01001923{
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001924 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
Florian Westphalf28e15b2016-11-22 14:44:18 +01001925
1926 if (nr_cpu_ids <= 1)
1927 return true;
1928
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001929 if (!state->mem) {
1930 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1931 XT_PCPU_BLOCK_SIZE);
1932 if (!state->mem)
1933 return false;
1934 }
1935 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1936 state->off += sizeof(*counter);
1937 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1938 state->mem = NULL;
1939 state->off = 0;
1940 }
Florian Westphalf28e15b2016-11-22 14:44:18 +01001941 return true;
1942}
1943EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1944
Florian Westphal4d31eef2016-11-22 14:44:17 +01001945void xt_percpu_counter_free(struct xt_counters *counters)
1946{
1947 unsigned long pcnt = counters->pcnt;
1948
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001949 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
Florian Westphal4d31eef2016-11-22 14:44:17 +01001950 free_percpu((void __percpu *)pcnt);
1951}
1952EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1953
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001954static int __net_init xt_net_init(struct net *net)
1955{
Florian Westphal1d610d42021-04-01 16:11:11 +02001956 struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001957 int i;
1958
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001959 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Florian Westphal1d610d42021-04-01 16:11:11 +02001960 INIT_LIST_HEAD(&xt_net->tables[i]);
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001961 return 0;
1962}
1963
Vasily Averin613d0772017-11-12 14:32:37 +03001964static void __net_exit xt_net_exit(struct net *net)
1965{
Florian Westphal1d610d42021-04-01 16:11:11 +02001966 struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
Vasily Averin613d0772017-11-12 14:32:37 +03001967 int i;
1968
1969 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Florian Westphal1d610d42021-04-01 16:11:11 +02001970 WARN_ON_ONCE(!list_empty(&xt_net->tables[i]));
Vasily Averin613d0772017-11-12 14:32:37 +03001971}
1972
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001973static struct pernet_operations xt_net_ops = {
1974 .init = xt_net_init,
Vasily Averin613d0772017-11-12 14:32:37 +03001975 .exit = xt_net_exit,
Florian Westphal1d610d42021-04-01 16:11:11 +02001976 .id = &xt_pernet_id,
1977 .size = sizeof(struct xt_pernet),
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001978};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001979
1980static int __init xt_init(void)
1981{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001982 unsigned int i;
1983 int rv;
1984
1985 for_each_possible_cpu(i) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001986 seqcount_init(&per_cpu(xt_recseq, i));
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001987 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001988
Francesco Ruggeri8d29d162019-02-10 11:58:29 -08001989 xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001990 if (!xt)
1991 return -ENOMEM;
1992
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001993 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001994 mutex_init(&xt[i].mutex);
Florian Westphal47a69592021-04-26 12:14:40 +02001995#ifdef CONFIG_NETFILTER_XTABLES_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -08001996 mutex_init(&xt[i].compat_mutex);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001997 xt[i].compat_tab = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001998#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001999 INIT_LIST_HEAD(&xt[i].target);
2000 INIT_LIST_HEAD(&xt[i].match);
Florian Westphalfdacd57c2021-08-03 16:47:19 +02002001 INIT_LIST_HEAD(&xt_templates[i]);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002002 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08002003 rv = register_pernet_subsys(&xt_net_ops);
2004 if (rv < 0)
2005 kfree(xt);
2006 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08002007}
2008
2009static void __exit xt_fini(void)
2010{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08002011 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08002012 kfree(xt);
2013}
2014
2015module_init(xt_init);
2016module_exit(xt_fini);
2017