blob: 5d8ba89a8da84099f2be7a2a1dc1e7d76bd75ec7 [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 Westphalae0ac0e2016-11-22 14:44:19 +010042#define XT_PCPU_BLOCK_SIZE 4096
Harald Welte2e4e6a12006-01-12 13:30:04 -080043
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 Dumazetda17c732018-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 Dumazetda17c732018-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 Bruijnf32815d2017-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,
Willem de Bruijn324318f2017-05-09 16:17:37 -0400291 int usersize, int size, int aligned_size)
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500292{
293 usersize = usersize ? : size;
294 if (copy_to_user(dst, src, usersize))
295 return -EFAULT;
Willem de Bruijn324318f2017-05-09 16:17:37 -0400296 if (usersize != aligned_size &&
297 clear_user(dst + usersize, aligned_size - usersize))
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500298 return -EFAULT;
299
300 return 0;
301}
302EXPORT_SYMBOL_GPL(xt_data_to_user);
303
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400304#define XT_DATA_TO_USER(U, K, TYPE) \
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500305 xt_data_to_user(U->data, K->data, \
306 K->u.kernel.TYPE->usersize, \
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400307 K->u.kernel.TYPE->TYPE##size, \
308 XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500309
310int xt_match_to_user(const struct xt_entry_match *m,
311 struct xt_entry_match __user *u)
312{
313 return XT_OBJ_TO_USER(u, m, match, 0) ||
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400314 XT_DATA_TO_USER(u, m, match);
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500315}
316EXPORT_SYMBOL_GPL(xt_match_to_user);
317
318int xt_target_to_user(const struct xt_entry_target *t,
319 struct xt_entry_target __user *u)
320{
321 return XT_OBJ_TO_USER(u, t, target, 0) ||
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400322 XT_DATA_TO_USER(u, t, target);
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500323}
324EXPORT_SYMBOL_GPL(xt_target_to_user);
325
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200326static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800327{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200328 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800329 int have_rev = 0;
330
331 list_for_each_entry(m, &xt[af].match, list) {
332 if (strcmp(m->name, name) == 0) {
333 if (m->revision > *bestp)
334 *bestp = m->revision;
335 if (m->revision == revision)
336 have_rev = 1;
337 }
338 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000339
340 if (af != NFPROTO_UNSPEC && !have_rev)
341 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
342
Harald Welte2e4e6a12006-01-12 13:30:04 -0800343 return have_rev;
344}
345
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200346static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800347{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200348 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800349 int have_rev = 0;
350
351 list_for_each_entry(t, &xt[af].target, list) {
352 if (strcmp(t->name, name) == 0) {
353 if (t->revision > *bestp)
354 *bestp = t->revision;
355 if (t->revision == revision)
356 have_rev = 1;
357 }
358 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000359
360 if (af != NFPROTO_UNSPEC && !have_rev)
361 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
362
Harald Welte2e4e6a12006-01-12 13:30:04 -0800363 return have_rev;
364}
365
366/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200367int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800368 int *err)
369{
370 int have_rev, best = -1;
371
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200372 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800373 if (target == 1)
374 have_rev = target_revfn(af, name, revision, &best);
375 else
376 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800377 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800378
379 /* Nothing at all? Return 0 to try loading module. */
380 if (best == -1) {
381 *err = -ENOENT;
382 return 0;
383 }
384
385 *err = best;
386 if (!have_rev)
387 *err = -EPROTONOSUPPORT;
388 return 1;
389}
390EXPORT_SYMBOL_GPL(xt_find_revision);
391
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000392static char *
393textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
Jan Engelhardt45185362009-04-05 10:43:09 +0200394{
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000395 static const char *const inetbr_names[] = {
Jan Engelhardt45185362009-04-05 10:43:09 +0200396 "PREROUTING", "INPUT", "FORWARD",
397 "OUTPUT", "POSTROUTING", "BROUTING",
398 };
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000399 static const char *const arp_names[] = {
400 "INPUT", "FORWARD", "OUTPUT",
401 };
402 const char *const *names;
403 unsigned int i, max;
Jan Engelhardt45185362009-04-05 10:43:09 +0200404 char *p = buf;
405 bool np = false;
406 int res;
407
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000408 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
409 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
410 ARRAY_SIZE(inetbr_names);
Jan Engelhardt45185362009-04-05 10:43:09 +0200411 *p = '\0';
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000412 for (i = 0; i < max; ++i) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200413 if (!(mask & (1 << i)))
414 continue;
415 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
416 if (res > 0) {
417 size -= res;
418 p += res;
419 }
420 np = true;
421 }
422
423 return buf;
424}
425
Jan Engelhardt916a9172008-10-08 11:35:20 +0200426int xt_check_match(struct xt_mtchk_param *par,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200427 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800428{
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100429 int ret;
430
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200431 if (XT_ALIGN(par->match->matchsize) != size &&
432 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200433 /*
434 * ebt_among is exempt from centralized matchsize checking
435 * because it uses a dynamic-size data set.
436 */
Florian Westphal1b6cd672018-02-09 15:52:00 +0100437 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
438 xt_prefix[par->family], par->match->name,
439 par->match->revision,
440 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800441 return -EINVAL;
442 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200443 if (par->match->table != NULL &&
444 strcmp(par->match->table, par->table) != 0) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100445 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
446 xt_prefix[par->family], par->match->name,
447 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800448 return -EINVAL;
449 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200450 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200451 char used[64], allow[64];
452
Florian Westphal1b6cd672018-02-09 15:52:00 +0100453 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
454 xt_prefix[par->family], par->match->name,
455 textify_hooks(used, sizeof(used),
456 par->hook_mask, par->family),
457 textify_hooks(allow, sizeof(allow),
458 par->match->hooks,
459 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800460 return -EINVAL;
461 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200462 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100463 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
464 xt_prefix[par->family], par->match->name,
465 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800466 return -EINVAL;
467 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100468 if (par->match->checkentry != NULL) {
469 ret = par->match->checkentry(par);
470 if (ret < 0)
471 return ret;
472 else if (ret > 0)
473 /* Flag up potential errors. */
474 return -EIO;
475 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800476 return 0;
477}
478EXPORT_SYMBOL_GPL(xt_check_match);
479
Florian Westphal13631bf2016-04-01 14:17:29 +0200480/** xt_check_entry_match - check that matches end before start of target
481 *
482 * @match: beginning of xt_entry_match
483 * @target: beginning of this rules target (alleged end of matches)
484 * @alignment: alignment requirement of match structures
485 *
486 * Validates that all matches add up to the beginning of the target,
487 * and that each match covers at least the base structure size.
488 *
489 * Return: 0 on success, negative errno on failure.
490 */
491static int xt_check_entry_match(const char *match, const char *target,
492 const size_t alignment)
493{
494 const struct xt_entry_match *pos;
495 int length = target - match;
496
497 if (length == 0) /* no matches */
498 return 0;
499
500 pos = (struct xt_entry_match *)match;
501 do {
502 if ((unsigned long)pos % alignment)
503 return -EINVAL;
504
505 if (length < (int)sizeof(struct xt_entry_match))
506 return -EINVAL;
507
508 if (pos->u.match_size < sizeof(struct xt_entry_match))
509 return -EINVAL;
510
511 if (pos->u.match_size > length)
512 return -EINVAL;
513
514 length -= pos->u.match_size;
515 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
516 } while (length > 0);
517
518 return 0;
519}
520
Florian Westphal1b293e32018-02-27 19:42:29 +0100521/** xt_check_table_hooks - check hook entry points are sane
522 *
523 * @info xt_table_info to check
524 * @valid_hooks - hook entry points that we can enter from
525 *
526 * Validates that the hook entry and underflows points are set up.
527 *
528 * Return: 0 on success, negative errno on failure.
529 */
530int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
531{
532 unsigned int i;
533
534 BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
535
536 for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
537 if (!(valid_hooks & (1 << i)))
538 continue;
539
540 if (info->hook_entry[i] == 0xFFFFFFFF)
541 return -EINVAL;
542 if (info->underflow[i] == 0xFFFFFFFF)
543 return -EINVAL;
544 }
545
546 return 0;
547}
548EXPORT_SYMBOL(xt_check_table_hooks);
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
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400649#define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
650 xt_data_to_user(U->data, K->data, \
651 K->u.kernel.TYPE->usersize, \
652 C_SIZE, \
653 COMPAT_XT_ALIGN(C_SIZE))
654
Jan Engelhardt739674f2009-06-26 08:23:19 +0200655int xt_compat_match_to_user(const struct xt_entry_match *m,
656 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700657{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200658 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700659 struct compat_xt_entry_match __user *cm = *dstptr;
660 int off = xt_compat_match_offset(match);
661 u_int16_t msize = m->u.user.match_size - off;
662
Willem de Bruijn4915f7b2017-01-02 17:19:45 -0500663 if (XT_OBJ_TO_USER(cm, m, match, msize))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800664 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700665
666 if (match->compat_to_user) {
667 if (match->compat_to_user((void __user *)cm->data, m->data))
668 return -EFAULT;
669 } else {
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400670 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700671 return -EFAULT;
672 }
673
674 *size -= off;
675 *dstptr += msize;
676 return 0;
677}
678EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
Florian Westphalfc1221b2016-04-01 14:17:26 +0200679
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200680/* non-compat version may have padding after verdict */
681struct compat_xt_standard_target {
682 struct compat_xt_entry_target t;
683 compat_uint_t verdict;
684};
685
Florian Westphal472ebdc2018-02-27 19:42:28 +0100686struct compat_xt_error_target {
687 struct compat_xt_entry_target t;
688 char errorname[XT_FUNCTION_MAXNAMELEN];
689};
690
Florian Westphal07a9da52018-02-27 19:42:27 +0100691static bool verdict_ok(int verdict)
692{
693 if (verdict > 0)
694 return true;
695
696 if (verdict < 0) {
697 int v = -verdict - 1;
698
699 if (verdict == XT_RETURN)
700 return true;
701
702 switch (v) {
703 case NF_ACCEPT: return true;
704 case NF_DROP: return true;
705 case NF_QUEUE: return true;
706 default:
707 break;
708 }
709
710 return false;
711 }
712
713 return false;
714}
715
Florian Westphal472ebdc2018-02-27 19:42:28 +0100716static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
717 const char *msg, unsigned int msglen)
718{
719 return usersize == kernsize && strnlen(msg, msglen) < msglen;
720}
721
Florian Westphalce683e52016-04-01 14:17:28 +0200722int xt_compat_check_entry_offsets(const void *base, const char *elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +0200723 unsigned int target_offset,
724 unsigned int next_offset)
725{
Florian Westphalce683e52016-04-01 14:17:28 +0200726 long size_of_base_struct = elems - (const char *)base;
Florian Westphalfc1221b2016-04-01 14:17:26 +0200727 const struct compat_xt_entry_target *t;
728 const char *e = base;
729
Florian Westphalce683e52016-04-01 14:17:28 +0200730 if (target_offset < size_of_base_struct)
731 return -EINVAL;
732
Florian Westphalfc1221b2016-04-01 14:17:26 +0200733 if (target_offset + sizeof(*t) > next_offset)
734 return -EINVAL;
735
736 t = (void *)(e + target_offset);
737 if (t->u.target_size < sizeof(*t))
738 return -EINVAL;
739
740 if (target_offset + t->u.target_size > next_offset)
741 return -EINVAL;
742
Florian Westphal07a9da52018-02-27 19:42:27 +0100743 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
744 const struct compat_xt_standard_target *st = (const void *)t;
745
746 if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
747 return -EINVAL;
748
749 if (!verdict_ok(st->verdict))
750 return -EINVAL;
Florian Westphal472ebdc2018-02-27 19:42:28 +0100751 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
752 const struct compat_xt_error_target *et = (const void *)t;
753
754 if (!error_tg_ok(t->u.target_size, sizeof(*et),
755 et->errorname, sizeof(et->errorname)))
756 return -EINVAL;
Florian Westphal07a9da52018-02-27 19:42:27 +0100757 }
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200758
Masahiro Yamada550116d2017-02-27 14:28:58 -0800759 /* compat_xt_entry match has less strict alignment requirements,
Florian Westphal13631bf2016-04-01 14:17:29 +0200760 * otherwise they are identical. In case of padding differences
761 * we need to add compat version of xt_check_entry_match.
762 */
763 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
764
765 return xt_check_entry_match(elems, base + target_offset,
766 __alignof__(struct compat_xt_entry_match));
Florian Westphalfc1221b2016-04-01 14:17:26 +0200767}
768EXPORT_SYMBOL(xt_compat_check_entry_offsets);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700769#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800770
Florian Westphal7d358122016-04-01 14:17:23 +0200771/**
772 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
773 *
774 * @base: pointer to arp/ip/ip6t_entry
Florian Westphalce683e52016-04-01 14:17:28 +0200775 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
Florian Westphal7d358122016-04-01 14:17:23 +0200776 * @target_offset: the arp/ip/ip6_t->target_offset
777 * @next_offset: the arp/ip/ip6_t->next_offset
778 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200779 * validates that target_offset and next_offset are sane and that all
780 * match sizes (if any) align with the target offset.
Florian Westphal7d358122016-04-01 14:17:23 +0200781 *
Florian Westphalce683e52016-04-01 14:17:28 +0200782 * This function does not validate the targets or matches themselves, it
Florian Westphal13631bf2016-04-01 14:17:29 +0200783 * only tests that all the offsets and sizes are correct, that all
784 * match structures are aligned, and that the last structure ends where
785 * the target structure begins.
786 *
787 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
Florian Westphalce683e52016-04-01 14:17:28 +0200788 *
Florian Westphal7d358122016-04-01 14:17:23 +0200789 * The arp/ip/ip6t_entry structure @base must have passed following tests:
790 * - it must point to a valid memory location
791 * - base to base + next_offset must be accessible, i.e. not exceed allocated
792 * length.
793 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200794 * A well-formed entry looks like this:
795 *
796 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
797 * e->elems[]-----' | |
798 * matchsize | |
799 * matchsize | |
800 * | |
801 * target_offset---------------------------------' |
802 * next_offset---------------------------------------------------'
803 *
804 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
805 * This is where matches (if any) and the target reside.
806 * target_offset: beginning of target.
807 * next_offset: start of the next rule; also: size of this rule.
808 * Since targets have a minimum size, target_offset + minlen <= next_offset.
809 *
810 * Every match stores its size, sum of sizes must not exceed target_offset.
811 *
Florian Westphal7d358122016-04-01 14:17:23 +0200812 * Return: 0 on success, negative errno on failure.
813 */
814int xt_check_entry_offsets(const void *base,
Florian Westphalce683e52016-04-01 14:17:28 +0200815 const char *elems,
Florian Westphal7d358122016-04-01 14:17:23 +0200816 unsigned int target_offset,
817 unsigned int next_offset)
818{
Florian Westphalce683e52016-04-01 14:17:28 +0200819 long size_of_base_struct = elems - (const char *)base;
Florian Westphal7d358122016-04-01 14:17:23 +0200820 const struct xt_entry_target *t;
821 const char *e = base;
822
Florian Westphalce683e52016-04-01 14:17:28 +0200823 /* target start is within the ip/ip6/arpt_entry struct */
824 if (target_offset < size_of_base_struct)
825 return -EINVAL;
826
Florian Westphal7d358122016-04-01 14:17:23 +0200827 if (target_offset + sizeof(*t) > next_offset)
828 return -EINVAL;
829
830 t = (void *)(e + target_offset);
Florian Westphala08e4e192016-04-01 14:17:25 +0200831 if (t->u.target_size < sizeof(*t))
832 return -EINVAL;
833
Florian Westphal7d358122016-04-01 14:17:23 +0200834 if (target_offset + t->u.target_size > next_offset)
835 return -EINVAL;
836
Florian Westphal07a9da52018-02-27 19:42:27 +0100837 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
838 const struct xt_standard_target *st = (const void *)t;
839
840 if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
841 return -EINVAL;
842
843 if (!verdict_ok(st->verdict))
844 return -EINVAL;
Florian Westphal472ebdc2018-02-27 19:42:28 +0100845 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
846 const struct xt_error_target *et = (const void *)t;
847
848 if (!error_tg_ok(t->u.target_size, sizeof(*et),
849 et->errorname, sizeof(et->errorname)))
850 return -EINVAL;
Florian Westphal07a9da52018-02-27 19:42:27 +0100851 }
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200852
Florian Westphal13631bf2016-04-01 14:17:29 +0200853 return xt_check_entry_match(elems, base + target_offset,
854 __alignof__(struct xt_entry_match));
Florian Westphal7d358122016-04-01 14:17:23 +0200855}
856EXPORT_SYMBOL(xt_check_entry_offsets);
857
Florian Westphalf4dc7772016-07-14 17:51:26 +0200858/**
859 * xt_alloc_entry_offsets - allocate array to store rule head offsets
860 *
861 * @size: number of entries
862 *
863 * Return: NULL or kmalloc'd or vmalloc'd array
864 */
865unsigned int *xt_alloc_entry_offsets(unsigned int size)
866{
Michal Hocko752ade62017-05-08 15:57:27 -0700867 return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO);
Florian Westphalf4dc7772016-07-14 17:51:26 +0200868
Florian Westphalf4dc7772016-07-14 17:51:26 +0200869}
870EXPORT_SYMBOL(xt_alloc_entry_offsets);
871
872/**
873 * xt_find_jump_offset - check if target is a valid jump offset
874 *
875 * @offsets: array containing all valid rule start offsets of a rule blob
876 * @target: the jump target to search for
877 * @size: entries in @offset
878 */
879bool xt_find_jump_offset(const unsigned int *offsets,
880 unsigned int target, unsigned int size)
881{
882 int m, low = 0, hi = size;
883
884 while (hi > low) {
885 m = (low + hi) / 2u;
886
887 if (offsets[m] > target)
888 hi = m;
889 else if (offsets[m] < target)
890 low = m + 1;
891 else
892 return true;
893 }
894
895 return false;
896}
897EXPORT_SYMBOL(xt_find_jump_offset);
898
Jan Engelhardt916a9172008-10-08 11:35:20 +0200899int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200900 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800901{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100902 int ret;
903
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200904 if (XT_ALIGN(par->target->targetsize) != size) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100905 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
906 xt_prefix[par->family], par->target->name,
907 par->target->revision,
908 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800909 return -EINVAL;
910 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200911 if (par->target->table != NULL &&
912 strcmp(par->target->table, par->table) != 0) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100913 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
914 xt_prefix[par->family], par->target->name,
915 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800916 return -EINVAL;
917 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200918 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200919 char used[64], allow[64];
920
Florian Westphal1b6cd672018-02-09 15:52:00 +0100921 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
922 xt_prefix[par->family], par->target->name,
923 textify_hooks(used, sizeof(used),
924 par->hook_mask, par->family),
925 textify_hooks(allow, sizeof(allow),
926 par->target->hooks,
927 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800928 return -EINVAL;
929 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200930 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100931 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
932 xt_prefix[par->family], par->target->name,
933 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800934 return -EINVAL;
935 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100936 if (par->target->checkentry != NULL) {
937 ret = par->target->checkentry(par);
938 if (ret < 0)
939 return ret;
940 else if (ret > 0)
941 /* Flag up potential errors. */
942 return -EIO;
943 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800944 return 0;
945}
946EXPORT_SYMBOL_GPL(xt_check_target);
947
Florian Westphald7591f02016-04-01 15:37:59 +0200948/**
949 * xt_copy_counters_from_user - copy counters and metadata from userspace
950 *
951 * @user: src pointer to userspace memory
952 * @len: alleged size of userspace memory
953 * @info: where to store the xt_counters_info metadata
954 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
955 *
956 * Copies counter meta data from @user and stores it in @info.
957 *
958 * vmallocs memory to hold the counters, then copies the counter data
959 * from @user to the new memory and returns a pointer to it.
960 *
961 * If @compat is true, @info gets converted automatically to the 64bit
962 * representation.
963 *
964 * The metadata associated with the counters is stored in @info.
965 *
966 * Return: returns pointer that caller has to test via IS_ERR().
967 * If IS_ERR is false, caller has to vfree the pointer.
968 */
969void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
970 struct xt_counters_info *info, bool compat)
971{
972 void *mem;
973 u64 size;
974
975#ifdef CONFIG_COMPAT
976 if (compat) {
977 /* structures only differ in size due to alignment */
978 struct compat_xt_counters_info compat_tmp;
979
980 if (len <= sizeof(compat_tmp))
981 return ERR_PTR(-EINVAL);
982
983 len -= sizeof(compat_tmp);
984 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
985 return ERR_PTR(-EFAULT);
986
Eric Dumazete466af72017-10-05 02:50:07 -0700987 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
Florian Westphald7591f02016-04-01 15:37:59 +0200988 info->num_counters = compat_tmp.num_counters;
989 user += sizeof(compat_tmp);
990 } else
991#endif
992 {
993 if (len <= sizeof(*info))
994 return ERR_PTR(-EINVAL);
995
996 len -= sizeof(*info);
997 if (copy_from_user(info, user, sizeof(*info)) != 0)
998 return ERR_PTR(-EFAULT);
999
Florian Westphald7591f02016-04-01 15:37:59 +02001000 user += sizeof(*info);
1001 }
Eric Dumazete466af72017-10-05 02:50:07 -07001002 info->name[sizeof(info->name) - 1] = '\0';
Florian Westphald7591f02016-04-01 15:37:59 +02001003
1004 size = sizeof(struct xt_counters);
1005 size *= info->num_counters;
1006
1007 if (size != (u64)len)
1008 return ERR_PTR(-EINVAL);
1009
1010 mem = vmalloc(len);
1011 if (!mem)
1012 return ERR_PTR(-ENOMEM);
1013
1014 if (copy_from_user(mem, user, len) == 0)
1015 return mem;
1016
1017 vfree(mem);
1018 return ERR_PTR(-EFAULT);
1019}
1020EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
1021
Dmitry Mishin27229712006-04-01 02:25:19 -08001022#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +02001023int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -08001024{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001025 u_int16_t csize = target->compatsize ? : target->targetsize;
1026 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -08001027}
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001028EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1029
1030void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001031 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001032{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001033 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001034 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
1035 int pad, off = xt_compat_target_offset(target);
1036 u_int16_t tsize = ct->u.user.target_size;
Florian Westphal09d96862016-04-01 14:17:34 +02001037 char name[sizeof(t->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001038
1039 t = *dstptr;
1040 memcpy(t, ct, sizeof(*ct));
1041 if (target->compat_from_user)
1042 target->compat_from_user(t->data, ct->data);
1043 else
1044 memcpy(t->data, ct->data, tsize - sizeof(*ct));
1045 pad = XT_ALIGN(target->targetsize) - target->targetsize;
1046 if (pad > 0)
1047 memset(t->data + target->targetsize, 0, pad);
1048
1049 tsize += off;
1050 t->u.user.target_size = tsize;
Florian Westphal09d96862016-04-01 14:17:34 +02001051 strlcpy(name, target->name, sizeof(name));
1052 module_put(target->me);
1053 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001054
1055 *size += off;
1056 *dstptr += tsize;
1057}
1058EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1059
Jan Engelhardt739674f2009-06-26 08:23:19 +02001060int xt_compat_target_to_user(const struct xt_entry_target *t,
1061 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001062{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001063 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001064 struct compat_xt_entry_target __user *ct = *dstptr;
1065 int off = xt_compat_target_offset(target);
1066 u_int16_t tsize = t->u.user.target_size - off;
1067
Willem de Bruijn4915f7b2017-01-02 17:19:45 -05001068 if (XT_OBJ_TO_USER(ct, t, target, tsize))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001069 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001070
1071 if (target->compat_to_user) {
1072 if (target->compat_to_user((void __user *)ct->data, t->data))
1073 return -EFAULT;
1074 } else {
Willem de Bruijn751a9c72017-05-17 11:24:47 -04001075 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001076 return -EFAULT;
1077 }
1078
1079 *size -= off;
1080 *dstptr += tsize;
1081 return 0;
1082}
1083EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -08001084#endif
1085
Harald Welte2e4e6a12006-01-12 13:30:04 -08001086struct xt_table_info *xt_alloc_table_info(unsigned int size)
1087{
Eric Dumazet711bdde2015-06-15 09:57:30 -07001088 struct xt_table_info *info = NULL;
1089 size_t sz = sizeof(*info) + size;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001090
Florian Westphald157bd72016-03-10 01:56:23 +01001091 if (sz < sizeof(*info))
1092 return NULL;
1093
Michal Hocko05372502018-01-30 11:30:11 -08001094 /* __GFP_NORETRY is not fully supported by kvmalloc but it should
1095 * work reasonably well if sz is too large and bail out rather
1096 * than shoot all processes down before realizing there is nothing
1097 * more to reclaim.
1098 */
1099 info = kvmalloc(sz, GFP_KERNEL | __GFP_NORETRY);
Michal Hockoeacd86c2017-07-12 14:35:37 -07001100 if (!info)
1101 return NULL;
1102
Eric Dumazet711bdde2015-06-15 09:57:30 -07001103 memset(info, 0, sizeof(*info));
1104 info->size = size;
1105 return info;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001106}
1107EXPORT_SYMBOL(xt_alloc_table_info);
1108
1109void xt_free_table_info(struct xt_table_info *info)
1110{
1111 int cpu;
1112
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001113 if (info->jumpstack != NULL) {
Eric Dumazetf6b50822014-06-24 02:15:35 -07001114 for_each_possible_cpu(cpu)
1115 kvfree(info->jumpstack[cpu]);
1116 kvfree(info->jumpstack);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001117 }
1118
Eric Dumazet711bdde2015-06-15 09:57:30 -07001119 kvfree(info);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001120}
1121EXPORT_SYMBOL(xt_free_table_info);
1122
Florian Westphal03d13b62017-12-08 17:01:53 +01001123/* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001124struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1125 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001126{
Florian Westphalb9e69e12016-02-25 10:08:36 +01001127 struct xt_table *t, *found = NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001128
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001129 mutex_lock(&xt[af].mutex);
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001130 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001131 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1132 return t;
Florian Westphalb9e69e12016-02-25 10:08:36 +01001133
1134 if (net == &init_net)
1135 goto out;
1136
1137 /* Table doesn't exist in this netns, re-try init */
1138 list_for_each_entry(t, &init_net.xt.tables[af], list) {
Florian Westphal03d13b62017-12-08 17:01:53 +01001139 int err;
1140
Florian Westphalb9e69e12016-02-25 10:08:36 +01001141 if (strcmp(t->name, name))
1142 continue;
Florian Westphal03d13b62017-12-08 17:01:53 +01001143 if (!try_module_get(t->me))
1144 goto out;
Florian Westphalb9e69e12016-02-25 10:08:36 +01001145 mutex_unlock(&xt[af].mutex);
Florian Westphal03d13b62017-12-08 17:01:53 +01001146 err = t->table_init(net);
1147 if (err < 0) {
Florian Westphalb9e69e12016-02-25 10:08:36 +01001148 module_put(t->me);
Florian Westphal03d13b62017-12-08 17:01:53 +01001149 return ERR_PTR(err);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001150 }
1151
1152 found = t;
1153
1154 mutex_lock(&xt[af].mutex);
1155 break;
1156 }
1157
1158 if (!found)
1159 goto out;
1160
1161 /* and once again: */
1162 list_for_each_entry(t, &net->xt.tables[af], list)
1163 if (strcmp(t->name, name) == 0)
1164 return t;
1165
1166 module_put(found->me);
1167 out:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001168 mutex_unlock(&xt[af].mutex);
Florian Westphal03d13b62017-12-08 17:01:53 +01001169 return ERR_PTR(-ENOENT);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001170}
1171EXPORT_SYMBOL_GPL(xt_find_table_lock);
1172
Florian Westphal03d13b62017-12-08 17:01:53 +01001173struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1174 const char *name)
1175{
1176 struct xt_table *t = xt_find_table_lock(net, af, name);
1177
Florian Westphal20651ce2018-01-09 14:30:48 +01001178#ifdef CONFIG_MODULES
Florian Westphal03d13b62017-12-08 17:01:53 +01001179 if (IS_ERR(t)) {
1180 int err = request_module("%stable_%s", xt_prefix[af], name);
Florian Westphale3eeacb2018-01-13 14:06:08 +01001181 if (err < 0)
Florian Westphal03d13b62017-12-08 17:01:53 +01001182 return ERR_PTR(err);
1183 t = xt_find_table_lock(net, af, name);
1184 }
1185#endif
1186
1187 return t;
1188}
1189EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1190
Harald Welte2e4e6a12006-01-12 13:30:04 -08001191void xt_table_unlock(struct xt_table *table)
1192{
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001193 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001194}
1195EXPORT_SYMBOL_GPL(xt_table_unlock);
1196
Dmitry Mishin27229712006-04-01 02:25:19 -08001197#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001198void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001199{
1200 mutex_lock(&xt[af].compat_mutex);
1201}
1202EXPORT_SYMBOL_GPL(xt_compat_lock);
1203
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001204void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001205{
1206 mutex_unlock(&xt[af].compat_mutex);
1207}
1208EXPORT_SYMBOL_GPL(xt_compat_unlock);
1209#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001210
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001211DEFINE_PER_CPU(seqcount_t, xt_recseq);
1212EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001213
Florian Westphaldcebd312015-07-14 17:51:09 +02001214struct static_key xt_tee_enabled __read_mostly;
1215EXPORT_SYMBOL_GPL(xt_tee_enabled);
1216
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001217static int xt_jumpstack_alloc(struct xt_table_info *i)
1218{
1219 unsigned int size;
1220 int cpu;
1221
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001222 size = sizeof(void **) * nr_cpu_ids;
1223 if (size > PAGE_SIZE)
Michal Hocko752ade62017-05-08 15:57:27 -07001224 i->jumpstack = kvzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001225 else
Joe Perches3dbd4432011-05-28 10:36:35 -07001226 i->jumpstack = kzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001227 if (i->jumpstack == NULL)
1228 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001229
Florian Westphal98d1bd82015-07-14 17:51:06 +02001230 /* ruleset without jumps -- no stack needed */
1231 if (i->stacksize == 0)
1232 return 0;
1233
Florian Westphal7814b6e2015-07-14 17:51:08 +02001234 /* Jumpstack needs to be able to record two full callchains, one
1235 * from the first rule set traversal, plus one table reentrancy
1236 * via -j TEE without clobbering the callchain that brought us to
1237 * TEE target.
1238 *
1239 * This is done by allocating two jumpstacks per cpu, on reentry
1240 * the upper half of the stack is used.
1241 *
1242 * see the jumpstack setup in ipt_do_table() for more details.
1243 */
1244 size = sizeof(void *) * i->stacksize * 2u;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001245 for_each_possible_cpu(cpu) {
Michal Hocko752ade62017-05-08 15:57:27 -07001246 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1247 cpu_to_node(cpu));
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001248 if (i->jumpstack[cpu] == NULL)
1249 /*
1250 * Freeing will be done later on by the callers. The
1251 * chain is: xt_replace_table -> __do_replace ->
1252 * do_replace -> xt_free_table_info.
1253 */
1254 return -ENOMEM;
1255 }
1256
1257 return 0;
1258}
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001259
Harald Welte2e4e6a12006-01-12 13:30:04 -08001260struct xt_table_info *
1261xt_replace_table(struct xt_table *table,
1262 unsigned int num_counters,
1263 struct xt_table_info *newinfo,
1264 int *error)
1265{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001266 struct xt_table_info *private;
Florian Westphal80055da2017-10-12 01:13:50 +02001267 unsigned int cpu;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001268 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001269
Jan Engelhardtd97a9e42010-04-21 14:45:51 +02001270 ret = xt_jumpstack_alloc(newinfo);
1271 if (ret < 0) {
1272 *error = ret;
1273 return NULL;
1274 }
1275
Harald Welte2e4e6a12006-01-12 13:30:04 -08001276 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001277 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001278 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001279
Harald Welte2e4e6a12006-01-12 13:30:04 -08001280 /* Check inside lock: is the old number correct? */
1281 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001282 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001283 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001284 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001285 *error = -EAGAIN;
1286 return NULL;
1287 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001288
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001289 newinfo->initial_entries = private->initial_entries;
Will Deaconb416c142013-10-21 13:14:53 +01001290 /*
1291 * Ensure contents of newinfo are visible before assigning to
1292 * private.
1293 */
1294 smp_wmb();
1295 table->private = newinfo;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001296
Florian Westphal80055da2017-10-12 01:13:50 +02001297 /* make sure all cpus see new ->private value */
1298 smp_wmb();
1299
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001300 /*
1301 * Even though table entries have now been swapped, other CPU's
Florian Westphal80055da2017-10-12 01:13:50 +02001302 * may still be using the old entries...
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001303 */
1304 local_bh_enable();
1305
Florian Westphal80055da2017-10-12 01:13:50 +02001306 /* ... so wait for even xt_recseq on all cpus */
1307 for_each_possible_cpu(cpu) {
1308 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1309 u32 seq = raw_read_seqcount(s);
1310
1311 if (seq & 1) {
1312 do {
1313 cond_resched();
1314 cpu_relax();
1315 } while (seq == raw_read_seqcount(s));
1316 }
1317 }
1318
Thomas Graffbabf312011-01-16 18:12:59 +01001319#ifdef CONFIG_AUDIT
1320 if (audit_enabled) {
Geliang Tang46b20c32017-08-07 21:44:25 +08001321 audit_log(current->audit_context, GFP_KERNEL,
1322 AUDIT_NETFILTER_CFG,
1323 "table=%s family=%u entries=%u",
1324 table->name, table->af, private->number);
Thomas Graffbabf312011-01-16 18:12:59 +01001325 }
1326#endif
1327
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001328 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001329}
1330EXPORT_SYMBOL_GPL(xt_replace_table);
1331
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001332struct xt_table *xt_register_table(struct net *net,
1333 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -08001334 struct xt_table_info *bootstrap,
1335 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001336{
1337 int ret;
1338 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001339 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001340
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001341 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001342 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001343 if (!table) {
1344 ret = -ENOMEM;
1345 goto out;
1346 }
1347
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001348 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001349 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001350 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -07001351 if (strcmp(t->name, table->name) == 0) {
1352 ret = -EEXIST;
1353 goto unlock;
1354 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001355 }
1356
1357 /* Simplifies replace_table code. */
1358 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +01001359
Harald Welte2e4e6a12006-01-12 13:30:04 -08001360 if (!xt_replace_table(table, 0, newinfo, &ret))
1361 goto unlock;
1362
1363 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001364 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001365
1366 /* save number of initial entries */
1367 private->initial_entries = private->number;
1368
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001369 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001370 mutex_unlock(&xt[table->af].mutex);
1371 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001372
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001373unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001374 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001375 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001376out:
1377 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001378}
1379EXPORT_SYMBOL_GPL(xt_register_table);
1380
1381void *xt_unregister_table(struct xt_table *table)
1382{
1383 struct xt_table_info *private;
1384
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001385 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001386 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -07001387 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001388 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001389 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001390
1391 return private;
1392}
1393EXPORT_SYMBOL_GPL(xt_unregister_table);
1394
1395#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001396struct xt_names_priv {
1397 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001398 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001399};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001400static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001401{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001402 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001403 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001404 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001405
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001406 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001407 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001408}
1409
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001410static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001411{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001412 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001413 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001414 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001415
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001416 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001417}
1418
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001419static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001420{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001421 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001422 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001423
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001424 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001425}
1426
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001427static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001428{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001429 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001430
Joe Perches861fb102015-05-12 18:28:23 -07001431 if (*table->name)
Steven Rostedt (Red Hat)e71456ae2014-10-27 17:43:45 -04001432 seq_printf(seq, "%s\n", table->name);
Joe Perches861fb102015-05-12 18:28:23 -07001433 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001434}
1435
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001436static const struct seq_operations xt_table_seq_ops = {
1437 .start = xt_table_seq_start,
1438 .next = xt_table_seq_next,
1439 .stop = xt_table_seq_stop,
1440 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001441};
1442
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001443static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001444{
1445 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001446 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001447
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001448 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1449 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001450 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001451 priv = ((struct seq_file *)file->private_data)->private;
Al Virod9dda782013-03-31 18:16:14 -04001452 priv->af = (unsigned long)PDE_DATA(inode);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001453 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001454 return ret;
1455}
1456
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001457static const struct file_operations xt_table_ops = {
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001458 .open = xt_table_open,
1459 .read = seq_read,
1460 .llseek = seq_lseek,
Pavel Emelyanov0e93bb942008-04-29 03:15:35 -07001461 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001462};
1463
Jan Engelhardteb132202009-02-18 16:42:19 +01001464/*
1465 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1466 * the multi-AF mutexes.
1467 */
1468struct nf_mttg_trav {
1469 struct list_head *head, *curr;
1470 uint8_t class, nfproto;
1471};
1472
1473enum {
1474 MTTG_TRAV_INIT,
1475 MTTG_TRAV_NFP_UNSPEC,
1476 MTTG_TRAV_NFP_SPEC,
1477 MTTG_TRAV_DONE,
1478};
1479
1480static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1481 bool is_target)
1482{
1483 static const uint8_t next_class[] = {
1484 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1485 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1486 };
1487 struct nf_mttg_trav *trav = seq->private;
1488
1489 switch (trav->class) {
1490 case MTTG_TRAV_INIT:
1491 trav->class = MTTG_TRAV_NFP_UNSPEC;
1492 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1493 trav->head = trav->curr = is_target ?
1494 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1495 break;
1496 case MTTG_TRAV_NFP_UNSPEC:
1497 trav->curr = trav->curr->next;
1498 if (trav->curr != trav->head)
1499 break;
1500 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1501 mutex_lock(&xt[trav->nfproto].mutex);
1502 trav->head = trav->curr = is_target ?
1503 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1504 trav->class = next_class[trav->class];
1505 break;
1506 case MTTG_TRAV_NFP_SPEC:
1507 trav->curr = trav->curr->next;
1508 if (trav->curr != trav->head)
1509 break;
Gustavo A. R. Silvae8542dc2017-11-07 08:19:29 -06001510 /* fall through */
Jan Engelhardteb132202009-02-18 16:42:19 +01001511 default:
1512 return NULL;
1513 }
1514
1515 if (ppos != NULL)
1516 ++*ppos;
1517 return trav;
1518}
1519
1520static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1521 bool is_target)
1522{
1523 struct nf_mttg_trav *trav = seq->private;
1524 unsigned int j;
1525
1526 trav->class = MTTG_TRAV_INIT;
1527 for (j = 0; j < *pos; ++j)
1528 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1529 return NULL;
1530 return trav;
1531}
1532
1533static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1534{
1535 struct nf_mttg_trav *trav = seq->private;
1536
1537 switch (trav->class) {
1538 case MTTG_TRAV_NFP_UNSPEC:
1539 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1540 break;
1541 case MTTG_TRAV_NFP_SPEC:
1542 mutex_unlock(&xt[trav->nfproto].mutex);
1543 break;
1544 }
1545}
1546
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001547static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1548{
Jan Engelhardteb132202009-02-18 16:42:19 +01001549 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001550}
1551
Jan Engelhardteb132202009-02-18 16:42:19 +01001552static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001553{
Jan Engelhardteb132202009-02-18 16:42:19 +01001554 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001555}
1556
1557static int xt_match_seq_show(struct seq_file *seq, void *v)
1558{
Jan Engelhardteb132202009-02-18 16:42:19 +01001559 const struct nf_mttg_trav *trav = seq->private;
1560 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001561
Jan Engelhardteb132202009-02-18 16:42:19 +01001562 switch (trav->class) {
1563 case MTTG_TRAV_NFP_UNSPEC:
1564 case MTTG_TRAV_NFP_SPEC:
1565 if (trav->curr == trav->head)
1566 return 0;
1567 match = list_entry(trav->curr, struct xt_match, list);
Joe Perches861fb102015-05-12 18:28:23 -07001568 if (*match->name)
1569 seq_printf(seq, "%s\n", match->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001570 }
1571 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001572}
1573
1574static const struct seq_operations xt_match_seq_ops = {
1575 .start = xt_match_seq_start,
1576 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001577 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001578 .show = xt_match_seq_show,
1579};
1580
1581static int xt_match_open(struct inode *inode, struct file *file)
1582{
Jan Engelhardteb132202009-02-18 16:42:19 +01001583 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001584 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1585 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001586 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001587
Al Virod9dda782013-03-31 18:16:14 -04001588 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001589 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001590}
1591
1592static const struct file_operations xt_match_ops = {
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001593 .open = xt_match_open,
1594 .read = seq_read,
1595 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001596 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001597};
1598
1599static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1600{
Jan Engelhardteb132202009-02-18 16:42:19 +01001601 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001602}
1603
Jan Engelhardteb132202009-02-18 16:42:19 +01001604static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001605{
Jan Engelhardteb132202009-02-18 16:42:19 +01001606 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001607}
1608
1609static int xt_target_seq_show(struct seq_file *seq, void *v)
1610{
Jan Engelhardteb132202009-02-18 16:42:19 +01001611 const struct nf_mttg_trav *trav = seq->private;
1612 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001613
Jan Engelhardteb132202009-02-18 16:42:19 +01001614 switch (trav->class) {
1615 case MTTG_TRAV_NFP_UNSPEC:
1616 case MTTG_TRAV_NFP_SPEC:
1617 if (trav->curr == trav->head)
1618 return 0;
1619 target = list_entry(trav->curr, struct xt_target, list);
Joe Perches861fb102015-05-12 18:28:23 -07001620 if (*target->name)
1621 seq_printf(seq, "%s\n", target->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001622 }
1623 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001624}
1625
1626static const struct seq_operations xt_target_seq_ops = {
1627 .start = xt_target_seq_start,
1628 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001629 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001630 .show = xt_target_seq_show,
1631};
1632
1633static int xt_target_open(struct inode *inode, struct file *file)
1634{
Jan Engelhardteb132202009-02-18 16:42:19 +01001635 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001636 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1637 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001638 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001639
Al Virod9dda782013-03-31 18:16:14 -04001640 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001641 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001642}
1643
1644static const struct file_operations xt_target_ops = {
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001645 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001646 .read = seq_read,
1647 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001648 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001649};
1650
1651#define FORMAT_TABLES "_tables_names"
1652#define FORMAT_MATCHES "_tables_matches"
1653#define FORMAT_TARGETS "_tables_targets"
1654
1655#endif /* CONFIG_PROC_FS */
1656
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001657/**
Florian Westphalb9e69e12016-02-25 10:08:36 +01001658 * xt_hook_ops_alloc - set up hooks for a new table
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001659 * @table: table with metadata needed to set up hooks
1660 * @fn: Hook function
1661 *
Florian Westphalb9e69e12016-02-25 10:08:36 +01001662 * This function will create the nf_hook_ops that the x_table needs
1663 * to hand to xt_hook_link_net().
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001664 */
Florian Westphalb9e69e12016-02-25 10:08:36 +01001665struct nf_hook_ops *
1666xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001667{
1668 unsigned int hook_mask = table->valid_hooks;
1669 uint8_t i, num_hooks = hweight32(hook_mask);
1670 uint8_t hooknum;
1671 struct nf_hook_ops *ops;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001672
Xiubo Lia6d0bae2016-06-02 10:59:56 +08001673 if (!num_hooks)
1674 return ERR_PTR(-EINVAL);
1675
Florian Westphal1ecc2812016-10-17 21:50:23 +02001676 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001677 if (ops == NULL)
1678 return ERR_PTR(-ENOMEM);
1679
1680 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1681 hook_mask >>= 1, ++hooknum) {
1682 if (!(hook_mask & 1))
1683 continue;
1684 ops[i].hook = fn;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001685 ops[i].pf = table->af;
1686 ops[i].hooknum = hooknum;
1687 ops[i].priority = table->priority;
1688 ++i;
1689 }
1690
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001691 return ops;
1692}
Florian Westphalb9e69e12016-02-25 10:08:36 +01001693EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001694
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001695int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001696{
1697#ifdef CONFIG_PROC_FS
1698 char buf[XT_FUNCTION_MAXNAMELEN];
1699 struct proc_dir_entry *proc;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001700 kuid_t root_uid;
1701 kgid_t root_gid;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001702#endif
1703
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001704 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001705 return -EINVAL;
1706
1707
1708#ifdef CONFIG_PROC_FS
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001709 root_uid = make_kuid(net->user_ns, 0);
1710 root_gid = make_kgid(net->user_ns, 0);
1711
Tobias Klauserce18afe2007-03-14 16:36:16 -07001712 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001713 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001714 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1715 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001716 if (!proc)
1717 goto out;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001718 if (uid_valid(root_uid) && gid_valid(root_gid))
1719 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001720
Tobias Klauserce18afe2007-03-14 16:36:16 -07001721 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001722 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001723 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1724 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001725 if (!proc)
1726 goto out_remove_tables;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001727 if (uid_valid(root_uid) && gid_valid(root_gid))
1728 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001729
Tobias Klauserce18afe2007-03-14 16:36:16 -07001730 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001731 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001732 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1733 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001734 if (!proc)
1735 goto out_remove_matches;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001736 if (uid_valid(root_uid) && gid_valid(root_gid))
1737 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001738#endif
1739
1740 return 0;
1741
1742#ifdef CONFIG_PROC_FS
1743out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001744 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001745 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001746 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001747
1748out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001749 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001750 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001751 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001752out:
1753 return -1;
1754#endif
1755}
1756EXPORT_SYMBOL_GPL(xt_proto_init);
1757
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001758void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001759{
1760#ifdef CONFIG_PROC_FS
1761 char buf[XT_FUNCTION_MAXNAMELEN];
1762
Tobias Klauserce18afe2007-03-14 16:36:16 -07001763 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001764 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001765 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001766
Tobias Klauserce18afe2007-03-14 16:36:16 -07001767 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001768 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001769 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001770
Tobias Klauserce18afe2007-03-14 16:36:16 -07001771 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001772 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001773 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001774#endif /*CONFIG_PROC_FS*/
1775}
1776EXPORT_SYMBOL_GPL(xt_proto_fini);
1777
Florian Westphalf28e15b2016-11-22 14:44:18 +01001778/**
1779 * xt_percpu_counter_alloc - allocate x_tables rule counter
1780 *
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001781 * @state: pointer to xt_percpu allocation state
Florian Westphalf28e15b2016-11-22 14:44:18 +01001782 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1783 *
1784 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1785 * contain the address of the real (percpu) counter.
1786 *
1787 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1788 * to fetch the real percpu counter.
1789 *
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001790 * To speed up allocation and improve data locality, a 4kb block is
1791 * allocated.
1792 *
1793 * xt_percpu_counter_alloc_state contains the base address of the
1794 * allocated page and the current sub-offset.
1795 *
Florian Westphalf28e15b2016-11-22 14:44:18 +01001796 * returns false on error.
1797 */
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001798bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1799 struct xt_counters *counter)
Florian Westphalf28e15b2016-11-22 14:44:18 +01001800{
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001801 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
Florian Westphalf28e15b2016-11-22 14:44:18 +01001802
1803 if (nr_cpu_ids <= 1)
1804 return true;
1805
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001806 if (!state->mem) {
1807 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1808 XT_PCPU_BLOCK_SIZE);
1809 if (!state->mem)
1810 return false;
1811 }
1812 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1813 state->off += sizeof(*counter);
1814 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1815 state->mem = NULL;
1816 state->off = 0;
1817 }
Florian Westphalf28e15b2016-11-22 14:44:18 +01001818 return true;
1819}
1820EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1821
Florian Westphal4d31eef2016-11-22 14:44:17 +01001822void xt_percpu_counter_free(struct xt_counters *counters)
1823{
1824 unsigned long pcnt = counters->pcnt;
1825
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001826 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
Florian Westphal4d31eef2016-11-22 14:44:17 +01001827 free_percpu((void __percpu *)pcnt);
1828}
1829EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1830
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001831static int __net_init xt_net_init(struct net *net)
1832{
1833 int i;
1834
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001835 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001836 INIT_LIST_HEAD(&net->xt.tables[i]);
1837 return 0;
1838}
1839
Vasily Averin613d0772017-11-12 14:32:37 +03001840static void __net_exit xt_net_exit(struct net *net)
1841{
1842 int i;
1843
1844 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1845 WARN_ON_ONCE(!list_empty(&net->xt.tables[i]));
1846}
1847
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001848static struct pernet_operations xt_net_ops = {
1849 .init = xt_net_init,
Vasily Averin613d0772017-11-12 14:32:37 +03001850 .exit = xt_net_exit,
Kirill Tkhai4d6b8072018-02-19 11:50:45 +03001851 .async = true,
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001852};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001853
1854static int __init xt_init(void)
1855{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001856 unsigned int i;
1857 int rv;
1858
1859 for_each_possible_cpu(i) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001860 seqcount_init(&per_cpu(xt_recseq, i));
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001861 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001862
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001863 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001864 if (!xt)
1865 return -ENOMEM;
1866
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001867 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001868 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001869#ifdef CONFIG_COMPAT
1870 mutex_init(&xt[i].compat_mutex);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001871 xt[i].compat_tab = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001872#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001873 INIT_LIST_HEAD(&xt[i].target);
1874 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001875 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001876 rv = register_pernet_subsys(&xt_net_ops);
1877 if (rv < 0)
1878 kfree(xt);
1879 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001880}
1881
1882static void __exit xt_fini(void)
1883{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001884 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001885 kfree(xt);
1886}
1887
1888module_init(xt_init);
1889module_exit(xt_fini);
1890