blob: 71325fef647da706f4dc23eaee28c3a6d1b6799d [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
Florian Westphal19926962018-02-27 19:42:31 +010043#define XT_MAX_TABLE_SIZE (512 * 1024 * 1024)
Harald Welte2e4e6a12006-01-12 13:30:04 -080044
Patrick McHardyb386d9f2007-12-17 21:47:48 -080045struct compat_delta {
Eric Dumazet255d0dc2010-12-18 18:35:15 +010046 unsigned int offset; /* offset in kernel */
47 int delta; /* delta in 32bit user land */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080048};
49
Harald Welte2e4e6a12006-01-12 13:30:04 -080050struct xt_af {
Ingo Molnar9e19bb62006-03-25 01:41:10 -080051 struct mutex mutex;
Harald Welte2e4e6a12006-01-12 13:30:04 -080052 struct list_head match;
53 struct list_head target;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080054#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080055 struct mutex compat_mutex;
Eric Dumazet255d0dc2010-12-18 18:35:15 +010056 struct compat_delta *compat_tab;
57 unsigned int number; /* number of slots in compat_tab[] */
58 unsigned int cur; /* number of used slots in compat_tab[] */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080059#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080060};
61
62static struct xt_af *xt;
63
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020064static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
65 [NFPROTO_UNSPEC] = "x",
66 [NFPROTO_IPV4] = "ip",
67 [NFPROTO_ARP] = "arp",
68 [NFPROTO_BRIDGE] = "eb",
69 [NFPROTO_IPV6] = "ip6",
Patrick McHardy37f9f732006-03-20 17:59:06 -080070};
71
Harald Welte2e4e6a12006-01-12 13:30:04 -080072/* Registration hooks for targets. */
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020073int xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080074{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020075 u_int8_t af = target->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -080076
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020077 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080078 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080079 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020080 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -080081}
82EXPORT_SYMBOL(xt_register_target);
83
84void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080085xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080086{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020087 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080088
Ingo Molnar9e19bb62006-03-25 01:41:10 -080089 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070090 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080091 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080092}
93EXPORT_SYMBOL(xt_unregister_target);
94
95int
Patrick McHardy52d9c422006-08-22 00:33:45 -070096xt_register_targets(struct xt_target *target, unsigned int n)
97{
98 unsigned int i;
99 int err = 0;
100
101 for (i = 0; i < n; i++) {
102 err = xt_register_target(&target[i]);
103 if (err)
104 goto err;
105 }
106 return err;
107
108err:
109 if (i > 0)
110 xt_unregister_targets(target, i);
111 return err;
112}
113EXPORT_SYMBOL(xt_register_targets);
114
115void
116xt_unregister_targets(struct xt_target *target, unsigned int n)
117{
Changli Gaof68c5302010-10-04 22:24:12 +0200118 while (n-- > 0)
119 xt_unregister_target(&target[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700120}
121EXPORT_SYMBOL(xt_unregister_targets);
122
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200123int xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800124{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200125 u_int8_t af = match->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800126
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200127 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800128 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800129 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200130 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800131}
132EXPORT_SYMBOL(xt_register_match);
133
134void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800135xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800136{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200137 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800138
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800139 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700140 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800141 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800142}
143EXPORT_SYMBOL(xt_unregister_match);
144
Patrick McHardy52d9c422006-08-22 00:33:45 -0700145int
146xt_register_matches(struct xt_match *match, unsigned int n)
147{
148 unsigned int i;
149 int err = 0;
150
151 for (i = 0; i < n; i++) {
152 err = xt_register_match(&match[i]);
153 if (err)
154 goto err;
155 }
156 return err;
157
158err:
159 if (i > 0)
160 xt_unregister_matches(match, i);
161 return err;
162}
163EXPORT_SYMBOL(xt_register_matches);
164
165void
166xt_unregister_matches(struct xt_match *match, unsigned int n)
167{
Changli Gaof68c5302010-10-04 22:24:12 +0200168 while (n-- > 0)
169 xt_unregister_match(&match[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700170}
171EXPORT_SYMBOL(xt_unregister_matches);
172
Harald Welte2e4e6a12006-01-12 13:30:04 -0800173
174/*
175 * These are weird, but module loading must not be done with mutex
176 * held (since they will register), and we have to have a single
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100177 * function to use.
Harald Welte2e4e6a12006-01-12 13:30:04 -0800178 */
179
180/* Find match, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200181struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800182{
183 struct xt_match *m;
Patrick McHardy42046e22011-03-14 19:11:44 +0100184 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800185
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200186 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800187 list_for_each_entry(m, &xt[af].match, list) {
188 if (strcmp(m->name, name) == 0) {
189 if (m->revision == revision) {
190 if (try_module_get(m->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800191 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800192 return m;
193 }
194 } else
195 err = -EPROTOTYPE; /* Found something. */
196 }
197 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800198 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200199
200 if (af != NFPROTO_UNSPEC)
201 /* Try searching again in the family-independent list */
202 return xt_find_match(NFPROTO_UNSPEC, name, revision);
203
Harald Welte2e4e6a12006-01-12 13:30:04 -0800204 return ERR_PTR(err);
205}
206EXPORT_SYMBOL(xt_find_match);
207
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200208struct xt_match *
209xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
210{
211 struct xt_match *match;
212
Eric Dumazetda17c732018-01-24 17:16:09 -0800213 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
214 return ERR_PTR(-EINVAL);
215
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100216 match = xt_find_match(nfproto, name, revision);
217 if (IS_ERR(match)) {
218 request_module("%st_%s", xt_prefix[nfproto], name);
219 match = xt_find_match(nfproto, name, revision);
220 }
221
222 return match;
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200223}
224EXPORT_SYMBOL_GPL(xt_request_find_match);
225
Harald Welte2e4e6a12006-01-12 13:30:04 -0800226/* Find target, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200227struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800228{
229 struct xt_target *t;
Patrick McHardy42046e22011-03-14 19:11:44 +0100230 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800231
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200232 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800233 list_for_each_entry(t, &xt[af].target, list) {
234 if (strcmp(t->name, name) == 0) {
235 if (t->revision == revision) {
236 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800237 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800238 return t;
239 }
240 } else
241 err = -EPROTOTYPE; /* Found something. */
242 }
243 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800244 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200245
246 if (af != NFPROTO_UNSPEC)
247 /* Try searching again in the family-independent list */
248 return xt_find_target(NFPROTO_UNSPEC, name, revision);
249
Harald Welte2e4e6a12006-01-12 13:30:04 -0800250 return ERR_PTR(err);
251}
252EXPORT_SYMBOL(xt_find_target);
253
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200254struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800255{
256 struct xt_target *target;
257
Eric Dumazetda17c732018-01-24 17:16:09 -0800258 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
259 return ERR_PTR(-EINVAL);
260
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100261 target = xt_find_target(af, name, revision);
262 if (IS_ERR(target)) {
263 request_module("%st_%s", xt_prefix[af], name);
264 target = xt_find_target(af, name, revision);
265 }
266
267 return target;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800268}
269EXPORT_SYMBOL_GPL(xt_request_find_target);
270
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500271
272static int xt_obj_to_user(u16 __user *psize, u16 size,
273 void __user *pname, const char *name,
274 u8 __user *prev, u8 rev)
275{
276 if (put_user(size, psize))
277 return -EFAULT;
278 if (copy_to_user(pname, name, strlen(name) + 1))
279 return -EFAULT;
280 if (put_user(rev, prev))
281 return -EFAULT;
282
283 return 0;
284}
285
286#define XT_OBJ_TO_USER(U, K, TYPE, C_SIZE) \
287 xt_obj_to_user(&U->u.TYPE##_size, C_SIZE ? : K->u.TYPE##_size, \
288 U->u.user.name, K->u.kernel.TYPE->name, \
289 &U->u.user.revision, K->u.kernel.TYPE->revision)
290
291int xt_data_to_user(void __user *dst, const void *src,
Willem de Bruijn324318f2017-05-09 16:17:37 -0400292 int usersize, int size, int aligned_size)
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500293{
294 usersize = usersize ? : size;
295 if (copy_to_user(dst, src, usersize))
296 return -EFAULT;
Willem de Bruijn324318f2017-05-09 16:17:37 -0400297 if (usersize != aligned_size &&
298 clear_user(dst + usersize, aligned_size - usersize))
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500299 return -EFAULT;
300
301 return 0;
302}
303EXPORT_SYMBOL_GPL(xt_data_to_user);
304
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400305#define XT_DATA_TO_USER(U, K, TYPE) \
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500306 xt_data_to_user(U->data, K->data, \
307 K->u.kernel.TYPE->usersize, \
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400308 K->u.kernel.TYPE->TYPE##size, \
309 XT_ALIGN(K->u.kernel.TYPE->TYPE##size))
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500310
311int xt_match_to_user(const struct xt_entry_match *m,
312 struct xt_entry_match __user *u)
313{
314 return XT_OBJ_TO_USER(u, m, match, 0) ||
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400315 XT_DATA_TO_USER(u, m, match);
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500316}
317EXPORT_SYMBOL_GPL(xt_match_to_user);
318
319int xt_target_to_user(const struct xt_entry_target *t,
320 struct xt_entry_target __user *u)
321{
322 return XT_OBJ_TO_USER(u, t, target, 0) ||
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400323 XT_DATA_TO_USER(u, t, target);
Willem de Bruijnf32815d2017-01-02 17:19:40 -0500324}
325EXPORT_SYMBOL_GPL(xt_target_to_user);
326
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200327static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800328{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200329 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800330 int have_rev = 0;
331
332 list_for_each_entry(m, &xt[af].match, list) {
333 if (strcmp(m->name, name) == 0) {
334 if (m->revision > *bestp)
335 *bestp = m->revision;
336 if (m->revision == revision)
337 have_rev = 1;
338 }
339 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000340
341 if (af != NFPROTO_UNSPEC && !have_rev)
342 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
343
Harald Welte2e4e6a12006-01-12 13:30:04 -0800344 return have_rev;
345}
346
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200347static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800348{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200349 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800350 int have_rev = 0;
351
352 list_for_each_entry(t, &xt[af].target, list) {
353 if (strcmp(t->name, name) == 0) {
354 if (t->revision > *bestp)
355 *bestp = t->revision;
356 if (t->revision == revision)
357 have_rev = 1;
358 }
359 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000360
361 if (af != NFPROTO_UNSPEC && !have_rev)
362 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
363
Harald Welte2e4e6a12006-01-12 13:30:04 -0800364 return have_rev;
365}
366
367/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200368int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800369 int *err)
370{
371 int have_rev, best = -1;
372
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200373 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800374 if (target == 1)
375 have_rev = target_revfn(af, name, revision, &best);
376 else
377 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800378 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800379
380 /* Nothing at all? Return 0 to try loading module. */
381 if (best == -1) {
382 *err = -ENOENT;
383 return 0;
384 }
385
386 *err = best;
387 if (!have_rev)
388 *err = -EPROTONOSUPPORT;
389 return 1;
390}
391EXPORT_SYMBOL_GPL(xt_find_revision);
392
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000393static char *
394textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
Jan Engelhardt45185362009-04-05 10:43:09 +0200395{
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000396 static const char *const inetbr_names[] = {
Jan Engelhardt45185362009-04-05 10:43:09 +0200397 "PREROUTING", "INPUT", "FORWARD",
398 "OUTPUT", "POSTROUTING", "BROUTING",
399 };
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000400 static const char *const arp_names[] = {
401 "INPUT", "FORWARD", "OUTPUT",
402 };
403 const char *const *names;
404 unsigned int i, max;
Jan Engelhardt45185362009-04-05 10:43:09 +0200405 char *p = buf;
406 bool np = false;
407 int res;
408
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000409 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
410 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
411 ARRAY_SIZE(inetbr_names);
Jan Engelhardt45185362009-04-05 10:43:09 +0200412 *p = '\0';
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000413 for (i = 0; i < max; ++i) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200414 if (!(mask & (1 << i)))
415 continue;
416 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
417 if (res > 0) {
418 size -= res;
419 p += res;
420 }
421 np = true;
422 }
423
424 return buf;
425}
426
Florian Westphalb1d0a5d2018-03-10 01:15:45 +0100427/**
428 * xt_check_proc_name - check that name is suitable for /proc file creation
429 *
430 * @name: file name candidate
431 * @size: length of buffer
432 *
433 * some x_tables modules wish to create a file in /proc.
434 * This function makes sure that the name is suitable for this
435 * purpose, it checks that name is NUL terminated and isn't a 'special'
436 * name, like "..".
437 *
438 * returns negative number on error or 0 if name is useable.
439 */
440int xt_check_proc_name(const char *name, unsigned int size)
441{
442 if (name[0] == '\0')
443 return -EINVAL;
444
445 if (strnlen(name, size) == size)
446 return -ENAMETOOLONG;
447
448 if (strcmp(name, ".") == 0 ||
449 strcmp(name, "..") == 0 ||
450 strchr(name, '/'))
451 return -EINVAL;
452
453 return 0;
454}
455EXPORT_SYMBOL(xt_check_proc_name);
456
Jan Engelhardt916a9172008-10-08 11:35:20 +0200457int xt_check_match(struct xt_mtchk_param *par,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200458 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800459{
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100460 int ret;
461
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200462 if (XT_ALIGN(par->match->matchsize) != size &&
463 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200464 /*
465 * ebt_among is exempt from centralized matchsize checking
466 * because it uses a dynamic-size data set.
467 */
Florian Westphal1b6cd672018-02-09 15:52:00 +0100468 pr_err_ratelimited("%s_tables: %s.%u match: invalid size %u (kernel) != (user) %u\n",
469 xt_prefix[par->family], par->match->name,
470 par->match->revision,
471 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800472 return -EINVAL;
473 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200474 if (par->match->table != NULL &&
475 strcmp(par->match->table, par->table) != 0) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100476 pr_info_ratelimited("%s_tables: %s match: only valid in %s table, not %s\n",
477 xt_prefix[par->family], par->match->name,
478 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800479 return -EINVAL;
480 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200481 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200482 char used[64], allow[64];
483
Florian Westphal1b6cd672018-02-09 15:52:00 +0100484 pr_info_ratelimited("%s_tables: %s match: used from hooks %s, but only valid from %s\n",
485 xt_prefix[par->family], par->match->name,
486 textify_hooks(used, sizeof(used),
487 par->hook_mask, par->family),
488 textify_hooks(allow, sizeof(allow),
489 par->match->hooks,
490 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800491 return -EINVAL;
492 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200493 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100494 pr_info_ratelimited("%s_tables: %s match: only valid for protocol %u\n",
495 xt_prefix[par->family], par->match->name,
496 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800497 return -EINVAL;
498 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100499 if (par->match->checkentry != NULL) {
500 ret = par->match->checkentry(par);
501 if (ret < 0)
502 return ret;
503 else if (ret > 0)
504 /* Flag up potential errors. */
505 return -EIO;
506 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800507 return 0;
508}
509EXPORT_SYMBOL_GPL(xt_check_match);
510
Florian Westphal13631bf2016-04-01 14:17:29 +0200511/** xt_check_entry_match - check that matches end before start of target
512 *
513 * @match: beginning of xt_entry_match
514 * @target: beginning of this rules target (alleged end of matches)
515 * @alignment: alignment requirement of match structures
516 *
517 * Validates that all matches add up to the beginning of the target,
518 * and that each match covers at least the base structure size.
519 *
520 * Return: 0 on success, negative errno on failure.
521 */
522static int xt_check_entry_match(const char *match, const char *target,
523 const size_t alignment)
524{
525 const struct xt_entry_match *pos;
526 int length = target - match;
527
528 if (length == 0) /* no matches */
529 return 0;
530
531 pos = (struct xt_entry_match *)match;
532 do {
533 if ((unsigned long)pos % alignment)
534 return -EINVAL;
535
536 if (length < (int)sizeof(struct xt_entry_match))
537 return -EINVAL;
538
539 if (pos->u.match_size < sizeof(struct xt_entry_match))
540 return -EINVAL;
541
542 if (pos->u.match_size > length)
543 return -EINVAL;
544
545 length -= pos->u.match_size;
546 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
547 } while (length > 0);
548
549 return 0;
550}
551
Florian Westphal1b293e32018-02-27 19:42:29 +0100552/** xt_check_table_hooks - check hook entry points are sane
553 *
554 * @info xt_table_info to check
555 * @valid_hooks - hook entry points that we can enter from
556 *
557 * Validates that the hook entry and underflows points are set up.
558 *
559 * Return: 0 on success, negative errno on failure.
560 */
561int xt_check_table_hooks(const struct xt_table_info *info, unsigned int valid_hooks)
562{
Florian Westphale816a2c2018-02-27 19:42:30 +0100563 const char *err = "unsorted underflow";
564 unsigned int i, max_uflow, max_entry;
565 bool check_hooks = false;
Florian Westphal1b293e32018-02-27 19:42:29 +0100566
567 BUILD_BUG_ON(ARRAY_SIZE(info->hook_entry) != ARRAY_SIZE(info->underflow));
568
Florian Westphale816a2c2018-02-27 19:42:30 +0100569 max_entry = 0;
570 max_uflow = 0;
571
Florian Westphal1b293e32018-02-27 19:42:29 +0100572 for (i = 0; i < ARRAY_SIZE(info->hook_entry); i++) {
573 if (!(valid_hooks & (1 << i)))
574 continue;
575
576 if (info->hook_entry[i] == 0xFFFFFFFF)
577 return -EINVAL;
578 if (info->underflow[i] == 0xFFFFFFFF)
579 return -EINVAL;
Florian Westphale816a2c2018-02-27 19:42:30 +0100580
581 if (check_hooks) {
582 if (max_uflow > info->underflow[i])
583 goto error;
584
585 if (max_uflow == info->underflow[i]) {
586 err = "duplicate underflow";
587 goto error;
588 }
589 if (max_entry > info->hook_entry[i]) {
590 err = "unsorted entry";
591 goto error;
592 }
593 if (max_entry == info->hook_entry[i]) {
594 err = "duplicate entry";
595 goto error;
596 }
597 }
598 max_entry = info->hook_entry[i];
599 max_uflow = info->underflow[i];
600 check_hooks = true;
Florian Westphal1b293e32018-02-27 19:42:29 +0100601 }
602
603 return 0;
Florian Westphale816a2c2018-02-27 19:42:30 +0100604error:
605 pr_err_ratelimited("%s at hook %d\n", err, i);
606 return -EINVAL;
Florian Westphal1b293e32018-02-27 19:42:29 +0100607}
608EXPORT_SYMBOL(xt_check_table_hooks);
609
Florian Westphal72597132018-03-06 08:26:00 +0100610static bool verdict_ok(int verdict)
611{
612 if (verdict > 0)
613 return true;
614
615 if (verdict < 0) {
616 int v = -verdict - 1;
617
618 if (verdict == XT_RETURN)
619 return true;
620
621 switch (v) {
622 case NF_ACCEPT: return true;
623 case NF_DROP: return true;
624 case NF_QUEUE: return true;
625 default:
626 break;
627 }
628
629 return false;
630 }
631
632 return false;
633}
634
635static bool error_tg_ok(unsigned int usersize, unsigned int kernsize,
636 const char *msg, unsigned int msglen)
637{
638 return usersize == kernsize && strnlen(msg, msglen) < msglen;
639}
640
Dmitry Mishin27229712006-04-01 02:25:19 -0800641#ifdef CONFIG_COMPAT
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100642int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800643{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100644 struct xt_af *xp = &xt[af];
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800645
Florian Westphal89370862018-02-27 19:42:36 +0100646 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
647
Florian Westphal7d7d7e02018-02-27 19:42:35 +0100648 if (WARN_ON(!xp->compat_tab))
649 return -ENOMEM;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100650
651 if (xp->cur >= xp->number)
652 return -EINVAL;
653
654 if (xp->cur)
655 delta += xp->compat_tab[xp->cur - 1].delta;
656 xp->compat_tab[xp->cur].offset = offset;
657 xp->compat_tab[xp->cur].delta = delta;
658 xp->cur++;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800659 return 0;
660}
661EXPORT_SYMBOL_GPL(xt_compat_add_offset);
662
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200663void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800664{
Florian Westphal89370862018-02-27 19:42:36 +0100665 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
666
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100667 if (xt[af].compat_tab) {
668 vfree(xt[af].compat_tab);
669 xt[af].compat_tab = NULL;
670 xt[af].number = 0;
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200671 xt[af].cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800672 }
673}
674EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
675
Florian Westphal3e5e5242010-02-15 18:17:10 +0100676int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800677{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100678 struct compat_delta *tmp = xt[af].compat_tab;
679 int mid, left = 0, right = xt[af].cur - 1;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800680
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100681 while (left <= right) {
682 mid = (left + right) >> 1;
683 if (offset > tmp[mid].offset)
684 left = mid + 1;
685 else if (offset < tmp[mid].offset)
686 right = mid - 1;
687 else
688 return mid ? tmp[mid - 1].delta : 0;
689 }
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200690 return left ? tmp[left - 1].delta : 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800691}
692EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
693
Florian Westphal9782a112018-02-27 19:42:34 +0100694int xt_compat_init_offsets(u8 af, unsigned int number)
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100695{
Florian Westphal7d7d7e02018-02-27 19:42:35 +0100696 size_t mem;
697
Florian Westphal89370862018-02-27 19:42:36 +0100698 WARN_ON(!mutex_is_locked(&xt[af].compat_mutex));
699
Florian Westphal7d7d7e02018-02-27 19:42:35 +0100700 if (!number || number > (INT_MAX / sizeof(struct compat_delta)))
701 return -EINVAL;
702
703 if (WARN_ON(xt[af].compat_tab))
704 return -EINVAL;
705
706 mem = sizeof(struct compat_delta) * number;
707 if (mem > XT_MAX_TABLE_SIZE)
708 return -ENOMEM;
709
710 xt[af].compat_tab = vmalloc(mem);
711 if (!xt[af].compat_tab)
712 return -ENOMEM;
713
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100714 xt[af].number = number;
715 xt[af].cur = 0;
Florian Westphal9782a112018-02-27 19:42:34 +0100716
717 return 0;
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100718}
719EXPORT_SYMBOL(xt_compat_init_offsets);
720
Jan Engelhardt5452e422008-04-14 11:15:35 +0200721int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800722{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700723 u_int16_t csize = match->compatsize ? : match->matchsize;
724 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800725}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700726EXPORT_SYMBOL_GPL(xt_compat_match_offset);
727
Florian Westphal01883462016-04-01 14:17:33 +0200728void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
729 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700730{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200731 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700732 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
733 int pad, off = xt_compat_match_offset(match);
734 u_int16_t msize = cm->u.user.match_size;
Florian Westphal09d96862016-04-01 14:17:34 +0200735 char name[sizeof(m->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700736
737 m = *dstptr;
738 memcpy(m, cm, sizeof(*cm));
739 if (match->compat_from_user)
740 match->compat_from_user(m->data, cm->data);
741 else
742 memcpy(m->data, cm->data, msize - sizeof(*cm));
743 pad = XT_ALIGN(match->matchsize) - match->matchsize;
744 if (pad > 0)
745 memset(m->data + match->matchsize, 0, pad);
746
747 msize += off;
748 m->u.user.match_size = msize;
Florian Westphal09d96862016-04-01 14:17:34 +0200749 strlcpy(name, match->name, sizeof(name));
750 module_put(match->me);
751 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700752
753 *size += off;
754 *dstptr += msize;
755}
756EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
757
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400758#define COMPAT_XT_DATA_TO_USER(U, K, TYPE, C_SIZE) \
759 xt_data_to_user(U->data, K->data, \
760 K->u.kernel.TYPE->usersize, \
761 C_SIZE, \
762 COMPAT_XT_ALIGN(C_SIZE))
763
Jan Engelhardt739674f2009-06-26 08:23:19 +0200764int xt_compat_match_to_user(const struct xt_entry_match *m,
765 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700766{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200767 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700768 struct compat_xt_entry_match __user *cm = *dstptr;
769 int off = xt_compat_match_offset(match);
770 u_int16_t msize = m->u.user.match_size - off;
771
Willem de Bruijn4915f7b2017-01-02 17:19:45 -0500772 if (XT_OBJ_TO_USER(cm, m, match, msize))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800773 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700774
775 if (match->compat_to_user) {
776 if (match->compat_to_user((void __user *)cm->data, m->data))
777 return -EFAULT;
778 } else {
Willem de Bruijn751a9c72017-05-17 11:24:47 -0400779 if (COMPAT_XT_DATA_TO_USER(cm, m, match, msize - sizeof(*cm)))
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700780 return -EFAULT;
781 }
782
783 *size -= off;
784 *dstptr += msize;
785 return 0;
786}
787EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
Florian Westphalfc1221b2016-04-01 14:17:26 +0200788
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200789/* non-compat version may have padding after verdict */
790struct compat_xt_standard_target {
791 struct compat_xt_entry_target t;
792 compat_uint_t verdict;
793};
794
Florian Westphal472ebdc2018-02-27 19:42:28 +0100795struct compat_xt_error_target {
796 struct compat_xt_entry_target t;
797 char errorname[XT_FUNCTION_MAXNAMELEN];
798};
799
Florian Westphalce683e52016-04-01 14:17:28 +0200800int xt_compat_check_entry_offsets(const void *base, const char *elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +0200801 unsigned int target_offset,
802 unsigned int next_offset)
803{
Florian Westphalce683e52016-04-01 14:17:28 +0200804 long size_of_base_struct = elems - (const char *)base;
Florian Westphalfc1221b2016-04-01 14:17:26 +0200805 const struct compat_xt_entry_target *t;
806 const char *e = base;
807
Florian Westphalce683e52016-04-01 14:17:28 +0200808 if (target_offset < size_of_base_struct)
809 return -EINVAL;
810
Florian Westphalfc1221b2016-04-01 14:17:26 +0200811 if (target_offset + sizeof(*t) > next_offset)
812 return -EINVAL;
813
814 t = (void *)(e + target_offset);
815 if (t->u.target_size < sizeof(*t))
816 return -EINVAL;
817
818 if (target_offset + t->u.target_size > next_offset)
819 return -EINVAL;
820
Florian Westphal07a9da52018-02-27 19:42:27 +0100821 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
822 const struct compat_xt_standard_target *st = (const void *)t;
823
824 if (COMPAT_XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
825 return -EINVAL;
826
827 if (!verdict_ok(st->verdict))
828 return -EINVAL;
Florian Westphal472ebdc2018-02-27 19:42:28 +0100829 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
830 const struct compat_xt_error_target *et = (const void *)t;
831
832 if (!error_tg_ok(t->u.target_size, sizeof(*et),
833 et->errorname, sizeof(et->errorname)))
834 return -EINVAL;
Florian Westphal07a9da52018-02-27 19:42:27 +0100835 }
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200836
Masahiro Yamada550116d2017-02-27 14:28:58 -0800837 /* compat_xt_entry match has less strict alignment requirements,
Florian Westphal13631bf2016-04-01 14:17:29 +0200838 * otherwise they are identical. In case of padding differences
839 * we need to add compat version of xt_check_entry_match.
840 */
841 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
842
843 return xt_check_entry_match(elems, base + target_offset,
844 __alignof__(struct compat_xt_entry_match));
Florian Westphalfc1221b2016-04-01 14:17:26 +0200845}
846EXPORT_SYMBOL(xt_compat_check_entry_offsets);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700847#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800848
Florian Westphal7d358122016-04-01 14:17:23 +0200849/**
850 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
851 *
852 * @base: pointer to arp/ip/ip6t_entry
Florian Westphalce683e52016-04-01 14:17:28 +0200853 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
Florian Westphal7d358122016-04-01 14:17:23 +0200854 * @target_offset: the arp/ip/ip6_t->target_offset
855 * @next_offset: the arp/ip/ip6_t->next_offset
856 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200857 * validates that target_offset and next_offset are sane and that all
858 * match sizes (if any) align with the target offset.
Florian Westphal7d358122016-04-01 14:17:23 +0200859 *
Florian Westphalce683e52016-04-01 14:17:28 +0200860 * This function does not validate the targets or matches themselves, it
Florian Westphal13631bf2016-04-01 14:17:29 +0200861 * only tests that all the offsets and sizes are correct, that all
862 * match structures are aligned, and that the last structure ends where
863 * the target structure begins.
864 *
865 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
Florian Westphalce683e52016-04-01 14:17:28 +0200866 *
Florian Westphal7d358122016-04-01 14:17:23 +0200867 * The arp/ip/ip6t_entry structure @base must have passed following tests:
868 * - it must point to a valid memory location
869 * - base to base + next_offset must be accessible, i.e. not exceed allocated
870 * length.
871 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200872 * A well-formed entry looks like this:
873 *
874 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
875 * e->elems[]-----' | |
876 * matchsize | |
877 * matchsize | |
878 * | |
879 * target_offset---------------------------------' |
880 * next_offset---------------------------------------------------'
881 *
882 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
883 * This is where matches (if any) and the target reside.
884 * target_offset: beginning of target.
885 * next_offset: start of the next rule; also: size of this rule.
886 * Since targets have a minimum size, target_offset + minlen <= next_offset.
887 *
888 * Every match stores its size, sum of sizes must not exceed target_offset.
889 *
Florian Westphal7d358122016-04-01 14:17:23 +0200890 * Return: 0 on success, negative errno on failure.
891 */
892int xt_check_entry_offsets(const void *base,
Florian Westphalce683e52016-04-01 14:17:28 +0200893 const char *elems,
Florian Westphal7d358122016-04-01 14:17:23 +0200894 unsigned int target_offset,
895 unsigned int next_offset)
896{
Florian Westphalce683e52016-04-01 14:17:28 +0200897 long size_of_base_struct = elems - (const char *)base;
Florian Westphal7d358122016-04-01 14:17:23 +0200898 const struct xt_entry_target *t;
899 const char *e = base;
900
Florian Westphalce683e52016-04-01 14:17:28 +0200901 /* target start is within the ip/ip6/arpt_entry struct */
902 if (target_offset < size_of_base_struct)
903 return -EINVAL;
904
Florian Westphal7d358122016-04-01 14:17:23 +0200905 if (target_offset + sizeof(*t) > next_offset)
906 return -EINVAL;
907
908 t = (void *)(e + target_offset);
Florian Westphala08e4e192016-04-01 14:17:25 +0200909 if (t->u.target_size < sizeof(*t))
910 return -EINVAL;
911
Florian Westphal7d358122016-04-01 14:17:23 +0200912 if (target_offset + t->u.target_size > next_offset)
913 return -EINVAL;
914
Florian Westphal07a9da52018-02-27 19:42:27 +0100915 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0) {
916 const struct xt_standard_target *st = (const void *)t;
917
918 if (XT_ALIGN(target_offset + sizeof(*st)) != next_offset)
919 return -EINVAL;
920
921 if (!verdict_ok(st->verdict))
922 return -EINVAL;
Florian Westphal472ebdc2018-02-27 19:42:28 +0100923 } else if (strcmp(t->u.user.name, XT_ERROR_TARGET) == 0) {
924 const struct xt_error_target *et = (const void *)t;
925
926 if (!error_tg_ok(t->u.target_size, sizeof(*et),
927 et->errorname, sizeof(et->errorname)))
928 return -EINVAL;
Florian Westphal07a9da52018-02-27 19:42:27 +0100929 }
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200930
Florian Westphal13631bf2016-04-01 14:17:29 +0200931 return xt_check_entry_match(elems, base + target_offset,
932 __alignof__(struct xt_entry_match));
Florian Westphal7d358122016-04-01 14:17:23 +0200933}
934EXPORT_SYMBOL(xt_check_entry_offsets);
935
Florian Westphalf4dc7772016-07-14 17:51:26 +0200936/**
937 * xt_alloc_entry_offsets - allocate array to store rule head offsets
938 *
939 * @size: number of entries
940 *
941 * Return: NULL or kmalloc'd or vmalloc'd array
942 */
943unsigned int *xt_alloc_entry_offsets(unsigned int size)
944{
Florian Westphal9d5c12a2018-02-27 19:42:32 +0100945 if (size > XT_MAX_TABLE_SIZE / sizeof(unsigned int))
946 return NULL;
947
Michal Hocko752ade62017-05-08 15:57:27 -0700948 return kvmalloc_array(size, sizeof(unsigned int), GFP_KERNEL | __GFP_ZERO);
Florian Westphalf4dc7772016-07-14 17:51:26 +0200949
Florian Westphalf4dc7772016-07-14 17:51:26 +0200950}
951EXPORT_SYMBOL(xt_alloc_entry_offsets);
952
953/**
954 * xt_find_jump_offset - check if target is a valid jump offset
955 *
956 * @offsets: array containing all valid rule start offsets of a rule blob
957 * @target: the jump target to search for
958 * @size: entries in @offset
959 */
960bool xt_find_jump_offset(const unsigned int *offsets,
961 unsigned int target, unsigned int size)
962{
963 int m, low = 0, hi = size;
964
965 while (hi > low) {
966 m = (low + hi) / 2u;
967
968 if (offsets[m] > target)
969 hi = m;
970 else if (offsets[m] < target)
971 low = m + 1;
972 else
973 return true;
974 }
975
976 return false;
977}
978EXPORT_SYMBOL(xt_find_jump_offset);
979
Jan Engelhardt916a9172008-10-08 11:35:20 +0200980int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200981 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800982{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100983 int ret;
984
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200985 if (XT_ALIGN(par->target->targetsize) != size) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100986 pr_err_ratelimited("%s_tables: %s.%u target: invalid size %u (kernel) != (user) %u\n",
987 xt_prefix[par->family], par->target->name,
988 par->target->revision,
989 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800990 return -EINVAL;
991 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200992 if (par->target->table != NULL &&
993 strcmp(par->target->table, par->table) != 0) {
Florian Westphal1b6cd672018-02-09 15:52:00 +0100994 pr_info_ratelimited("%s_tables: %s target: only valid in %s table, not %s\n",
995 xt_prefix[par->family], par->target->name,
996 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800997 return -EINVAL;
998 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200999 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +02001000 char used[64], allow[64];
1001
Florian Westphal1b6cd672018-02-09 15:52:00 +01001002 pr_info_ratelimited("%s_tables: %s target: used from hooks %s, but only usable from %s\n",
1003 xt_prefix[par->family], par->target->name,
1004 textify_hooks(used, sizeof(used),
1005 par->hook_mask, par->family),
1006 textify_hooks(allow, sizeof(allow),
1007 par->target->hooks,
1008 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -08001009 return -EINVAL;
1010 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +02001011 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Florian Westphal1b6cd672018-02-09 15:52:00 +01001012 pr_info_ratelimited("%s_tables: %s target: only valid for protocol %u\n",
1013 xt_prefix[par->family], par->target->name,
1014 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -08001015 return -EINVAL;
1016 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +01001017 if (par->target->checkentry != NULL) {
1018 ret = par->target->checkentry(par);
1019 if (ret < 0)
1020 return ret;
1021 else if (ret > 0)
1022 /* Flag up potential errors. */
1023 return -EIO;
1024 }
Patrick McHardy37f9f732006-03-20 17:59:06 -08001025 return 0;
1026}
1027EXPORT_SYMBOL_GPL(xt_check_target);
1028
Florian Westphald7591f02016-04-01 15:37:59 +02001029/**
1030 * xt_copy_counters_from_user - copy counters and metadata from userspace
1031 *
1032 * @user: src pointer to userspace memory
1033 * @len: alleged size of userspace memory
1034 * @info: where to store the xt_counters_info metadata
1035 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
1036 *
1037 * Copies counter meta data from @user and stores it in @info.
1038 *
1039 * vmallocs memory to hold the counters, then copies the counter data
1040 * from @user to the new memory and returns a pointer to it.
1041 *
1042 * If @compat is true, @info gets converted automatically to the 64bit
1043 * representation.
1044 *
1045 * The metadata associated with the counters is stored in @info.
1046 *
1047 * Return: returns pointer that caller has to test via IS_ERR().
1048 * If IS_ERR is false, caller has to vfree the pointer.
1049 */
1050void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
1051 struct xt_counters_info *info, bool compat)
1052{
1053 void *mem;
1054 u64 size;
1055
1056#ifdef CONFIG_COMPAT
1057 if (compat) {
1058 /* structures only differ in size due to alignment */
1059 struct compat_xt_counters_info compat_tmp;
1060
1061 if (len <= sizeof(compat_tmp))
1062 return ERR_PTR(-EINVAL);
1063
1064 len -= sizeof(compat_tmp);
1065 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
1066 return ERR_PTR(-EFAULT);
1067
Eric Dumazete466af72017-10-05 02:50:07 -07001068 memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
Florian Westphald7591f02016-04-01 15:37:59 +02001069 info->num_counters = compat_tmp.num_counters;
1070 user += sizeof(compat_tmp);
1071 } else
1072#endif
1073 {
1074 if (len <= sizeof(*info))
1075 return ERR_PTR(-EINVAL);
1076
1077 len -= sizeof(*info);
1078 if (copy_from_user(info, user, sizeof(*info)) != 0)
1079 return ERR_PTR(-EFAULT);
1080
Florian Westphald7591f02016-04-01 15:37:59 +02001081 user += sizeof(*info);
1082 }
Eric Dumazete466af72017-10-05 02:50:07 -07001083 info->name[sizeof(info->name) - 1] = '\0';
Florian Westphald7591f02016-04-01 15:37:59 +02001084
1085 size = sizeof(struct xt_counters);
1086 size *= info->num_counters;
1087
1088 if (size != (u64)len)
1089 return ERR_PTR(-EINVAL);
1090
1091 mem = vmalloc(len);
1092 if (!mem)
1093 return ERR_PTR(-ENOMEM);
1094
1095 if (copy_from_user(mem, user, len) == 0)
1096 return mem;
1097
1098 vfree(mem);
1099 return ERR_PTR(-EFAULT);
1100}
1101EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
1102
Dmitry Mishin27229712006-04-01 02:25:19 -08001103#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +02001104int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -08001105{
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001106 u_int16_t csize = target->compatsize ? : target->targetsize;
1107 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -08001108}
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001109EXPORT_SYMBOL_GPL(xt_compat_target_offset);
1110
1111void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -08001112 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001113{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001114 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001115 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
1116 int pad, off = xt_compat_target_offset(target);
1117 u_int16_t tsize = ct->u.user.target_size;
Florian Westphal09d96862016-04-01 14:17:34 +02001118 char name[sizeof(t->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001119
1120 t = *dstptr;
1121 memcpy(t, ct, sizeof(*ct));
1122 if (target->compat_from_user)
1123 target->compat_from_user(t->data, ct->data);
1124 else
1125 memcpy(t->data, ct->data, tsize - sizeof(*ct));
1126 pad = XT_ALIGN(target->targetsize) - target->targetsize;
1127 if (pad > 0)
1128 memset(t->data + target->targetsize, 0, pad);
1129
1130 tsize += off;
1131 t->u.user.target_size = tsize;
Florian Westphal09d96862016-04-01 14:17:34 +02001132 strlcpy(name, target->name, sizeof(name));
1133 module_put(target->me);
1134 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001135
1136 *size += off;
1137 *dstptr += tsize;
1138}
1139EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
1140
Jan Engelhardt739674f2009-06-26 08:23:19 +02001141int xt_compat_target_to_user(const struct xt_entry_target *t,
1142 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001143{
Jan Engelhardt5452e422008-04-14 11:15:35 +02001144 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001145 struct compat_xt_entry_target __user *ct = *dstptr;
1146 int off = xt_compat_target_offset(target);
1147 u_int16_t tsize = t->u.user.target_size - off;
1148
Willem de Bruijn4915f7b2017-01-02 17:19:45 -05001149 if (XT_OBJ_TO_USER(ct, t, target, tsize))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001150 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001151
1152 if (target->compat_to_user) {
1153 if (target->compat_to_user((void __user *)ct->data, t->data))
1154 return -EFAULT;
1155 } else {
Willem de Bruijn751a9c72017-05-17 11:24:47 -04001156 if (COMPAT_XT_DATA_TO_USER(ct, t, target, tsize - sizeof(*ct)))
Patrick McHardy9fa492c2006-09-20 12:05:37 -07001157 return -EFAULT;
1158 }
1159
1160 *size -= off;
1161 *dstptr += tsize;
1162 return 0;
1163}
1164EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -08001165#endif
1166
Harald Welte2e4e6a12006-01-12 13:30:04 -08001167struct xt_table_info *xt_alloc_table_info(unsigned int size)
1168{
Eric Dumazet711bdde2015-06-15 09:57:30 -07001169 struct xt_table_info *info = NULL;
1170 size_t sz = sizeof(*info) + size;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001171
Florian Westphal19926962018-02-27 19:42:31 +01001172 if (sz < sizeof(*info) || sz >= XT_MAX_TABLE_SIZE)
Florian Westphald157bd72016-03-10 01:56:23 +01001173 return NULL;
1174
Michal Hocko05372502018-01-30 11:30:11 -08001175 /* __GFP_NORETRY is not fully supported by kvmalloc but it should
1176 * work reasonably well if sz is too large and bail out rather
1177 * than shoot all processes down before realizing there is nothing
1178 * more to reclaim.
1179 */
1180 info = kvmalloc(sz, GFP_KERNEL | __GFP_NORETRY);
Michal Hockoeacd86c2017-07-12 14:35:37 -07001181 if (!info)
1182 return NULL;
1183
Eric Dumazet711bdde2015-06-15 09:57:30 -07001184 memset(info, 0, sizeof(*info));
1185 info->size = size;
1186 return info;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001187}
1188EXPORT_SYMBOL(xt_alloc_table_info);
1189
1190void xt_free_table_info(struct xt_table_info *info)
1191{
1192 int cpu;
1193
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001194 if (info->jumpstack != NULL) {
Eric Dumazetf6b50822014-06-24 02:15:35 -07001195 for_each_possible_cpu(cpu)
1196 kvfree(info->jumpstack[cpu]);
1197 kvfree(info->jumpstack);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001198 }
1199
Eric Dumazet711bdde2015-06-15 09:57:30 -07001200 kvfree(info);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001201}
1202EXPORT_SYMBOL(xt_free_table_info);
1203
Florian Westphal03d13b62017-12-08 17:01:53 +01001204/* Find table by name, grabs mutex & ref. Returns ERR_PTR on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001205struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
1206 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001207{
Florian Westphalb9e69e12016-02-25 10:08:36 +01001208 struct xt_table *t, *found = NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001209
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001210 mutex_lock(&xt[af].mutex);
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001211 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001212 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1213 return t;
Florian Westphalb9e69e12016-02-25 10:08:36 +01001214
1215 if (net == &init_net)
1216 goto out;
1217
1218 /* Table doesn't exist in this netns, re-try init */
1219 list_for_each_entry(t, &init_net.xt.tables[af], list) {
Florian Westphal03d13b62017-12-08 17:01:53 +01001220 int err;
1221
Florian Westphalb9e69e12016-02-25 10:08:36 +01001222 if (strcmp(t->name, name))
1223 continue;
Florian Westphal03d13b62017-12-08 17:01:53 +01001224 if (!try_module_get(t->me))
1225 goto out;
Florian Westphalb9e69e12016-02-25 10:08:36 +01001226 mutex_unlock(&xt[af].mutex);
Florian Westphal03d13b62017-12-08 17:01:53 +01001227 err = t->table_init(net);
1228 if (err < 0) {
Florian Westphalb9e69e12016-02-25 10:08:36 +01001229 module_put(t->me);
Florian Westphal03d13b62017-12-08 17:01:53 +01001230 return ERR_PTR(err);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001231 }
1232
1233 found = t;
1234
1235 mutex_lock(&xt[af].mutex);
1236 break;
1237 }
1238
1239 if (!found)
1240 goto out;
1241
1242 /* and once again: */
1243 list_for_each_entry(t, &net->xt.tables[af], list)
1244 if (strcmp(t->name, name) == 0)
1245 return t;
1246
1247 module_put(found->me);
1248 out:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001249 mutex_unlock(&xt[af].mutex);
Florian Westphal03d13b62017-12-08 17:01:53 +01001250 return ERR_PTR(-ENOENT);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001251}
1252EXPORT_SYMBOL_GPL(xt_find_table_lock);
1253
Florian Westphal03d13b62017-12-08 17:01:53 +01001254struct xt_table *xt_request_find_table_lock(struct net *net, u_int8_t af,
1255 const char *name)
1256{
1257 struct xt_table *t = xt_find_table_lock(net, af, name);
1258
Florian Westphal20651ce2018-01-09 14:30:48 +01001259#ifdef CONFIG_MODULES
Florian Westphal03d13b62017-12-08 17:01:53 +01001260 if (IS_ERR(t)) {
1261 int err = request_module("%stable_%s", xt_prefix[af], name);
Florian Westphale3eeacb2018-01-13 14:06:08 +01001262 if (err < 0)
Florian Westphal03d13b62017-12-08 17:01:53 +01001263 return ERR_PTR(err);
1264 t = xt_find_table_lock(net, af, name);
1265 }
1266#endif
1267
1268 return t;
1269}
1270EXPORT_SYMBOL_GPL(xt_request_find_table_lock);
1271
Harald Welte2e4e6a12006-01-12 13:30:04 -08001272void xt_table_unlock(struct xt_table *table)
1273{
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001274 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001275}
1276EXPORT_SYMBOL_GPL(xt_table_unlock);
1277
Dmitry Mishin27229712006-04-01 02:25:19 -08001278#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001279void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001280{
1281 mutex_lock(&xt[af].compat_mutex);
1282}
1283EXPORT_SYMBOL_GPL(xt_compat_lock);
1284
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001285void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001286{
1287 mutex_unlock(&xt[af].compat_mutex);
1288}
1289EXPORT_SYMBOL_GPL(xt_compat_unlock);
1290#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001291
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001292DEFINE_PER_CPU(seqcount_t, xt_recseq);
1293EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001294
Florian Westphaldcebd312015-07-14 17:51:09 +02001295struct static_key xt_tee_enabled __read_mostly;
1296EXPORT_SYMBOL_GPL(xt_tee_enabled);
1297
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001298static int xt_jumpstack_alloc(struct xt_table_info *i)
1299{
1300 unsigned int size;
1301 int cpu;
1302
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001303 size = sizeof(void **) * nr_cpu_ids;
1304 if (size > PAGE_SIZE)
Michal Hocko752ade62017-05-08 15:57:27 -07001305 i->jumpstack = kvzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001306 else
Joe Perches3dbd4432011-05-28 10:36:35 -07001307 i->jumpstack = kzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001308 if (i->jumpstack == NULL)
1309 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001310
Florian Westphal98d1bd82015-07-14 17:51:06 +02001311 /* ruleset without jumps -- no stack needed */
1312 if (i->stacksize == 0)
1313 return 0;
1314
Florian Westphal7814b6e2015-07-14 17:51:08 +02001315 /* Jumpstack needs to be able to record two full callchains, one
1316 * from the first rule set traversal, plus one table reentrancy
1317 * via -j TEE without clobbering the callchain that brought us to
1318 * TEE target.
1319 *
1320 * This is done by allocating two jumpstacks per cpu, on reentry
1321 * the upper half of the stack is used.
1322 *
1323 * see the jumpstack setup in ipt_do_table() for more details.
1324 */
1325 size = sizeof(void *) * i->stacksize * 2u;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001326 for_each_possible_cpu(cpu) {
Michal Hocko752ade62017-05-08 15:57:27 -07001327 i->jumpstack[cpu] = kvmalloc_node(size, GFP_KERNEL,
1328 cpu_to_node(cpu));
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001329 if (i->jumpstack[cpu] == NULL)
1330 /*
1331 * Freeing will be done later on by the callers. The
1332 * chain is: xt_replace_table -> __do_replace ->
1333 * do_replace -> xt_free_table_info.
1334 */
1335 return -ENOMEM;
1336 }
1337
1338 return 0;
1339}
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001340
Florian Westphalc84ca952018-02-27 19:42:33 +01001341struct xt_counters *xt_counters_alloc(unsigned int counters)
1342{
1343 struct xt_counters *mem;
1344
1345 if (counters == 0 || counters > INT_MAX / sizeof(*mem))
1346 return NULL;
1347
1348 counters *= sizeof(*mem);
1349 if (counters > XT_MAX_TABLE_SIZE)
1350 return NULL;
1351
1352 return vzalloc(counters);
1353}
1354EXPORT_SYMBOL(xt_counters_alloc);
1355
Harald Welte2e4e6a12006-01-12 13:30:04 -08001356struct xt_table_info *
1357xt_replace_table(struct xt_table *table,
1358 unsigned int num_counters,
1359 struct xt_table_info *newinfo,
1360 int *error)
1361{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001362 struct xt_table_info *private;
Florian Westphal80055da2017-10-12 01:13:50 +02001363 unsigned int cpu;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001364 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001365
Jan Engelhardtd97a9e42010-04-21 14:45:51 +02001366 ret = xt_jumpstack_alloc(newinfo);
1367 if (ret < 0) {
1368 *error = ret;
1369 return NULL;
1370 }
1371
Harald Welte2e4e6a12006-01-12 13:30:04 -08001372 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001373 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001374 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001375
Harald Welte2e4e6a12006-01-12 13:30:04 -08001376 /* Check inside lock: is the old number correct? */
1377 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001378 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001379 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001380 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001381 *error = -EAGAIN;
1382 return NULL;
1383 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001384
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001385 newinfo->initial_entries = private->initial_entries;
Will Deaconb416c142013-10-21 13:14:53 +01001386 /*
1387 * Ensure contents of newinfo are visible before assigning to
1388 * private.
1389 */
1390 smp_wmb();
1391 table->private = newinfo;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001392
Florian Westphal80055da2017-10-12 01:13:50 +02001393 /* make sure all cpus see new ->private value */
1394 smp_wmb();
1395
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001396 /*
1397 * Even though table entries have now been swapped, other CPU's
Florian Westphal80055da2017-10-12 01:13:50 +02001398 * may still be using the old entries...
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001399 */
1400 local_bh_enable();
1401
Florian Westphal80055da2017-10-12 01:13:50 +02001402 /* ... so wait for even xt_recseq on all cpus */
1403 for_each_possible_cpu(cpu) {
1404 seqcount_t *s = &per_cpu(xt_recseq, cpu);
1405 u32 seq = raw_read_seqcount(s);
1406
1407 if (seq & 1) {
1408 do {
1409 cond_resched();
1410 cpu_relax();
1411 } while (seq == raw_read_seqcount(s));
1412 }
1413 }
1414
Thomas Graffbabf312011-01-16 18:12:59 +01001415#ifdef CONFIG_AUDIT
1416 if (audit_enabled) {
Geliang Tang46b20c32017-08-07 21:44:25 +08001417 audit_log(current->audit_context, GFP_KERNEL,
1418 AUDIT_NETFILTER_CFG,
1419 "table=%s family=%u entries=%u",
1420 table->name, table->af, private->number);
Thomas Graffbabf312011-01-16 18:12:59 +01001421 }
1422#endif
1423
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001424 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001425}
1426EXPORT_SYMBOL_GPL(xt_replace_table);
1427
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001428struct xt_table *xt_register_table(struct net *net,
1429 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -08001430 struct xt_table_info *bootstrap,
1431 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001432{
1433 int ret;
1434 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001435 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001436
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001437 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001438 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001439 if (!table) {
1440 ret = -ENOMEM;
1441 goto out;
1442 }
1443
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001444 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001445 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001446 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -07001447 if (strcmp(t->name, table->name) == 0) {
1448 ret = -EEXIST;
1449 goto unlock;
1450 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001451 }
1452
1453 /* Simplifies replace_table code. */
1454 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +01001455
Harald Welte2e4e6a12006-01-12 13:30:04 -08001456 if (!xt_replace_table(table, 0, newinfo, &ret))
1457 goto unlock;
1458
1459 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001460 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001461
1462 /* save number of initial entries */
1463 private->initial_entries = private->number;
1464
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001465 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001466 mutex_unlock(&xt[table->af].mutex);
1467 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001468
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001469unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001470 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001471 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001472out:
1473 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001474}
1475EXPORT_SYMBOL_GPL(xt_register_table);
1476
1477void *xt_unregister_table(struct xt_table *table)
1478{
1479 struct xt_table_info *private;
1480
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001481 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001482 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -07001483 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001484 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001485 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001486
1487 return private;
1488}
1489EXPORT_SYMBOL_GPL(xt_unregister_table);
1490
1491#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001492struct xt_names_priv {
1493 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001494 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001495};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001496static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001497{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001498 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001499 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001500 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001501
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001502 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001503 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001504}
1505
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001506static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001507{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001508 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001509 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001510 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001511
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001512 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001513}
1514
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001515static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001516{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001517 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001518 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001519
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001520 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001521}
1522
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001523static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001524{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001525 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001526
Joe Perches861fb102015-05-12 18:28:23 -07001527 if (*table->name)
Steven Rostedt (Red Hat)e71456ae2014-10-27 17:43:45 -04001528 seq_printf(seq, "%s\n", table->name);
Joe Perches861fb102015-05-12 18:28:23 -07001529 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001530}
1531
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001532static const struct seq_operations xt_table_seq_ops = {
1533 .start = xt_table_seq_start,
1534 .next = xt_table_seq_next,
1535 .stop = xt_table_seq_stop,
1536 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001537};
1538
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001539static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001540{
1541 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001542 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001543
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001544 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1545 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001546 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001547 priv = ((struct seq_file *)file->private_data)->private;
Al Virod9dda782013-03-31 18:16:14 -04001548 priv->af = (unsigned long)PDE_DATA(inode);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001549 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001550 return ret;
1551}
1552
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001553static const struct file_operations xt_table_ops = {
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001554 .open = xt_table_open,
1555 .read = seq_read,
1556 .llseek = seq_lseek,
Pavel Emelyanov0e93bb942008-04-29 03:15:35 -07001557 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001558};
1559
Jan Engelhardteb132202009-02-18 16:42:19 +01001560/*
1561 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1562 * the multi-AF mutexes.
1563 */
1564struct nf_mttg_trav {
1565 struct list_head *head, *curr;
1566 uint8_t class, nfproto;
1567};
1568
1569enum {
1570 MTTG_TRAV_INIT,
1571 MTTG_TRAV_NFP_UNSPEC,
1572 MTTG_TRAV_NFP_SPEC,
1573 MTTG_TRAV_DONE,
1574};
1575
1576static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1577 bool is_target)
1578{
1579 static const uint8_t next_class[] = {
1580 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1581 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1582 };
1583 struct nf_mttg_trav *trav = seq->private;
1584
1585 switch (trav->class) {
1586 case MTTG_TRAV_INIT:
1587 trav->class = MTTG_TRAV_NFP_UNSPEC;
1588 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1589 trav->head = trav->curr = is_target ?
1590 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1591 break;
1592 case MTTG_TRAV_NFP_UNSPEC:
1593 trav->curr = trav->curr->next;
1594 if (trav->curr != trav->head)
1595 break;
1596 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1597 mutex_lock(&xt[trav->nfproto].mutex);
1598 trav->head = trav->curr = is_target ?
1599 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1600 trav->class = next_class[trav->class];
1601 break;
1602 case MTTG_TRAV_NFP_SPEC:
1603 trav->curr = trav->curr->next;
1604 if (trav->curr != trav->head)
1605 break;
Gustavo A. R. Silvae8542dc2017-11-07 08:19:29 -06001606 /* fall through */
Jan Engelhardteb132202009-02-18 16:42:19 +01001607 default:
1608 return NULL;
1609 }
1610
1611 if (ppos != NULL)
1612 ++*ppos;
1613 return trav;
1614}
1615
1616static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1617 bool is_target)
1618{
1619 struct nf_mttg_trav *trav = seq->private;
1620 unsigned int j;
1621
1622 trav->class = MTTG_TRAV_INIT;
1623 for (j = 0; j < *pos; ++j)
1624 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1625 return NULL;
1626 return trav;
1627}
1628
1629static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1630{
1631 struct nf_mttg_trav *trav = seq->private;
1632
1633 switch (trav->class) {
1634 case MTTG_TRAV_NFP_UNSPEC:
1635 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1636 break;
1637 case MTTG_TRAV_NFP_SPEC:
1638 mutex_unlock(&xt[trav->nfproto].mutex);
1639 break;
1640 }
1641}
1642
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001643static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1644{
Jan Engelhardteb132202009-02-18 16:42:19 +01001645 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001646}
1647
Jan Engelhardteb132202009-02-18 16:42:19 +01001648static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001649{
Jan Engelhardteb132202009-02-18 16:42:19 +01001650 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001651}
1652
1653static int xt_match_seq_show(struct seq_file *seq, void *v)
1654{
Jan Engelhardteb132202009-02-18 16:42:19 +01001655 const struct nf_mttg_trav *trav = seq->private;
1656 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001657
Jan Engelhardteb132202009-02-18 16:42:19 +01001658 switch (trav->class) {
1659 case MTTG_TRAV_NFP_UNSPEC:
1660 case MTTG_TRAV_NFP_SPEC:
1661 if (trav->curr == trav->head)
1662 return 0;
1663 match = list_entry(trav->curr, struct xt_match, list);
Joe Perches861fb102015-05-12 18:28:23 -07001664 if (*match->name)
1665 seq_printf(seq, "%s\n", match->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001666 }
1667 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001668}
1669
1670static const struct seq_operations xt_match_seq_ops = {
1671 .start = xt_match_seq_start,
1672 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001673 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001674 .show = xt_match_seq_show,
1675};
1676
1677static int xt_match_open(struct inode *inode, struct file *file)
1678{
Jan Engelhardteb132202009-02-18 16:42:19 +01001679 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001680 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1681 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001682 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001683
Al Virod9dda782013-03-31 18:16:14 -04001684 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001685 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001686}
1687
1688static const struct file_operations xt_match_ops = {
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001689 .open = xt_match_open,
1690 .read = seq_read,
1691 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001692 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001693};
1694
1695static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1696{
Jan Engelhardteb132202009-02-18 16:42:19 +01001697 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001698}
1699
Jan Engelhardteb132202009-02-18 16:42:19 +01001700static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001701{
Jan Engelhardteb132202009-02-18 16:42:19 +01001702 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001703}
1704
1705static int xt_target_seq_show(struct seq_file *seq, void *v)
1706{
Jan Engelhardteb132202009-02-18 16:42:19 +01001707 const struct nf_mttg_trav *trav = seq->private;
1708 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001709
Jan Engelhardteb132202009-02-18 16:42:19 +01001710 switch (trav->class) {
1711 case MTTG_TRAV_NFP_UNSPEC:
1712 case MTTG_TRAV_NFP_SPEC:
1713 if (trav->curr == trav->head)
1714 return 0;
1715 target = list_entry(trav->curr, struct xt_target, list);
Joe Perches861fb102015-05-12 18:28:23 -07001716 if (*target->name)
1717 seq_printf(seq, "%s\n", target->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001718 }
1719 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001720}
1721
1722static const struct seq_operations xt_target_seq_ops = {
1723 .start = xt_target_seq_start,
1724 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001725 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001726 .show = xt_target_seq_show,
1727};
1728
1729static int xt_target_open(struct inode *inode, struct file *file)
1730{
Jan Engelhardteb132202009-02-18 16:42:19 +01001731 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001732 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1733 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001734 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001735
Al Virod9dda782013-03-31 18:16:14 -04001736 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001737 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001738}
1739
1740static const struct file_operations xt_target_ops = {
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001741 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001742 .read = seq_read,
1743 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001744 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001745};
1746
1747#define FORMAT_TABLES "_tables_names"
1748#define FORMAT_MATCHES "_tables_matches"
1749#define FORMAT_TARGETS "_tables_targets"
1750
1751#endif /* CONFIG_PROC_FS */
1752
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001753/**
Florian Westphalb9e69e12016-02-25 10:08:36 +01001754 * xt_hook_ops_alloc - set up hooks for a new table
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001755 * @table: table with metadata needed to set up hooks
1756 * @fn: Hook function
1757 *
Florian Westphalb9e69e12016-02-25 10:08:36 +01001758 * This function will create the nf_hook_ops that the x_table needs
1759 * to hand to xt_hook_link_net().
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001760 */
Florian Westphalb9e69e12016-02-25 10:08:36 +01001761struct nf_hook_ops *
1762xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001763{
1764 unsigned int hook_mask = table->valid_hooks;
1765 uint8_t i, num_hooks = hweight32(hook_mask);
1766 uint8_t hooknum;
1767 struct nf_hook_ops *ops;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001768
Xiubo Lia6d0bae2016-06-02 10:59:56 +08001769 if (!num_hooks)
1770 return ERR_PTR(-EINVAL);
1771
Florian Westphal1ecc2812016-10-17 21:50:23 +02001772 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001773 if (ops == NULL)
1774 return ERR_PTR(-ENOMEM);
1775
1776 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1777 hook_mask >>= 1, ++hooknum) {
1778 if (!(hook_mask & 1))
1779 continue;
1780 ops[i].hook = fn;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001781 ops[i].pf = table->af;
1782 ops[i].hooknum = hooknum;
1783 ops[i].priority = table->priority;
1784 ++i;
1785 }
1786
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001787 return ops;
1788}
Florian Westphalb9e69e12016-02-25 10:08:36 +01001789EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001790
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001791int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001792{
1793#ifdef CONFIG_PROC_FS
1794 char buf[XT_FUNCTION_MAXNAMELEN];
1795 struct proc_dir_entry *proc;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001796 kuid_t root_uid;
1797 kgid_t root_gid;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001798#endif
1799
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001800 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001801 return -EINVAL;
1802
1803
1804#ifdef CONFIG_PROC_FS
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001805 root_uid = make_kuid(net->user_ns, 0);
1806 root_gid = make_kgid(net->user_ns, 0);
1807
Tobias Klauserce18afe2007-03-14 16:36:16 -07001808 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001809 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001810 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1811 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001812 if (!proc)
1813 goto out;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001814 if (uid_valid(root_uid) && gid_valid(root_gid))
1815 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001816
Tobias Klauserce18afe2007-03-14 16:36:16 -07001817 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001818 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001819 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1820 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001821 if (!proc)
1822 goto out_remove_tables;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001823 if (uid_valid(root_uid) && gid_valid(root_gid))
1824 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001825
Tobias Klauserce18afe2007-03-14 16:36:16 -07001826 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001827 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001828 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1829 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001830 if (!proc)
1831 goto out_remove_matches;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001832 if (uid_valid(root_uid) && gid_valid(root_gid))
1833 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001834#endif
1835
1836 return 0;
1837
1838#ifdef CONFIG_PROC_FS
1839out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001840 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001841 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001842 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001843
1844out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001845 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001846 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001847 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001848out:
1849 return -1;
1850#endif
1851}
1852EXPORT_SYMBOL_GPL(xt_proto_init);
1853
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001854void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001855{
1856#ifdef CONFIG_PROC_FS
1857 char buf[XT_FUNCTION_MAXNAMELEN];
1858
Tobias Klauserce18afe2007-03-14 16:36:16 -07001859 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001860 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001861 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001862
Tobias Klauserce18afe2007-03-14 16:36:16 -07001863 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001864 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001865 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001866
Tobias Klauserce18afe2007-03-14 16:36:16 -07001867 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001868 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001869 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001870#endif /*CONFIG_PROC_FS*/
1871}
1872EXPORT_SYMBOL_GPL(xt_proto_fini);
1873
Florian Westphalf28e15b2016-11-22 14:44:18 +01001874/**
1875 * xt_percpu_counter_alloc - allocate x_tables rule counter
1876 *
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001877 * @state: pointer to xt_percpu allocation state
Florian Westphalf28e15b2016-11-22 14:44:18 +01001878 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1879 *
1880 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1881 * contain the address of the real (percpu) counter.
1882 *
1883 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1884 * to fetch the real percpu counter.
1885 *
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001886 * To speed up allocation and improve data locality, a 4kb block is
Ben Hutchings9ba5c402018-03-29 15:12:41 +01001887 * allocated. Freeing any counter may free an entire block, so all
1888 * counters allocated using the same state must be freed at the same
1889 * time.
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001890 *
1891 * xt_percpu_counter_alloc_state contains the base address of the
1892 * allocated page and the current sub-offset.
1893 *
Florian Westphalf28e15b2016-11-22 14:44:18 +01001894 * returns false on error.
1895 */
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001896bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1897 struct xt_counters *counter)
Florian Westphalf28e15b2016-11-22 14:44:18 +01001898{
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001899 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
Florian Westphalf28e15b2016-11-22 14:44:18 +01001900
1901 if (nr_cpu_ids <= 1)
1902 return true;
1903
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001904 if (!state->mem) {
1905 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1906 XT_PCPU_BLOCK_SIZE);
1907 if (!state->mem)
1908 return false;
1909 }
1910 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1911 state->off += sizeof(*counter);
1912 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1913 state->mem = NULL;
1914 state->off = 0;
1915 }
Florian Westphalf28e15b2016-11-22 14:44:18 +01001916 return true;
1917}
1918EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1919
Florian Westphal4d31eef2016-11-22 14:44:17 +01001920void xt_percpu_counter_free(struct xt_counters *counters)
1921{
1922 unsigned long pcnt = counters->pcnt;
1923
Florian Westphalae0ac0e2016-11-22 14:44:19 +01001924 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
Florian Westphal4d31eef2016-11-22 14:44:17 +01001925 free_percpu((void __percpu *)pcnt);
1926}
1927EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1928
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001929static int __net_init xt_net_init(struct net *net)
1930{
1931 int i;
1932
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001933 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001934 INIT_LIST_HEAD(&net->xt.tables[i]);
1935 return 0;
1936}
1937
Vasily Averin613d0772017-11-12 14:32:37 +03001938static void __net_exit xt_net_exit(struct net *net)
1939{
1940 int i;
1941
1942 for (i = 0; i < NFPROTO_NUMPROTO; i++)
1943 WARN_ON_ONCE(!list_empty(&net->xt.tables[i]));
1944}
1945
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001946static struct pernet_operations xt_net_ops = {
1947 .init = xt_net_init,
Vasily Averin613d0772017-11-12 14:32:37 +03001948 .exit = xt_net_exit,
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001949};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001950
1951static int __init xt_init(void)
1952{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001953 unsigned int i;
1954 int rv;
1955
1956 for_each_possible_cpu(i) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001957 seqcount_init(&per_cpu(xt_recseq, i));
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001958 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001959
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001960 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001961 if (!xt)
1962 return -ENOMEM;
1963
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001964 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001965 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001966#ifdef CONFIG_COMPAT
1967 mutex_init(&xt[i].compat_mutex);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001968 xt[i].compat_tab = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001969#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001970 INIT_LIST_HEAD(&xt[i].target);
1971 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001972 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001973 rv = register_pernet_subsys(&xt_net_ops);
1974 if (rv < 0)
1975 kfree(xt);
1976 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001977}
1978
1979static void __exit xt_fini(void)
1980{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001981 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001982 kfree(xt);
1983}
1984
1985module_init(xt_init);
1986module_exit(xt_fini);
1987