blob: 1db69888cc14982516fb0cda5b4987ef0222c48f [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/*
2 * x_tables core - Backend for {ip,ip6,arp}_tables
3 *
4 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02005 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
Harald Welte2e4e6a12006-01-12 13:30:04 -08006 *
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 Engelhardtbe91fd52010-03-18 02:22:32 +010016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Harald Welte2e4e6a12006-01-12 13:30:04 -080017#include <linux/kernel.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040018#include <linux/module.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080019#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 Molnar9e19bb62006-03-25 01:41:10 -080025#include <linux/mutex.h>
Al Virod7fe0f22006-12-03 23:15:30 -050026#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Thomas Graffbabf312011-01-16 18:12:59 +010028#include <linux/audit.h>
Philip Whinerayf13f2ae2015-11-22 11:35:07 +000029#include <linux/user_namespace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080031
32#include <linux/netfilter/x_tables.h>
33#include <linux/netfilter_arp.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020034#include <linux/netfilter_ipv4/ip_tables.h>
35#include <linux/netfilter_ipv6/ip6_tables.h>
36#include <linux/netfilter_arp/arp_tables.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080037
Harald Welte2e4e6a12006-01-12 13:30:04 -080038MODULE_LICENSE("GPL");
39MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt043ef462008-10-08 11:35:15 +020040MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080041
Florian Westphaldb6a0cb2016-11-22 14:44:19 +010042#define XT_PCPU_BLOCK_SIZE 4096
43
Patrick McHardyb386d9f2007-12-17 21:47:48 -080044struct compat_delta {
Eric Dumazet255d0dc2010-12-18 18:35:15 +010045 unsigned int offset; /* offset in kernel */
46 int delta; /* delta in 32bit user land */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080047};
48
Harald Welte2e4e6a12006-01-12 13:30:04 -080049struct xt_af {
Ingo Molnar9e19bb62006-03-25 01:41:10 -080050 struct mutex mutex;
Harald Welte2e4e6a12006-01-12 13:30:04 -080051 struct list_head match;
52 struct list_head target;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080053#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080054 struct mutex compat_mutex;
Eric Dumazet255d0dc2010-12-18 18:35:15 +010055 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 McHardyb386d9f2007-12-17 21:47:48 -080058#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080059};
60
61static struct xt_af *xt;
62
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020063static 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 McHardy37f9f732006-03-20 17:59:06 -080069};
70
Harald Welte2e4e6a12006-01-12 13:30:04 -080071/* Registration hooks for targets. */
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020072int xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080073{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020074 u_int8_t af = target->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -080075
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020076 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080077 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080078 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020079 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -080080}
81EXPORT_SYMBOL(xt_register_target);
82
83void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080084xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080085{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020086 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080087
Ingo Molnar9e19bb62006-03-25 01:41:10 -080088 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070089 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080090 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080091}
92EXPORT_SYMBOL(xt_unregister_target);
93
94int
Patrick McHardy52d9c422006-08-22 00:33:45 -070095xt_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
107err:
108 if (i > 0)
109 xt_unregister_targets(target, i);
110 return err;
111}
112EXPORT_SYMBOL(xt_register_targets);
113
114void
115xt_unregister_targets(struct xt_target *target, unsigned int n)
116{
Changli Gaof68c5302010-10-04 22:24:12 +0200117 while (n-- > 0)
118 xt_unregister_target(&target[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700119}
120EXPORT_SYMBOL(xt_unregister_targets);
121
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200122int xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800123{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200124 u_int8_t af = match->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800125
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200126 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800127 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800128 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200129 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800130}
131EXPORT_SYMBOL(xt_register_match);
132
133void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800134xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800135{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200136 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800137
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800138 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700139 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800140 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800141}
142EXPORT_SYMBOL(xt_unregister_match);
143
Patrick McHardy52d9c422006-08-22 00:33:45 -0700144int
145xt_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
157err:
158 if (i > 0)
159 xt_unregister_matches(match, i);
160 return err;
161}
162EXPORT_SYMBOL(xt_register_matches);
163
164void
165xt_unregister_matches(struct xt_match *match, unsigned int n)
166{
Changli Gaof68c5302010-10-04 22:24:12 +0200167 while (n-- > 0)
168 xt_unregister_match(&match[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700169}
170EXPORT_SYMBOL(xt_unregister_matches);
171
Harald Welte2e4e6a12006-01-12 13:30:04 -0800172
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 Hemmingeradb00ae2011-03-09 14:14:26 +0100176 * function to use.
Harald Welte2e4e6a12006-01-12 13:30:04 -0800177 */
178
179/* Find match, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200180struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800181{
182 struct xt_match *m;
Patrick McHardy42046e22011-03-14 19:11:44 +0100183 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800184
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200185 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800186 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 Molnar9e19bb62006-03-25 01:41:10 -0800190 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800191 return m;
192 }
193 } else
194 err = -EPROTOTYPE; /* Found something. */
195 }
196 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800197 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200198
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 Welte2e4e6a12006-01-12 13:30:04 -0800203 return ERR_PTR(err);
204}
205EXPORT_SYMBOL(xt_find_match);
206
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200207struct xt_match *
208xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
209{
210 struct xt_match *match;
211
Eric Dumazetb39f3f32018-01-24 17:16:09 -0800212 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
213 return ERR_PTR(-EINVAL);
214
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100215 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 Engelhardtfd0ec0e2009-07-10 19:27:47 +0200222}
223EXPORT_SYMBOL_GPL(xt_request_find_match);
224
Harald Welte2e4e6a12006-01-12 13:30:04 -0800225/* Find target, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200226struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800227{
228 struct xt_target *t;
Patrick McHardy42046e22011-03-14 19:11:44 +0100229 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800230
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200231 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800232 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 Molnar9e19bb62006-03-25 01:41:10 -0800236 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800237 return t;
238 }
239 } else
240 err = -EPROTOTYPE; /* Found something. */
241 }
242 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800243 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200244
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 Welte2e4e6a12006-01-12 13:30:04 -0800249 return ERR_PTR(err);
250}
251EXPORT_SYMBOL(xt_find_target);
252
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200253struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800254{
255 struct xt_target *target;
256
Eric Dumazetb39f3f32018-01-24 17:16:09 -0800257 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
258 return ERR_PTR(-EINVAL);
259
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100260 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 Welte2e4e6a12006-01-12 13:30:04 -0800267}
268EXPORT_SYMBOL_GPL(xt_request_find_target);
269
Willem de Bruijn1e533302017-01-02 17:19:40 -0500270
271static int xt_obj_to_user(u16 __user *psize, u16 size,
272 void __user *pname, const char *name,
273 u8 __user *prev, u8 rev)
274{
275 if (put_user(size, psize))
276 return -EFAULT;
277 if (copy_to_user(pname, name, strlen(name) + 1))
278 return -EFAULT;
279 if (put_user(rev, prev))
280 return -EFAULT;
281
282 return 0;
283}
284
285#define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE) \
286 xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size, \
287 U->u.user.name, K->u.kernel.TYPE->name, \
288 &U->u.user.revision, K->u.kernel.TYPE->revision)
289
290int xt_data_to_user(void __user *dst, const void *src,
291 int usersize, int size)
292{
293 usersize = usersize ? : size;
294 if (copy_to_user(dst, src, usersize))
295 return -EFAULT;
296 if (usersize != size && clear_user(dst + usersize, size - usersize))
297 return -EFAULT;
298
299 return 0;
300}
301EXPORT_SYMBOL_GPL(xt_data_to_user);
302
303#define XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
304 xt_data_to_user(U->data, K->data, \
305 K->u.kernel.TYPE->usersize, \
306 C_SIZE ? : K->u.kernel.TYPE->TYPE##size)
307
308int xt_match_to_user(const struct xt_entry_match *m,
309 struct xt_entry_match __user *u)
310{
311 return XT_OBJ_TO_USER(u, m, match, 0) ||
312 XT_DATA_TO_USER(u, m, match, 0);
313}
314EXPORT_SYMBOL_GPL(xt_match_to_user);
315
316int xt_target_to_user(const struct xt_entry_target *t,
317 struct xt_entry_target __user *u)
318{
319 return XT_OBJ_TO_USER(u, t, target, 0) ||
320 XT_DATA_TO_USER(u, t, target, 0);
321}
322EXPORT_SYMBOL_GPL(xt_target_to_user);
323
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200324static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800325{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200326 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800327 int have_rev = 0;
328
329 list_for_each_entry(m, &xt[af].match, list) {
330 if (strcmp(m->name, name) == 0) {
331 if (m->revision > *bestp)
332 *bestp = m->revision;
333 if (m->revision == revision)
334 have_rev = 1;
335 }
336 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000337
338 if (af != NFPROTO_UNSPEC && !have_rev)
339 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
340
Harald Welte2e4e6a12006-01-12 13:30:04 -0800341 return have_rev;
342}
343
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200344static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800345{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200346 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800347 int have_rev = 0;
348
349 list_for_each_entry(t, &xt[af].target, list) {
350 if (strcmp(t->name, name) == 0) {
351 if (t->revision > *bestp)
352 *bestp = t->revision;
353 if (t->revision == revision)
354 have_rev = 1;
355 }
356 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000357
358 if (af != NFPROTO_UNSPEC && !have_rev)
359 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
360
Harald Welte2e4e6a12006-01-12 13:30:04 -0800361 return have_rev;
362}
363
364/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200365int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800366 int *err)
367{
368 int have_rev, best = -1;
369
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200370 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800371 if (target == 1)
372 have_rev = target_revfn(af, name, revision, &best);
373 else
374 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800375 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800376
377 /* Nothing at all? Return 0 to try loading module. */
378 if (best == -1) {
379 *err = -ENOENT;
380 return 0;
381 }
382
383 *err = best;
384 if (!have_rev)
385 *err = -EPROTONOSUPPORT;
386 return 1;
387}
388EXPORT_SYMBOL_GPL(xt_find_revision);
389
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000390static char *
391textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
Jan Engelhardt45185362009-04-05 10:43:09 +0200392{
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000393 static const char *const inetbr_names[] = {
Jan Engelhardt45185362009-04-05 10:43:09 +0200394 "PREROUTING", "INPUT", "FORWARD",
395 "OUTPUT", "POSTROUTING", "BROUTING",
396 };
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000397 static const char *const arp_names[] = {
398 "INPUT", "FORWARD", "OUTPUT",
399 };
400 const char *const *names;
401 unsigned int i, max;
Jan Engelhardt45185362009-04-05 10:43:09 +0200402 char *p = buf;
403 bool np = false;
404 int res;
405
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000406 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
407 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
408 ARRAY_SIZE(inetbr_names);
Jan Engelhardt45185362009-04-05 10:43:09 +0200409 *p = '\0';
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000410 for (i = 0; i < max; ++i) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200411 if (!(mask & (1 << i)))
412 continue;
413 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
414 if (res > 0) {
415 size -= res;
416 p += res;
417 }
418 np = true;
419 }
420
421 return buf;
422}
423
Florian Westphalc6ab7c62018-03-10 01:15:45 +0100424/**
425 * xt_check_proc_name - check that name is suitable for /proc file creation
426 *
427 * @name: file name candidate
428 * @size: length of buffer
429 *
430 * some x_tables modules wish to create a file in /proc.
431 * This function makes sure that the name is suitable for this
432 * purpose, it checks that name is NUL terminated and isn't a 'special'
433 * name, like "..".
434 *
435 * returns negative number on error or 0 if name is useable.
436 */
437int xt_check_proc_name(const char *name, unsigned int size)
438{
439 if (name[0] == '\0')
440 return -EINVAL;
441
442 if (strnlen(name, size) == size)
443 return -ENAMETOOLONG;
444
445 if (strcmp(name, ".") == 0 ||
446 strcmp(name, "..") == 0 ||
447 strchr(name, '/'))
448 return -EINVAL;
449
450 return 0;
451}
452EXPORT_SYMBOL(xt_check_proc_name);
453
Jan Engelhardt916a9172008-10-08 11:35:20 +0200454int xt_check_match(struct xt_mtchk_param *par,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200455 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800456{
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100457 int ret;
458
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200459 if (XT_ALIGN(par->match->matchsize) != size &&
460 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200461 /*
462 * ebt_among is exempt from centralized matchsize checking
463 * because it uses a dynamic-size data set.
464 */
Jan Engelhardtb4024052009-06-25 18:32:12 +0200465 pr_err("%s_tables: %s.%u match: invalid size "
466 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200467 xt_prefix[par->family], par->match->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200468 par->match->revision,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200469 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800470 return -EINVAL;
471 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200472 if (par->match->table != NULL &&
473 strcmp(par->match->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200474 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200475 xt_prefix[par->family], par->match->name,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200476 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800477 return -EINVAL;
478 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200479 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200480 char used[64], allow[64];
481
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200482 pr_err("%s_tables: %s match: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200483 "valid from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200484 xt_prefix[par->family], par->match->name,
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000485 textify_hooks(used, sizeof(used), par->hook_mask,
486 par->family),
487 textify_hooks(allow, sizeof(allow), par->match->hooks,
488 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800489 return -EINVAL;
490 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200491 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200492 pr_err("%s_tables: %s match: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200493 xt_prefix[par->family], par->match->name,
494 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800495 return -EINVAL;
496 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100497 if (par->match->checkentry != NULL) {
498 ret = par->match->checkentry(par);
499 if (ret < 0)
500 return ret;
501 else if (ret > 0)
502 /* Flag up potential errors. */
503 return -EIO;
504 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800505 return 0;
506}
507EXPORT_SYMBOL_GPL(xt_check_match);
508
Florian Westphal13631bf2016-04-01 14:17:29 +0200509/** xt_check_entry_match - check that matches end before start of target
510 *
511 * @match: beginning of xt_entry_match
512 * @target: beginning of this rules target (alleged end of matches)
513 * @alignment: alignment requirement of match structures
514 *
515 * Validates that all matches add up to the beginning of the target,
516 * and that each match covers at least the base structure size.
517 *
518 * Return: 0 on success, negative errno on failure.
519 */
520static int xt_check_entry_match(const char *match, const char *target,
521 const size_t alignment)
522{
523 const struct xt_entry_match *pos;
524 int length = target - match;
525
526 if (length == 0) /* no matches */
527 return 0;
528
529 pos = (struct xt_entry_match *)match;
530 do {
531 if ((unsigned long)pos % alignment)
532 return -EINVAL;
533
534 if (length < (int)sizeof(struct xt_entry_match))
535 return -EINVAL;
536
537 if (pos->u.match_size < sizeof(struct xt_entry_match))
538 return -EINVAL;
539
540 if (pos->u.match_size > length)
541 return -EINVAL;
542
543 length -= pos->u.match_size;
544 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
545 } while (length > 0);
546
547 return 0;
548}
549
Dmitry Mishin27229712006-04-01 02:25:19 -0800550#ifdef CONFIG_COMPAT
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100551int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800552{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100553 struct xt_af *xp = &xt[af];
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800554
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100555 if (!xp->compat_tab) {
556 if (!xp->number)
557 return -EINVAL;
558 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
559 if (!xp->compat_tab)
560 return -ENOMEM;
561 xp->cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800562 }
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100563
564 if (xp->cur >= xp->number)
565 return -EINVAL;
566
567 if (xp->cur)
568 delta += xp->compat_tab[xp->cur - 1].delta;
569 xp->compat_tab[xp->cur].offset = offset;
570 xp->compat_tab[xp->cur].delta = delta;
571 xp->cur++;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800572 return 0;
573}
574EXPORT_SYMBOL_GPL(xt_compat_add_offset);
575
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200576void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800577{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100578 if (xt[af].compat_tab) {
579 vfree(xt[af].compat_tab);
580 xt[af].compat_tab = NULL;
581 xt[af].number = 0;
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200582 xt[af].cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800583 }
584}
585EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
586
Florian Westphal3e5e5242010-02-15 18:17:10 +0100587int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800588{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100589 struct compat_delta *tmp = xt[af].compat_tab;
590 int mid, left = 0, right = xt[af].cur - 1;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800591
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100592 while (left <= right) {
593 mid = (left + right) >> 1;
594 if (offset > tmp[mid].offset)
595 left = mid + 1;
596 else if (offset < tmp[mid].offset)
597 right = mid - 1;
598 else
599 return mid ? tmp[mid - 1].delta : 0;
600 }
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200601 return left ? tmp[left - 1].delta : 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800602}
603EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
604
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100605void xt_compat_init_offsets(u_int8_t af, unsigned int number)
606{
607 xt[af].number = number;
608 xt[af].cur = 0;
609}
610EXPORT_SYMBOL(xt_compat_init_offsets);
611
Jan Engelhardt5452e422008-04-14 11:15:35 +0200612int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800613{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700614 u_int16_t csize = match->compatsize ? : match->matchsize;
615 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800616}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700617EXPORT_SYMBOL_GPL(xt_compat_match_offset);
618
Florian Westphal01883462016-04-01 14:17:33 +0200619void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
620 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700621{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200622 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700623 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
624 int pad, off = xt_compat_match_offset(match);
625 u_int16_t msize = cm->u.user.match_size;
Florian Westphal09d96862016-04-01 14:17:34 +0200626 char name[sizeof(m->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700627
628 m = *dstptr;
629 memcpy(m, cm, sizeof(*cm));
630 if (match->compat_from_user)
631 match->compat_from_user(m->data, cm->data);
632 else
633 memcpy(m->data, cm->data, msize - sizeof(*cm));
634 pad = XT_ALIGN(match->matchsize) - match->matchsize;
635 if (pad > 0)
636 memset(m->data + match->matchsize, 0, pad);
637
638 msize += off;
639 m->u.user.match_size = msize;
Florian Westphal09d96862016-04-01 14:17:34 +0200640 strlcpy(name, match->name, sizeof(name));
641 module_put(match->me);
642 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700643
644 *size += off;
645 *dstptr += msize;
646}
647EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
648
Jan Engelhardt739674f2009-06-26 08:23:19 +0200649int xt_compat_match_to_user(const struct xt_entry_match *m,
650 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700651{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200652 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700653 struct compat_xt_entry_match __user *cm = *dstptr;
654 int off = xt_compat_match_offset(match);
655 u_int16_t msize = m->u.user.match_size - off;
656
657 if (copy_to_user(cm, m, sizeof(*cm)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800658 put_user(msize, &cm->u.user.match_size) ||
659 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
660 strlen(m->u.kernel.match->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800661 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700662
663 if (match->compat_to_user) {
664 if (match->compat_to_user((void __user *)cm->data, m->data))
665 return -EFAULT;
666 } else {
667 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
668 return -EFAULT;
669 }
670
671 *size -= off;
672 *dstptr += msize;
673 return 0;
674}
675EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
Florian Westphalfc1221b2016-04-01 14:17:26 +0200676
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200677/* non-compat version may have padding after verdict */
678struct compat_xt_standard_target {
679 struct compat_xt_entry_target t;
680 compat_uint_t verdict;
681};
682
Florian Westphalce683e52016-04-01 14:17:28 +0200683int xt_compat_check_entry_offsets(const void *base, const char *elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +0200684 unsigned int target_offset,
685 unsigned int next_offset)
686{
Florian Westphalce683e52016-04-01 14:17:28 +0200687 long size_of_base_struct = elems - (const char *)base;
Florian Westphalfc1221b2016-04-01 14:17:26 +0200688 const struct compat_xt_entry_target *t;
689 const char *e = base;
690
Florian Westphalce683e52016-04-01 14:17:28 +0200691 if (target_offset < size_of_base_struct)
692 return -EINVAL;
693
Florian Westphalfc1221b2016-04-01 14:17:26 +0200694 if (target_offset + sizeof(*t) > next_offset)
695 return -EINVAL;
696
697 t = (void *)(e + target_offset);
698 if (t->u.target_size < sizeof(*t))
699 return -EINVAL;
700
701 if (target_offset + t->u.target_size > next_offset)
702 return -EINVAL;
703
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200704 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
Florian Westphal7b7eba02016-06-01 02:04:44 +0200705 COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200706 return -EINVAL;
707
Florian Westphal13631bf2016-04-01 14:17:29 +0200708 /* compat_xt_entry match has less strict aligment requirements,
709 * otherwise they are identical. In case of padding differences
710 * we need to add compat version of xt_check_entry_match.
711 */
712 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
713
714 return xt_check_entry_match(elems, base + target_offset,
715 __alignof__(struct compat_xt_entry_match));
Florian Westphalfc1221b2016-04-01 14:17:26 +0200716}
717EXPORT_SYMBOL(xt_compat_check_entry_offsets);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700718#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800719
Florian Westphal7d358122016-04-01 14:17:23 +0200720/**
721 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
722 *
723 * @base: pointer to arp/ip/ip6t_entry
Florian Westphalce683e52016-04-01 14:17:28 +0200724 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
Florian Westphal7d358122016-04-01 14:17:23 +0200725 * @target_offset: the arp/ip/ip6_t->target_offset
726 * @next_offset: the arp/ip/ip6_t->next_offset
727 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200728 * validates that target_offset and next_offset are sane and that all
729 * match sizes (if any) align with the target offset.
Florian Westphal7d358122016-04-01 14:17:23 +0200730 *
Florian Westphalce683e52016-04-01 14:17:28 +0200731 * This function does not validate the targets or matches themselves, it
Florian Westphal13631bf2016-04-01 14:17:29 +0200732 * only tests that all the offsets and sizes are correct, that all
733 * match structures are aligned, and that the last structure ends where
734 * the target structure begins.
735 *
736 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
Florian Westphalce683e52016-04-01 14:17:28 +0200737 *
Florian Westphal7d358122016-04-01 14:17:23 +0200738 * The arp/ip/ip6t_entry structure @base must have passed following tests:
739 * - it must point to a valid memory location
740 * - base to base + next_offset must be accessible, i.e. not exceed allocated
741 * length.
742 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200743 * A well-formed entry looks like this:
744 *
745 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
746 * e->elems[]-----' | |
747 * matchsize | |
748 * matchsize | |
749 * | |
750 * target_offset---------------------------------' |
751 * next_offset---------------------------------------------------'
752 *
753 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
754 * This is where matches (if any) and the target reside.
755 * target_offset: beginning of target.
756 * next_offset: start of the next rule; also: size of this rule.
757 * Since targets have a minimum size, target_offset + minlen <= next_offset.
758 *
759 * Every match stores its size, sum of sizes must not exceed target_offset.
760 *
Florian Westphal7d358122016-04-01 14:17:23 +0200761 * Return: 0 on success, negative errno on failure.
762 */
763int xt_check_entry_offsets(const void *base,
Florian Westphalce683e52016-04-01 14:17:28 +0200764 const char *elems,
Florian Westphal7d358122016-04-01 14:17:23 +0200765 unsigned int target_offset,
766 unsigned int next_offset)
767{
Florian Westphalce683e52016-04-01 14:17:28 +0200768 long size_of_base_struct = elems - (const char *)base;
Florian Westphal7d358122016-04-01 14:17:23 +0200769 const struct xt_entry_target *t;
770 const char *e = base;
771
Florian Westphalce683e52016-04-01 14:17:28 +0200772 /* target start is within the ip/ip6/arpt_entry struct */
773 if (target_offset < size_of_base_struct)
774 return -EINVAL;
775
Florian Westphal7d358122016-04-01 14:17:23 +0200776 if (target_offset + sizeof(*t) > next_offset)
777 return -EINVAL;
778
779 t = (void *)(e + target_offset);
Florian Westphala08e4e12016-04-01 14:17:25 +0200780 if (t->u.target_size < sizeof(*t))
781 return -EINVAL;
782
Florian Westphal7d358122016-04-01 14:17:23 +0200783 if (target_offset + t->u.target_size > next_offset)
784 return -EINVAL;
785
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200786 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
Florian Westphal7b7eba02016-06-01 02:04:44 +0200787 XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200788 return -EINVAL;
789
Florian Westphal13631bf2016-04-01 14:17:29 +0200790 return xt_check_entry_match(elems, base + target_offset,
791 __alignof__(struct xt_entry_match));
Florian Westphal7d358122016-04-01 14:17:23 +0200792}
793EXPORT_SYMBOL(xt_check_entry_offsets);
794
Florian Westphalf4dc7772016-07-14 17:51:26 +0200795/**
796 * xt_alloc_entry_offsets - allocate array to store rule head offsets
797 *
798 * @size: number of entries
799 *
800 * Return: NULL or kmalloc'd or vmalloc'd array
801 */
802unsigned int *xt_alloc_entry_offsets(unsigned int size)
803{
804 unsigned int *off;
805
806 off = kcalloc(size, sizeof(unsigned int), GFP_KERNEL | __GFP_NOWARN);
807
808 if (off)
809 return off;
810
811 if (size < (SIZE_MAX / sizeof(unsigned int)))
812 off = vmalloc(size * sizeof(unsigned int));
813
814 return off;
815}
816EXPORT_SYMBOL(xt_alloc_entry_offsets);
817
818/**
819 * xt_find_jump_offset - check if target is a valid jump offset
820 *
821 * @offsets: array containing all valid rule start offsets of a rule blob
822 * @target: the jump target to search for
823 * @size: entries in @offset
824 */
825bool xt_find_jump_offset(const unsigned int *offsets,
826 unsigned int target, unsigned int size)
827{
828 int m, low = 0, hi = size;
829
830 while (hi > low) {
831 m = (low + hi) / 2u;
832
833 if (offsets[m] > target)
834 hi = m;
835 else if (offsets[m] < target)
836 low = m + 1;
837 else
838 return true;
839 }
840
841 return false;
842}
843EXPORT_SYMBOL(xt_find_jump_offset);
844
Jan Engelhardt916a9172008-10-08 11:35:20 +0200845int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200846 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800847{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100848 int ret;
849
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200850 if (XT_ALIGN(par->target->targetsize) != size) {
Jan Engelhardtb4024052009-06-25 18:32:12 +0200851 pr_err("%s_tables: %s.%u target: invalid size "
852 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200853 xt_prefix[par->family], par->target->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200854 par->target->revision,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200855 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800856 return -EINVAL;
857 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200858 if (par->target->table != NULL &&
859 strcmp(par->target->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200860 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200861 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200862 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800863 return -EINVAL;
864 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200865 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200866 char used[64], allow[64];
867
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200868 pr_err("%s_tables: %s target: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200869 "usable from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200870 xt_prefix[par->family], par->target->name,
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000871 textify_hooks(used, sizeof(used), par->hook_mask,
872 par->family),
873 textify_hooks(allow, sizeof(allow), par->target->hooks,
874 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800875 return -EINVAL;
876 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200877 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200878 pr_err("%s_tables: %s target: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200879 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200880 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800881 return -EINVAL;
882 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100883 if (par->target->checkentry != NULL) {
884 ret = par->target->checkentry(par);
885 if (ret < 0)
886 return ret;
887 else if (ret > 0)
888 /* Flag up potential errors. */
889 return -EIO;
890 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800891 return 0;
892}
893EXPORT_SYMBOL_GPL(xt_check_target);
894
Florian Westphald7591f02016-04-01 15:37:59 +0200895/**
896 * xt_copy_counters_from_user - copy counters and metadata from userspace
897 *
898 * @user: src pointer to userspace memory
899 * @len: alleged size of userspace memory
900 * @info: where to store the xt_counters_info metadata
901 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
902 *
903 * Copies counter meta data from @user and stores it in @info.
904 *
905 * vmallocs memory to hold the counters, then copies the counter data
906 * from @user to the new memory and returns a pointer to it.
907 *
908 * If @compat is true, @info gets converted automatically to the 64bit
909 * representation.
910 *
911 * The metadata associated with the counters is stored in @info.
912 *
913 * Return: returns pointer that caller has to test via IS_ERR().
914 * If IS_ERR is false, caller has to vfree the pointer.
915 */
916void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
917 struct xt_counters_info *info, bool compat)
918{
919 void *mem;
920 u64 size;
921
922#ifdef CONFIG_COMPAT
923 if (compat) {
924 /* structures only differ in size due to alignment */
925 struct compat_xt_counters_info compat_tmp;
926
927 if (len <= sizeof(compat_tmp))
928 return ERR_PTR(-EINVAL);
929
930 len -= sizeof(compat_tmp);
931 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
932 return ERR_PTR(-EFAULT);
933
Eric Dumazet3ea051b2017-10-05 02:50:07 -0700934 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
Florian Westphald7591f02016-04-01 15:37:59 +0200935 info->num_counters = compat_tmp.num_counters;
936 user += sizeof(compat_tmp);
937 } else
938#endif
939 {
940 if (len <= sizeof(*info))
941 return ERR_PTR(-EINVAL);
942
943 len -= sizeof(*info);
944 if (copy_from_user(info, user, sizeof(*info)) != 0)
945 return ERR_PTR(-EFAULT);
946
Florian Westphald7591f02016-04-01 15:37:59 +0200947 user += sizeof(*info);
948 }
Eric Dumazet3ea051b2017-10-05 02:50:07 -0700949 info->name[sizeof(info->name) - 1] = '\0';
Florian Westphald7591f02016-04-01 15:37:59 +0200950
951 size = sizeof(struct xt_counters);
952 size *= info->num_counters;
953
954 if (size != (u64)len)
955 return ERR_PTR(-EINVAL);
956
957 mem = vmalloc(len);
958 if (!mem)
959 return ERR_PTR(-ENOMEM);
960
961 if (copy_from_user(mem, user, len) == 0)
962 return mem;
963
964 vfree(mem);
965 return ERR_PTR(-EFAULT);
966}
967EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
968
Dmitry Mishin27229712006-04-01 02:25:19 -0800969#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +0200970int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800971{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700972 u_int16_t csize = target->compatsize ? : target->targetsize;
973 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800974}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700975EXPORT_SYMBOL_GPL(xt_compat_target_offset);
976
977void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800978 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700979{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200980 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700981 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
982 int pad, off = xt_compat_target_offset(target);
983 u_int16_t tsize = ct->u.user.target_size;
Florian Westphal09d96862016-04-01 14:17:34 +0200984 char name[sizeof(t->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700985
986 t = *dstptr;
987 memcpy(t, ct, sizeof(*ct));
988 if (target->compat_from_user)
989 target->compat_from_user(t->data, ct->data);
990 else
991 memcpy(t->data, ct->data, tsize - sizeof(*ct));
992 pad = XT_ALIGN(target->targetsize) - target->targetsize;
993 if (pad > 0)
994 memset(t->data + target->targetsize, 0, pad);
995
996 tsize += off;
997 t->u.user.target_size = tsize;
Florian Westphal09d96862016-04-01 14:17:34 +0200998 strlcpy(name, target->name, sizeof(name));
999 module_put(target->me);
1000 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001001
1002 *size += off;
1003 *dstptr += tsize;
1004}
1005EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1006
Jan Engelhardt739674f2009-06-26 08:23:19 +02001007int xt_compat_target_to_user(const struct xt_entry_target *t,
1008 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001009{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001010 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001011 struct compat_xt_entry_target __user *ct = *dstptr;
1012 int off = xt_compat_target_offset(target);
1013 u_int16_t tsize = t->u.user.target_size - off;
1014
1015 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -08001016 put_user(tsize, &ct->u.user.target_size) ||
1017 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
1018 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001019 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001020
1021 if (target->compat_to_user) {
1022 if (target->compat_to_user((void __user *)ct->data, t->data))
1023 return -EFAULT;
1024 } else {
1025 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
1026 return -EFAULT;
1027 }
1028
1029 *size -= off;
1030 *dstptr += tsize;
1031 return 0;
1032}
1033EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -08001034#endif
1035
Harald Welte2e4e6a12006-01-12 13:30:04 -08001036struct xt_table_info *xt_alloc_table_info(unsigned int size)
1037{
Eric Dumazet711bdde2015-06-15 09:57:30 -07001038 struct xt_table_info *info = NULL;
1039 size_t sz = sizeof(*info) + size;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001040
Florian Westphald157bd72016-03-10 01:56:23 +01001041 if (sz < sizeof(*info))
1042 return NULL;
1043
Harald Welte2e4e6a12006-01-12 13:30:04 -08001044 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
Dmitry Vyukov1099c702017-12-28 09:48:54 +01001045 if ((size >> PAGE_SHIFT) + 2 > totalram_pages)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001046 return NULL;
1047
Eric Dumazet711bdde2015-06-15 09:57:30 -07001048 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
1049 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
1050 if (!info) {
1051 info = vmalloc(sz);
1052 if (!info)
1053 return NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001054 }
Eric Dumazet711bdde2015-06-15 09:57:30 -07001055 memset(info, 0, sizeof(*info));
1056 info->size = size;
1057 return info;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001058}
1059EXPORT_SYMBOL(xt_alloc_table_info);
1060
1061void xt_free_table_info(struct xt_table_info *info)
1062{
1063 int cpu;
1064
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001065 if (info->jumpstack != NULL) {
Eric Dumazetf6b50822014-06-24 02:15:35 -07001066 for_each_possible_cpu(cpu)
1067 kvfree(info->jumpstack[cpu]);
1068 kvfree(info->jumpstack);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001069 }
1070
Eric Dumazet711bdde2015-06-15 09:57:30 -07001071 kvfree(info);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001072}
1073EXPORT_SYMBOL(xt_free_table_info);
1074
1075/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001076struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1077 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001078{
Florian Westphalb9e69e12016-02-25 10:08:36 +01001079 struct xt_table *t, *found = NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001080
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001081 mutex_lock(&xt[af].mutex);
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001082 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001083 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1084 return t;
Florian Westphalb9e69e12016-02-25 10:08:36 +01001085
1086 if (net == &init_net)
1087 goto out;
1088
1089 /* Table doesn't exist in this netns, re-try init */
1090 list_for_each_entry(t, &init_net.xt.tables[af], list) {
1091 if (strcmp(t->name, name))
1092 continue;
Dan Carpenterc70c7be2017-04-28 15:57:56 +03001093 if (!try_module_get(t->me)) {
1094 mutex_unlock(&xt[af].mutex);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001095 return NULL;
Dan Carpenterc70c7be2017-04-28 15:57:56 +03001096 }
Florian Westphalb9e69e12016-02-25 10:08:36 +01001097
1098 mutex_unlock(&xt[af].mutex);
1099 if (t->table_init(net) != 0) {
1100 module_put(t->me);
1101 return NULL;
1102 }
1103
1104 found = t;
1105
1106 mutex_lock(&xt[af].mutex);
1107 break;
1108 }
1109
1110 if (!found)
1111 goto out;
1112
1113 /* and once again: */
1114 list_for_each_entry(t, &net->xt.tables[af], list)
1115 if (strcmp(t->name, name) == 0)
1116 return t;
1117
1118 module_put(found->me);
1119 out:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001120 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001121 return NULL;
1122}
1123EXPORT_SYMBOL_GPL(xt_find_table_lock);
1124
1125void xt_table_unlock(struct xt_table *table)
1126{
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001127 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001128}
1129EXPORT_SYMBOL_GPL(xt_table_unlock);
1130
Dmitry Mishin27229712006-04-01 02:25:19 -08001131#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001132void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001133{
1134 mutex_lock(&xt[af].compat_mutex);
1135}
1136EXPORT_SYMBOL_GPL(xt_compat_lock);
1137
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001138void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001139{
1140 mutex_unlock(&xt[af].compat_mutex);
1141}
1142EXPORT_SYMBOL_GPL(xt_compat_unlock);
1143#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001144
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001145DEFINE_PER_CPU(seqcount_t, xt_recseq);
1146EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001147
Florian Westphaldcebd312015-07-14 17:51:09 +02001148struct static_key xt_tee_enabled __read_mostly;
1149EXPORT_SYMBOL_GPL(xt_tee_enabled);
1150
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001151static int xt_jumpstack_alloc(struct xt_table_info *i)
1152{
1153 unsigned int size;
1154 int cpu;
1155
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001156 size = sizeof(void **) * nr_cpu_ids;
1157 if (size > PAGE_SIZE)
Joe Perches3dbd4432011-05-28 10:36:35 -07001158 i->jumpstack = vzalloc(size);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001159 else
Joe Perches3dbd4432011-05-28 10:36:35 -07001160 i->jumpstack = kzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001161 if (i->jumpstack == NULL)
1162 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001163
Florian Westphal98d1bd82015-07-14 17:51:06 +02001164 /* ruleset without jumps -- no stack needed */
1165 if (i->stacksize == 0)
1166 return 0;
1167
Florian Westphal7814b6e2015-07-14 17:51:08 +02001168 /* Jumpstack needs to be able to record two full callchains, one
1169 * from the first rule set traversal, plus one table reentrancy
1170 * via -j TEE without clobbering the callchain that brought us to
1171 * TEE target.
1172 *
1173 * This is done by allocating two jumpstacks per cpu, on reentry
1174 * the upper half of the stack is used.
1175 *
1176 * see the jumpstack setup in ipt_do_table() for more details.
1177 */
1178 size = sizeof(void *) * i->stacksize * 2u;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001179 for_each_possible_cpu(cpu) {
1180 if (size > PAGE_SIZE)
1181 i->jumpstack[cpu] = vmalloc_node(size,
1182 cpu_to_node(cpu));
1183 else
1184 i->jumpstack[cpu] = kmalloc_node(size,
1185 GFP_KERNEL, cpu_to_node(cpu));
1186 if (i->jumpstack[cpu] == NULL)
1187 /*
1188 * Freeing will be done later on by the callers. The
1189 * chain is: xt_replace_table -> __do_replace ->
1190 * do_replace -> xt_free_table_info.
1191 */
1192 return -ENOMEM;
1193 }
1194
1195 return 0;
1196}
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001197
Harald Welte2e4e6a12006-01-12 13:30:04 -08001198struct xt_table_info *
1199xt_replace_table(struct xt_table *table,
1200 unsigned int num_counters,
1201 struct xt_table_info *newinfo,
1202 int *error)
1203{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001204 struct xt_table_info *private;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001205 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001206
Jan Engelhardtd97a9e42010-04-21 14:45:51 +02001207 ret = xt_jumpstack_alloc(newinfo);
1208 if (ret < 0) {
1209 *error = ret;
1210 return NULL;
1211 }
1212
Harald Welte2e4e6a12006-01-12 13:30:04 -08001213 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001214 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001215 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001216
Harald Welte2e4e6a12006-01-12 13:30:04 -08001217 /* Check inside lock: is the old number correct? */
1218 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001219 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001220 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001221 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001222 *error = -EAGAIN;
1223 return NULL;
1224 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001225
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001226 newinfo->initial_entries = private->initial_entries;
Will Deaconb416c142013-10-21 13:14:53 +01001227 /*
1228 * Ensure contents of newinfo are visible before assigning to
1229 * private.
1230 */
1231 smp_wmb();
1232 table->private = newinfo;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001233
1234 /*
1235 * Even though table entries have now been swapped, other CPU's
1236 * may still be using the old entries. This is okay, because
1237 * resynchronization happens because of the locking done
1238 * during the get_counters() routine.
1239 */
1240 local_bh_enable();
1241
Thomas Graffbabf312011-01-16 18:12:59 +01001242#ifdef CONFIG_AUDIT
1243 if (audit_enabled) {
1244 struct audit_buffer *ab;
1245
1246 ab = audit_log_start(current->audit_context, GFP_KERNEL,
1247 AUDIT_NETFILTER_CFG);
1248 if (ab) {
1249 audit_log_format(ab, "table=%s family=%u entries=%u",
1250 table->name, table->af,
1251 private->number);
1252 audit_log_end(ab);
1253 }
1254 }
1255#endif
1256
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001257 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001258}
1259EXPORT_SYMBOL_GPL(xt_replace_table);
1260
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001261struct xt_table *xt_register_table(struct net *net,
1262 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -08001263 struct xt_table_info *bootstrap,
1264 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001265{
1266 int ret;
1267 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001268 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001269
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001270 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001271 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001272 if (!table) {
1273 ret = -ENOMEM;
1274 goto out;
1275 }
1276
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001277 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001278 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001279 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -07001280 if (strcmp(t->name, table->name) == 0) {
1281 ret = -EEXIST;
1282 goto unlock;
1283 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001284 }
1285
1286 /* Simplifies replace_table code. */
1287 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +01001288
Harald Welte2e4e6a12006-01-12 13:30:04 -08001289 if (!xt_replace_table(table, 0, newinfo, &ret))
1290 goto unlock;
1291
1292 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001293 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001294
1295 /* save number of initial entries */
1296 private->initial_entries = private->number;
1297
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001298 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001299 mutex_unlock(&xt[table->af].mutex);
1300 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001301
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001302unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001303 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001304 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001305out:
1306 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001307}
1308EXPORT_SYMBOL_GPL(xt_register_table);
1309
1310void *xt_unregister_table(struct xt_table *table)
1311{
1312 struct xt_table_info *private;
1313
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001314 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001315 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -07001316 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001317 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001318 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001319
1320 return private;
1321}
1322EXPORT_SYMBOL_GPL(xt_unregister_table);
1323
1324#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001325struct xt_names_priv {
1326 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001327 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001328};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001329static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001330{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001331 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001332 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001333 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001334
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001335 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001336 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001337}
1338
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001339static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001340{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001341 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001342 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001343 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001344
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001345 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001346}
1347
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001348static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001349{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001350 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001351 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001352
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001353 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001354}
1355
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001356static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001357{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001358 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001359
Joe Perches861fb102015-05-12 18:28:23 -07001360 if (*table->name)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -04001361 seq_printf(seq, "%s\n", table->name);
Joe Perches861fb102015-05-12 18:28:23 -07001362 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001363}
1364
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001365static const struct seq_operations xt_table_seq_ops = {
1366 .start = xt_table_seq_start,
1367 .next = xt_table_seq_next,
1368 .stop = xt_table_seq_stop,
1369 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001370};
1371
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001372static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001373{
1374 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001375 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001376
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001377 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1378 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001379 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001380 priv = ((struct seq_file *)file->private_data)->private;
Al Virod9dda782013-03-31 18:16:14 -04001381 priv->af = (unsigned long)PDE_DATA(inode);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001382 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001383 return ret;
1384}
1385
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001386static const struct file_operations xt_table_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001387 .owner = THIS_MODULE,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001388 .open = xt_table_open,
1389 .read = seq_read,
1390 .llseek = seq_lseek,
Pavel Emelyanov0e93bb92008-04-29 03:15:35 -07001391 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001392};
1393
Jan Engelhardteb132202009-02-18 16:42:19 +01001394/*
1395 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1396 * the multi-AF mutexes.
1397 */
1398struct nf_mttg_trav {
1399 struct list_head *head, *curr;
1400 uint8_t class, nfproto;
1401};
1402
1403enum {
1404 MTTG_TRAV_INIT,
1405 MTTG_TRAV_NFP_UNSPEC,
1406 MTTG_TRAV_NFP_SPEC,
1407 MTTG_TRAV_DONE,
1408};
1409
1410static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1411 bool is_target)
1412{
1413 static const uint8_t next_class[] = {
1414 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1415 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1416 };
1417 struct nf_mttg_trav *trav = seq->private;
1418
1419 switch (trav->class) {
1420 case MTTG_TRAV_INIT:
1421 trav->class = MTTG_TRAV_NFP_UNSPEC;
1422 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1423 trav->head = trav->curr = is_target ?
1424 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1425 break;
1426 case MTTG_TRAV_NFP_UNSPEC:
1427 trav->curr = trav->curr->next;
1428 if (trav->curr != trav->head)
1429 break;
1430 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1431 mutex_lock(&xt[trav->nfproto].mutex);
1432 trav->head = trav->curr = is_target ?
1433 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1434 trav->class = next_class[trav->class];
1435 break;
1436 case MTTG_TRAV_NFP_SPEC:
1437 trav->curr = trav->curr->next;
1438 if (trav->curr != trav->head)
1439 break;
1440 /* fallthru, _stop will unlock */
1441 default:
1442 return NULL;
1443 }
1444
1445 if (ppos != NULL)
1446 ++*ppos;
1447 return trav;
1448}
1449
1450static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1451 bool is_target)
1452{
1453 struct nf_mttg_trav *trav = seq->private;
1454 unsigned int j;
1455
1456 trav->class = MTTG_TRAV_INIT;
1457 for (j = 0; j < *pos; ++j)
1458 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1459 return NULL;
1460 return trav;
1461}
1462
1463static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1464{
1465 struct nf_mttg_trav *trav = seq->private;
1466
1467 switch (trav->class) {
1468 case MTTG_TRAV_NFP_UNSPEC:
1469 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1470 break;
1471 case MTTG_TRAV_NFP_SPEC:
1472 mutex_unlock(&xt[trav->nfproto].mutex);
1473 break;
1474 }
1475}
1476
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001477static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1478{
Jan Engelhardteb132202009-02-18 16:42:19 +01001479 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001480}
1481
Jan Engelhardteb132202009-02-18 16:42:19 +01001482static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001483{
Jan Engelhardteb132202009-02-18 16:42:19 +01001484 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001485}
1486
1487static int xt_match_seq_show(struct seq_file *seq, void *v)
1488{
Jan Engelhardteb132202009-02-18 16:42:19 +01001489 const struct nf_mttg_trav *trav = seq->private;
1490 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001491
Jan Engelhardteb132202009-02-18 16:42:19 +01001492 switch (trav->class) {
1493 case MTTG_TRAV_NFP_UNSPEC:
1494 case MTTG_TRAV_NFP_SPEC:
1495 if (trav->curr == trav->head)
1496 return 0;
1497 match = list_entry(trav->curr, struct xt_match, list);
Joe Perches861fb102015-05-12 18:28:23 -07001498 if (*match->name)
1499 seq_printf(seq, "%s\n", match->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001500 }
1501 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001502}
1503
1504static const struct seq_operations xt_match_seq_ops = {
1505 .start = xt_match_seq_start,
1506 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001507 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001508 .show = xt_match_seq_show,
1509};
1510
1511static int xt_match_open(struct inode *inode, struct file *file)
1512{
Jan Engelhardteb132202009-02-18 16:42:19 +01001513 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001514 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1515 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001516 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001517
Al Virod9dda782013-03-31 18:16:14 -04001518 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001519 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001520}
1521
1522static const struct file_operations xt_match_ops = {
1523 .owner = THIS_MODULE,
1524 .open = xt_match_open,
1525 .read = seq_read,
1526 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001527 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001528};
1529
1530static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1531{
Jan Engelhardteb132202009-02-18 16:42:19 +01001532 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001533}
1534
Jan Engelhardteb132202009-02-18 16:42:19 +01001535static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001536{
Jan Engelhardteb132202009-02-18 16:42:19 +01001537 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001538}
1539
1540static int xt_target_seq_show(struct seq_file *seq, void *v)
1541{
Jan Engelhardteb132202009-02-18 16:42:19 +01001542 const struct nf_mttg_trav *trav = seq->private;
1543 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001544
Jan Engelhardteb132202009-02-18 16:42:19 +01001545 switch (trav->class) {
1546 case MTTG_TRAV_NFP_UNSPEC:
1547 case MTTG_TRAV_NFP_SPEC:
1548 if (trav->curr == trav->head)
1549 return 0;
1550 target = list_entry(trav->curr, struct xt_target, list);
Joe Perches861fb102015-05-12 18:28:23 -07001551 if (*target->name)
1552 seq_printf(seq, "%s\n", target->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001553 }
1554 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001555}
1556
1557static const struct seq_operations xt_target_seq_ops = {
1558 .start = xt_target_seq_start,
1559 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001560 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001561 .show = xt_target_seq_show,
1562};
1563
1564static int xt_target_open(struct inode *inode, struct file *file)
1565{
Jan Engelhardteb132202009-02-18 16:42:19 +01001566 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001567 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1568 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001569 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001570
Al Virod9dda782013-03-31 18:16:14 -04001571 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001572 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001573}
1574
1575static const struct file_operations xt_target_ops = {
1576 .owner = THIS_MODULE,
1577 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001578 .read = seq_read,
1579 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001580 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001581};
1582
1583#define FORMAT_TABLES "_tables_names"
1584#define FORMAT_MATCHES "_tables_matches"
1585#define FORMAT_TARGETS "_tables_targets"
1586
1587#endif /* CONFIG_PROC_FS */
1588
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001589/**
Florian Westphalb9e69e12016-02-25 10:08:36 +01001590 * xt_hook_ops_alloc - set up hooks for a new table
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001591 * @table: table with metadata needed to set up hooks
1592 * @fn: Hook function
1593 *
Florian Westphalb9e69e12016-02-25 10:08:36 +01001594 * This function will create the nf_hook_ops that the x_table needs
1595 * to hand to xt_hook_link_net().
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001596 */
Florian Westphalb9e69e12016-02-25 10:08:36 +01001597struct nf_hook_ops *
1598xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001599{
1600 unsigned int hook_mask = table->valid_hooks;
1601 uint8_t i, num_hooks = hweight32(hook_mask);
1602 uint8_t hooknum;
1603 struct nf_hook_ops *ops;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001604
Xiubo Lia6d0bae2016-06-02 10:59:56 +08001605 if (!num_hooks)
1606 return ERR_PTR(-EINVAL);
1607
Florian Westphal1ecc2812016-10-17 21:50:23 +02001608 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001609 if (ops == NULL)
1610 return ERR_PTR(-ENOMEM);
1611
1612 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1613 hook_mask >>= 1, ++hooknum) {
1614 if (!(hook_mask & 1))
1615 continue;
1616 ops[i].hook = fn;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001617 ops[i].pf = table->af;
1618 ops[i].hooknum = hooknum;
1619 ops[i].priority = table->priority;
1620 ++i;
1621 }
1622
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001623 return ops;
1624}
Florian Westphalb9e69e12016-02-25 10:08:36 +01001625EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001626
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001627int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001628{
1629#ifdef CONFIG_PROC_FS
1630 char buf[XT_FUNCTION_MAXNAMELEN];
1631 struct proc_dir_entry *proc;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001632 kuid_t root_uid;
1633 kgid_t root_gid;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001634#endif
1635
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001636 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001637 return -EINVAL;
1638
1639
1640#ifdef CONFIG_PROC_FS
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001641 root_uid = make_kuid(net->user_ns, 0);
1642 root_gid = make_kgid(net->user_ns, 0);
1643
Tobias Klauserce18afe2007-03-14 16:36:16 -07001644 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001645 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001646 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1647 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001648 if (!proc)
1649 goto out;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001650 if (uid_valid(root_uid) && gid_valid(root_gid))
1651 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001652
Tobias Klauserce18afe2007-03-14 16:36:16 -07001653 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001654 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001655 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1656 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001657 if (!proc)
1658 goto out_remove_tables;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001659 if (uid_valid(root_uid) && gid_valid(root_gid))
1660 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001661
Tobias Klauserce18afe2007-03-14 16:36:16 -07001662 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001663 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001664 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1665 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001666 if (!proc)
1667 goto out_remove_matches;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001668 if (uid_valid(root_uid) && gid_valid(root_gid))
1669 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001670#endif
1671
1672 return 0;
1673
1674#ifdef CONFIG_PROC_FS
1675out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001676 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001677 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001678 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001679
1680out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001681 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001682 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001683 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001684out:
1685 return -1;
1686#endif
1687}
1688EXPORT_SYMBOL_GPL(xt_proto_init);
1689
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001690void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001691{
1692#ifdef CONFIG_PROC_FS
1693 char buf[XT_FUNCTION_MAXNAMELEN];
1694
Tobias Klauserce18afe2007-03-14 16:36:16 -07001695 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001696 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001697 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001698
Tobias Klauserce18afe2007-03-14 16:36:16 -07001699 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001700 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001701 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001702
Tobias Klauserce18afe2007-03-14 16:36:16 -07001703 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001704 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001705 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001706#endif /*CONFIG_PROC_FS*/
1707}
1708EXPORT_SYMBOL_GPL(xt_proto_fini);
1709
Florian Westphaldac44482016-11-22 14:44:18 +01001710/**
1711 * xt_percpu_counter_alloc - allocate x_tables rule counter
1712 *
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001713 * @state: pointer to xt_percpu allocation state
Florian Westphaldac44482016-11-22 14:44:18 +01001714 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1715 *
1716 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1717 * contain the address of the real (percpu) counter.
1718 *
1719 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1720 * to fetch the real percpu counter.
1721 *
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001722 * To speed up allocation and improve data locality, a 4kb block is
1723 * allocated.
1724 *
1725 * xt_percpu_counter_alloc_state contains the base address of the
1726 * allocated page and the current sub-offset.
1727 *
Florian Westphaldac44482016-11-22 14:44:18 +01001728 * returns false on error.
1729 */
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001730bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1731 struct xt_counters *counter)
Florian Westphaldac44482016-11-22 14:44:18 +01001732{
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001733 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
Florian Westphaldac44482016-11-22 14:44:18 +01001734
1735 if (nr_cpu_ids <= 1)
1736 return true;
1737
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001738 if (!state->mem) {
1739 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1740 XT_PCPU_BLOCK_SIZE);
1741 if (!state->mem)
1742 return false;
1743 }
1744 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1745 state->off += sizeof(*counter);
1746 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1747 state->mem = NULL;
1748 state->off = 0;
1749 }
Florian Westphaldac44482016-11-22 14:44:18 +01001750 return true;
1751}
1752EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1753
Florian Westphal61346e22016-11-22 14:44:17 +01001754void xt_percpu_counter_free(struct xt_counters *counters)
1755{
1756 unsigned long pcnt = counters->pcnt;
1757
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001758 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
Florian Westphal61346e22016-11-22 14:44:17 +01001759 free_percpu((void __percpu *)pcnt);
1760}
1761EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1762
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001763static int __net_init xt_net_init(struct net *net)
1764{
1765 int i;
1766
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001767 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001768 INIT_LIST_HEAD(&net->xt.tables[i]);
1769 return 0;
1770}
1771
1772static struct pernet_operations xt_net_ops = {
1773 .init = xt_net_init,
1774};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001775
1776static int __init xt_init(void)
1777{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001778 unsigned int i;
1779 int rv;
1780
1781 for_each_possible_cpu(i) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001782 seqcount_init(&per_cpu(xt_recseq, i));
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001783 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001784
Francesco Ruggeri9b04b512019-05-10 09:19:30 -07001785 xt = kcalloc(NFPROTO_NUMPROTO, sizeof(struct xt_af), GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001786 if (!xt)
1787 return -ENOMEM;
1788
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001789 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001790 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001791#ifdef CONFIG_COMPAT
1792 mutex_init(&xt[i].compat_mutex);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001793 xt[i].compat_tab = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001794#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001795 INIT_LIST_HEAD(&xt[i].target);
1796 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001797 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001798 rv = register_pernet_subsys(&xt_net_ops);
1799 if (rv < 0)
1800 kfree(xt);
1801 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001802}
1803
1804static void __exit xt_fini(void)
1805{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001806 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001807 kfree(xt);
1808}
1809
1810module_init(xt_init);
1811module_exit(xt_fini);
1812