blob: 2675d580c490b3de07562ce9174a0b4f69d55035 [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
42#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
43
Patrick McHardyb386d9f2007-12-17 21:47:48 -080044struct compat_delta {
Eric Dumazet255d0dc2010-12-18 18:35:15 +010045 unsigned int offset; /* offset in kernel */
46 int delta; /* delta in 32bit user land */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080047};
48
Harald Welte2e4e6a12006-01-12 13:30:04 -080049struct xt_af {
Ingo Molnar9e19bb62006-03-25 01:41:10 -080050 struct mutex mutex;
Harald Welte2e4e6a12006-01-12 13:30:04 -080051 struct list_head match;
52 struct list_head target;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080053#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080054 struct mutex compat_mutex;
Eric Dumazet255d0dc2010-12-18 18:35:15 +010055 struct compat_delta *compat_tab;
56 unsigned int number; /* number of slots in compat_tab[] */
57 unsigned int cur; /* number of used slots in compat_tab[] */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080058#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080059};
60
61static struct xt_af *xt;
62
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020063static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
64 [NFPROTO_UNSPEC] = "x",
65 [NFPROTO_IPV4] = "ip",
66 [NFPROTO_ARP] = "arp",
67 [NFPROTO_BRIDGE] = "eb",
68 [NFPROTO_IPV6] = "ip6",
Patrick McHardy37f9f732006-03-20 17:59:06 -080069};
70
Harald Welte2e4e6a12006-01-12 13:30:04 -080071/* Registration hooks for targets. */
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020072int xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080073{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020074 u_int8_t af = target->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -080075
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020076 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080077 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080078 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020079 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -080080}
81EXPORT_SYMBOL(xt_register_target);
82
83void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080084xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080085{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020086 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080087
Ingo Molnar9e19bb62006-03-25 01:41:10 -080088 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070089 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080090 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080091}
92EXPORT_SYMBOL(xt_unregister_target);
93
94int
Patrick McHardy52d9c422006-08-22 00:33:45 -070095xt_register_targets(struct xt_target *target, unsigned int n)
96{
97 unsigned int i;
98 int err = 0;
99
100 for (i = 0; i < n; i++) {
101 err = xt_register_target(&target[i]);
102 if (err)
103 goto err;
104 }
105 return err;
106
107err:
108 if (i > 0)
109 xt_unregister_targets(target, i);
110 return err;
111}
112EXPORT_SYMBOL(xt_register_targets);
113
114void
115xt_unregister_targets(struct xt_target *target, unsigned int n)
116{
Changli Gaof68c5302010-10-04 22:24:12 +0200117 while (n-- > 0)
118 xt_unregister_target(&target[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700119}
120EXPORT_SYMBOL(xt_unregister_targets);
121
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200122int xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800123{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200124 u_int8_t af = match->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800125
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200126 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800127 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800128 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200129 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800130}
131EXPORT_SYMBOL(xt_register_match);
132
133void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800134xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800135{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200136 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800137
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800138 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700139 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800140 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800141}
142EXPORT_SYMBOL(xt_unregister_match);
143
Patrick McHardy52d9c422006-08-22 00:33:45 -0700144int
145xt_register_matches(struct xt_match *match, unsigned int n)
146{
147 unsigned int i;
148 int err = 0;
149
150 for (i = 0; i < n; i++) {
151 err = xt_register_match(&match[i]);
152 if (err)
153 goto err;
154 }
155 return err;
156
157err:
158 if (i > 0)
159 xt_unregister_matches(match, i);
160 return err;
161}
162EXPORT_SYMBOL(xt_register_matches);
163
164void
165xt_unregister_matches(struct xt_match *match, unsigned int n)
166{
Changli Gaof68c5302010-10-04 22:24:12 +0200167 while (n-- > 0)
168 xt_unregister_match(&match[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700169}
170EXPORT_SYMBOL(xt_unregister_matches);
171
Harald Welte2e4e6a12006-01-12 13:30:04 -0800172
173/*
174 * These are weird, but module loading must not be done with mutex
175 * held (since they will register), and we have to have a single
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100176 * function to use.
Harald Welte2e4e6a12006-01-12 13:30:04 -0800177 */
178
179/* Find match, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200180struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800181{
182 struct xt_match *m;
Patrick McHardy42046e22011-03-14 19:11:44 +0100183 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800184
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200185 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800186 list_for_each_entry(m, &xt[af].match, list) {
187 if (strcmp(m->name, name) == 0) {
188 if (m->revision == revision) {
189 if (try_module_get(m->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800190 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800191 return m;
192 }
193 } else
194 err = -EPROTOTYPE; /* Found something. */
195 }
196 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800197 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200198
199 if (af != NFPROTO_UNSPEC)
200 /* Try searching again in the family-independent list */
201 return xt_find_match(NFPROTO_UNSPEC, name, revision);
202
Harald Welte2e4e6a12006-01-12 13:30:04 -0800203 return ERR_PTR(err);
204}
205EXPORT_SYMBOL(xt_find_match);
206
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200207struct xt_match *
208xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
209{
210 struct xt_match *match;
211
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100212 match = xt_find_match(nfproto, name, revision);
213 if (IS_ERR(match)) {
214 request_module("%st_%s", xt_prefix[nfproto], name);
215 match = xt_find_match(nfproto, name, revision);
216 }
217
218 return match;
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200219}
220EXPORT_SYMBOL_GPL(xt_request_find_match);
221
Harald Welte2e4e6a12006-01-12 13:30:04 -0800222/* Find target, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200223struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800224{
225 struct xt_target *t;
Patrick McHardy42046e22011-03-14 19:11:44 +0100226 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800227
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200228 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800229 list_for_each_entry(t, &xt[af].target, list) {
230 if (strcmp(t->name, name) == 0) {
231 if (t->revision == revision) {
232 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800233 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800234 return t;
235 }
236 } else
237 err = -EPROTOTYPE; /* Found something. */
238 }
239 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800240 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200241
242 if (af != NFPROTO_UNSPEC)
243 /* Try searching again in the family-independent list */
244 return xt_find_target(NFPROTO_UNSPEC, name, revision);
245
Harald Welte2e4e6a12006-01-12 13:30:04 -0800246 return ERR_PTR(err);
247}
248EXPORT_SYMBOL(xt_find_target);
249
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200250struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800251{
252 struct xt_target *target;
253
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100254 target = xt_find_target(af, name, revision);
255 if (IS_ERR(target)) {
256 request_module("%st_%s", xt_prefix[af], name);
257 target = xt_find_target(af, name, revision);
258 }
259
260 return target;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800261}
262EXPORT_SYMBOL_GPL(xt_request_find_target);
263
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200264static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800265{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200266 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800267 int have_rev = 0;
268
269 list_for_each_entry(m, &xt[af].match, list) {
270 if (strcmp(m->name, name) == 0) {
271 if (m->revision > *bestp)
272 *bestp = m->revision;
273 if (m->revision == revision)
274 have_rev = 1;
275 }
276 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000277
278 if (af != NFPROTO_UNSPEC && !have_rev)
279 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
280
Harald Welte2e4e6a12006-01-12 13:30:04 -0800281 return have_rev;
282}
283
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200284static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800285{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200286 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800287 int have_rev = 0;
288
289 list_for_each_entry(t, &xt[af].target, list) {
290 if (strcmp(t->name, name) == 0) {
291 if (t->revision > *bestp)
292 *bestp = t->revision;
293 if (t->revision == revision)
294 have_rev = 1;
295 }
296 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000297
298 if (af != NFPROTO_UNSPEC && !have_rev)
299 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
300
Harald Welte2e4e6a12006-01-12 13:30:04 -0800301 return have_rev;
302}
303
304/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200305int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800306 int *err)
307{
308 int have_rev, best = -1;
309
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200310 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800311 if (target == 1)
312 have_rev = target_revfn(af, name, revision, &best);
313 else
314 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800315 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800316
317 /* Nothing at all? Return 0 to try loading module. */
318 if (best == -1) {
319 *err = -ENOENT;
320 return 0;
321 }
322
323 *err = best;
324 if (!have_rev)
325 *err = -EPROTONOSUPPORT;
326 return 1;
327}
328EXPORT_SYMBOL_GPL(xt_find_revision);
329
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000330static char *
331textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
Jan Engelhardt45185362009-04-05 10:43:09 +0200332{
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000333 static const char *const inetbr_names[] = {
Jan Engelhardt45185362009-04-05 10:43:09 +0200334 "PREROUTING", "INPUT", "FORWARD",
335 "OUTPUT", "POSTROUTING", "BROUTING",
336 };
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000337 static const char *const arp_names[] = {
338 "INPUT", "FORWARD", "OUTPUT",
339 };
340 const char *const *names;
341 unsigned int i, max;
Jan Engelhardt45185362009-04-05 10:43:09 +0200342 char *p = buf;
343 bool np = false;
344 int res;
345
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000346 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
347 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
348 ARRAY_SIZE(inetbr_names);
Jan Engelhardt45185362009-04-05 10:43:09 +0200349 *p = '\0';
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000350 for (i = 0; i < max; ++i) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200351 if (!(mask & (1 << i)))
352 continue;
353 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
354 if (res > 0) {
355 size -= res;
356 p += res;
357 }
358 np = true;
359 }
360
361 return buf;
362}
363
Jan Engelhardt916a9172008-10-08 11:35:20 +0200364int xt_check_match(struct xt_mtchk_param *par,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200365 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800366{
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100367 int ret;
368
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200369 if (XT_ALIGN(par->match->matchsize) != size &&
370 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200371 /*
372 * ebt_among is exempt from centralized matchsize checking
373 * because it uses a dynamic-size data set.
374 */
Jan Engelhardtb4024052009-06-25 18:32:12 +0200375 pr_err("%s_tables: %s.%u match: invalid size "
376 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200377 xt_prefix[par->family], par->match->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200378 par->match->revision,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200379 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800380 return -EINVAL;
381 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200382 if (par->match->table != NULL &&
383 strcmp(par->match->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200384 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200385 xt_prefix[par->family], par->match->name,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200386 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800387 return -EINVAL;
388 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200389 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200390 char used[64], allow[64];
391
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200392 pr_err("%s_tables: %s match: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200393 "valid from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200394 xt_prefix[par->family], par->match->name,
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000395 textify_hooks(used, sizeof(used), par->hook_mask,
396 par->family),
397 textify_hooks(allow, sizeof(allow), par->match->hooks,
398 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800399 return -EINVAL;
400 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200401 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200402 pr_err("%s_tables: %s match: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200403 xt_prefix[par->family], par->match->name,
404 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800405 return -EINVAL;
406 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100407 if (par->match->checkentry != NULL) {
408 ret = par->match->checkentry(par);
409 if (ret < 0)
410 return ret;
411 else if (ret > 0)
412 /* Flag up potential errors. */
413 return -EIO;
414 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800415 return 0;
416}
417EXPORT_SYMBOL_GPL(xt_check_match);
418
Florian Westphal13631bf2016-04-01 14:17:29 +0200419/** xt_check_entry_match - check that matches end before start of target
420 *
421 * @match: beginning of xt_entry_match
422 * @target: beginning of this rules target (alleged end of matches)
423 * @alignment: alignment requirement of match structures
424 *
425 * Validates that all matches add up to the beginning of the target,
426 * and that each match covers at least the base structure size.
427 *
428 * Return: 0 on success, negative errno on failure.
429 */
430static int xt_check_entry_match(const char *match, const char *target,
431 const size_t alignment)
432{
433 const struct xt_entry_match *pos;
434 int length = target - match;
435
436 if (length == 0) /* no matches */
437 return 0;
438
439 pos = (struct xt_entry_match *)match;
440 do {
441 if ((unsigned long)pos % alignment)
442 return -EINVAL;
443
444 if (length < (int)sizeof(struct xt_entry_match))
445 return -EINVAL;
446
447 if (pos->u.match_size < sizeof(struct xt_entry_match))
448 return -EINVAL;
449
450 if (pos->u.match_size > length)
451 return -EINVAL;
452
453 length -= pos->u.match_size;
454 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
455 } while (length > 0);
456
457 return 0;
458}
459
Dmitry Mishin27229712006-04-01 02:25:19 -0800460#ifdef CONFIG_COMPAT
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100461int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800462{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100463 struct xt_af *xp = &xt[af];
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800464
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100465 if (!xp->compat_tab) {
466 if (!xp->number)
467 return -EINVAL;
468 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
469 if (!xp->compat_tab)
470 return -ENOMEM;
471 xp->cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800472 }
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100473
474 if (xp->cur >= xp->number)
475 return -EINVAL;
476
477 if (xp->cur)
478 delta += xp->compat_tab[xp->cur - 1].delta;
479 xp->compat_tab[xp->cur].offset = offset;
480 xp->compat_tab[xp->cur].delta = delta;
481 xp->cur++;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800482 return 0;
483}
484EXPORT_SYMBOL_GPL(xt_compat_add_offset);
485
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200486void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800487{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100488 if (xt[af].compat_tab) {
489 vfree(xt[af].compat_tab);
490 xt[af].compat_tab = NULL;
491 xt[af].number = 0;
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200492 xt[af].cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800493 }
494}
495EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
496
Florian Westphal3e5e5242010-02-15 18:17:10 +0100497int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800498{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100499 struct compat_delta *tmp = xt[af].compat_tab;
500 int mid, left = 0, right = xt[af].cur - 1;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800501
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100502 while (left <= right) {
503 mid = (left + right) >> 1;
504 if (offset > tmp[mid].offset)
505 left = mid + 1;
506 else if (offset < tmp[mid].offset)
507 right = mid - 1;
508 else
509 return mid ? tmp[mid - 1].delta : 0;
510 }
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200511 return left ? tmp[left - 1].delta : 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800512}
513EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
514
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100515void xt_compat_init_offsets(u_int8_t af, unsigned int number)
516{
517 xt[af].number = number;
518 xt[af].cur = 0;
519}
520EXPORT_SYMBOL(xt_compat_init_offsets);
521
Jan Engelhardt5452e422008-04-14 11:15:35 +0200522int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800523{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700524 u_int16_t csize = match->compatsize ? : match->matchsize;
525 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800526}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700527EXPORT_SYMBOL_GPL(xt_compat_match_offset);
528
Florian Westphal01883462016-04-01 14:17:33 +0200529void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
530 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700531{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200532 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700533 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
534 int pad, off = xt_compat_match_offset(match);
535 u_int16_t msize = cm->u.user.match_size;
Florian Westphal09d96862016-04-01 14:17:34 +0200536 char name[sizeof(m->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700537
538 m = *dstptr;
539 memcpy(m, cm, sizeof(*cm));
540 if (match->compat_from_user)
541 match->compat_from_user(m->data, cm->data);
542 else
543 memcpy(m->data, cm->data, msize - sizeof(*cm));
544 pad = XT_ALIGN(match->matchsize) - match->matchsize;
545 if (pad > 0)
546 memset(m->data + match->matchsize, 0, pad);
547
548 msize += off;
549 m->u.user.match_size = msize;
Florian Westphal09d96862016-04-01 14:17:34 +0200550 strlcpy(name, match->name, sizeof(name));
551 module_put(match->me);
552 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700553
554 *size += off;
555 *dstptr += msize;
556}
557EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
558
Jan Engelhardt739674f2009-06-26 08:23:19 +0200559int xt_compat_match_to_user(const struct xt_entry_match *m,
560 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700561{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200562 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700563 struct compat_xt_entry_match __user *cm = *dstptr;
564 int off = xt_compat_match_offset(match);
565 u_int16_t msize = m->u.user.match_size - off;
566
567 if (copy_to_user(cm, m, sizeof(*cm)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800568 put_user(msize, &cm->u.user.match_size) ||
569 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
570 strlen(m->u.kernel.match->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800571 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700572
573 if (match->compat_to_user) {
574 if (match->compat_to_user((void __user *)cm->data, m->data))
575 return -EFAULT;
576 } else {
577 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
578 return -EFAULT;
579 }
580
581 *size -= off;
582 *dstptr += msize;
583 return 0;
584}
585EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
Florian Westphalfc1221b2016-04-01 14:17:26 +0200586
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200587/* non-compat version may have padding after verdict */
588struct compat_xt_standard_target {
589 struct compat_xt_entry_target t;
590 compat_uint_t verdict;
591};
592
Florian Westphalce683e52016-04-01 14:17:28 +0200593int xt_compat_check_entry_offsets(const void *base, const char *elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +0200594 unsigned int target_offset,
595 unsigned int next_offset)
596{
Florian Westphalce683e52016-04-01 14:17:28 +0200597 long size_of_base_struct = elems - (const char *)base;
Florian Westphalfc1221b2016-04-01 14:17:26 +0200598 const struct compat_xt_entry_target *t;
599 const char *e = base;
600
Florian Westphalce683e52016-04-01 14:17:28 +0200601 if (target_offset < size_of_base_struct)
602 return -EINVAL;
603
Florian Westphalfc1221b2016-04-01 14:17:26 +0200604 if (target_offset + sizeof(*t) > next_offset)
605 return -EINVAL;
606
607 t = (void *)(e + target_offset);
608 if (t->u.target_size < sizeof(*t))
609 return -EINVAL;
610
611 if (target_offset + t->u.target_size > next_offset)
612 return -EINVAL;
613
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200614 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
Florian Westphal7b7eba02016-06-01 02:04:44 +0200615 COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200616 return -EINVAL;
617
Florian Westphal13631bf2016-04-01 14:17:29 +0200618 /* compat_xt_entry match has less strict aligment requirements,
619 * otherwise they are identical. In case of padding differences
620 * we need to add compat version of xt_check_entry_match.
621 */
622 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
623
624 return xt_check_entry_match(elems, base + target_offset,
625 __alignof__(struct compat_xt_entry_match));
Florian Westphalfc1221b2016-04-01 14:17:26 +0200626}
627EXPORT_SYMBOL(xt_compat_check_entry_offsets);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700628#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800629
Florian Westphal7d358122016-04-01 14:17:23 +0200630/**
631 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
632 *
633 * @base: pointer to arp/ip/ip6t_entry
Florian Westphalce683e52016-04-01 14:17:28 +0200634 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
Florian Westphal7d358122016-04-01 14:17:23 +0200635 * @target_offset: the arp/ip/ip6_t->target_offset
636 * @next_offset: the arp/ip/ip6_t->next_offset
637 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200638 * validates that target_offset and next_offset are sane and that all
639 * match sizes (if any) align with the target offset.
Florian Westphal7d358122016-04-01 14:17:23 +0200640 *
Florian Westphalce683e52016-04-01 14:17:28 +0200641 * This function does not validate the targets or matches themselves, it
Florian Westphal13631bf2016-04-01 14:17:29 +0200642 * only tests that all the offsets and sizes are correct, that all
643 * match structures are aligned, and that the last structure ends where
644 * the target structure begins.
645 *
646 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
Florian Westphalce683e52016-04-01 14:17:28 +0200647 *
Florian Westphal7d358122016-04-01 14:17:23 +0200648 * The arp/ip/ip6t_entry structure @base must have passed following tests:
649 * - it must point to a valid memory location
650 * - base to base + next_offset must be accessible, i.e. not exceed allocated
651 * length.
652 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200653 * A well-formed entry looks like this:
654 *
655 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
656 * e->elems[]-----' | |
657 * matchsize | |
658 * matchsize | |
659 * | |
660 * target_offset---------------------------------' |
661 * next_offset---------------------------------------------------'
662 *
663 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
664 * This is where matches (if any) and the target reside.
665 * target_offset: beginning of target.
666 * next_offset: start of the next rule; also: size of this rule.
667 * Since targets have a minimum size, target_offset + minlen <= next_offset.
668 *
669 * Every match stores its size, sum of sizes must not exceed target_offset.
670 *
Florian Westphal7d358122016-04-01 14:17:23 +0200671 * Return: 0 on success, negative errno on failure.
672 */
673int xt_check_entry_offsets(const void *base,
Florian Westphalce683e52016-04-01 14:17:28 +0200674 const char *elems,
Florian Westphal7d358122016-04-01 14:17:23 +0200675 unsigned int target_offset,
676 unsigned int next_offset)
677{
Florian Westphalce683e52016-04-01 14:17:28 +0200678 long size_of_base_struct = elems - (const char *)base;
Florian Westphal7d358122016-04-01 14:17:23 +0200679 const struct xt_entry_target *t;
680 const char *e = base;
681
Florian Westphalce683e52016-04-01 14:17:28 +0200682 /* target start is within the ip/ip6/arpt_entry struct */
683 if (target_offset < size_of_base_struct)
684 return -EINVAL;
685
Florian Westphal7d358122016-04-01 14:17:23 +0200686 if (target_offset + sizeof(*t) > next_offset)
687 return -EINVAL;
688
689 t = (void *)(e + target_offset);
Florian Westphala08e4e192016-04-01 14:17:25 +0200690 if (t->u.target_size < sizeof(*t))
691 return -EINVAL;
692
Florian Westphal7d358122016-04-01 14:17:23 +0200693 if (target_offset + t->u.target_size > next_offset)
694 return -EINVAL;
695
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200696 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
Florian Westphal7b7eba02016-06-01 02:04:44 +0200697 XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200698 return -EINVAL;
699
Florian Westphal13631bf2016-04-01 14:17:29 +0200700 return xt_check_entry_match(elems, base + target_offset,
701 __alignof__(struct xt_entry_match));
Florian Westphal7d358122016-04-01 14:17:23 +0200702}
703EXPORT_SYMBOL(xt_check_entry_offsets);
704
Jan Engelhardt916a9172008-10-08 11:35:20 +0200705int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200706 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800707{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100708 int ret;
709
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200710 if (XT_ALIGN(par->target->targetsize) != size) {
Jan Engelhardtb4024052009-06-25 18:32:12 +0200711 pr_err("%s_tables: %s.%u target: invalid size "
712 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200713 xt_prefix[par->family], par->target->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200714 par->target->revision,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200715 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800716 return -EINVAL;
717 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200718 if (par->target->table != NULL &&
719 strcmp(par->target->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200720 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200721 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200722 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800723 return -EINVAL;
724 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200725 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200726 char used[64], allow[64];
727
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200728 pr_err("%s_tables: %s target: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200729 "usable from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200730 xt_prefix[par->family], par->target->name,
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000731 textify_hooks(used, sizeof(used), par->hook_mask,
732 par->family),
733 textify_hooks(allow, sizeof(allow), par->target->hooks,
734 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800735 return -EINVAL;
736 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200737 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200738 pr_err("%s_tables: %s target: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200739 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200740 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800741 return -EINVAL;
742 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100743 if (par->target->checkentry != NULL) {
744 ret = par->target->checkentry(par);
745 if (ret < 0)
746 return ret;
747 else if (ret > 0)
748 /* Flag up potential errors. */
749 return -EIO;
750 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800751 return 0;
752}
753EXPORT_SYMBOL_GPL(xt_check_target);
754
Florian Westphald7591f02016-04-01 15:37:59 +0200755/**
756 * xt_copy_counters_from_user - copy counters and metadata from userspace
757 *
758 * @user: src pointer to userspace memory
759 * @len: alleged size of userspace memory
760 * @info: where to store the xt_counters_info metadata
761 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
762 *
763 * Copies counter meta data from @user and stores it in @info.
764 *
765 * vmallocs memory to hold the counters, then copies the counter data
766 * from @user to the new memory and returns a pointer to it.
767 *
768 * If @compat is true, @info gets converted automatically to the 64bit
769 * representation.
770 *
771 * The metadata associated with the counters is stored in @info.
772 *
773 * Return: returns pointer that caller has to test via IS_ERR().
774 * If IS_ERR is false, caller has to vfree the pointer.
775 */
776void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
777 struct xt_counters_info *info, bool compat)
778{
779 void *mem;
780 u64 size;
781
782#ifdef CONFIG_COMPAT
783 if (compat) {
784 /* structures only differ in size due to alignment */
785 struct compat_xt_counters_info compat_tmp;
786
787 if (len <= sizeof(compat_tmp))
788 return ERR_PTR(-EINVAL);
789
790 len -= sizeof(compat_tmp);
791 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
792 return ERR_PTR(-EFAULT);
793
794 strlcpy(info->name, compat_tmp.name, sizeof(info->name));
795 info->num_counters = compat_tmp.num_counters;
796 user += sizeof(compat_tmp);
797 } else
798#endif
799 {
800 if (len <= sizeof(*info))
801 return ERR_PTR(-EINVAL);
802
803 len -= sizeof(*info);
804 if (copy_from_user(info, user, sizeof(*info)) != 0)
805 return ERR_PTR(-EFAULT);
806
807 info->name[sizeof(info->name) - 1] = '\0';
808 user += sizeof(*info);
809 }
810
811 size = sizeof(struct xt_counters);
812 size *= info->num_counters;
813
814 if (size != (u64)len)
815 return ERR_PTR(-EINVAL);
816
817 mem = vmalloc(len);
818 if (!mem)
819 return ERR_PTR(-ENOMEM);
820
821 if (copy_from_user(mem, user, len) == 0)
822 return mem;
823
824 vfree(mem);
825 return ERR_PTR(-EFAULT);
826}
827EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
828
Dmitry Mishin27229712006-04-01 02:25:19 -0800829#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +0200830int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800831{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700832 u_int16_t csize = target->compatsize ? : target->targetsize;
833 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800834}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700835EXPORT_SYMBOL_GPL(xt_compat_target_offset);
836
837void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800838 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700839{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200840 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700841 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
842 int pad, off = xt_compat_target_offset(target);
843 u_int16_t tsize = ct->u.user.target_size;
Florian Westphal09d96862016-04-01 14:17:34 +0200844 char name[sizeof(t->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700845
846 t = *dstptr;
847 memcpy(t, ct, sizeof(*ct));
848 if (target->compat_from_user)
849 target->compat_from_user(t->data, ct->data);
850 else
851 memcpy(t->data, ct->data, tsize - sizeof(*ct));
852 pad = XT_ALIGN(target->targetsize) - target->targetsize;
853 if (pad > 0)
854 memset(t->data + target->targetsize, 0, pad);
855
856 tsize += off;
857 t->u.user.target_size = tsize;
Florian Westphal09d96862016-04-01 14:17:34 +0200858 strlcpy(name, target->name, sizeof(name));
859 module_put(target->me);
860 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700861
862 *size += off;
863 *dstptr += tsize;
864}
865EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
866
Jan Engelhardt739674f2009-06-26 08:23:19 +0200867int xt_compat_target_to_user(const struct xt_entry_target *t,
868 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700869{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200870 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700871 struct compat_xt_entry_target __user *ct = *dstptr;
872 int off = xt_compat_target_offset(target);
873 u_int16_t tsize = t->u.user.target_size - off;
874
875 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800876 put_user(tsize, &ct->u.user.target_size) ||
877 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
878 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800879 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700880
881 if (target->compat_to_user) {
882 if (target->compat_to_user((void __user *)ct->data, t->data))
883 return -EFAULT;
884 } else {
885 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
886 return -EFAULT;
887 }
888
889 *size -= off;
890 *dstptr += tsize;
891 return 0;
892}
893EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -0800894#endif
895
Harald Welte2e4e6a12006-01-12 13:30:04 -0800896struct xt_table_info *xt_alloc_table_info(unsigned int size)
897{
Eric Dumazet711bdde62015-06-15 09:57:30 -0700898 struct xt_table_info *info = NULL;
899 size_t sz = sizeof(*info) + size;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800900
Florian Westphald157bd72016-03-10 01:56:23 +0100901 if (sz < sizeof(*info))
902 return NULL;
903
Harald Welte2e4e6a12006-01-12 13:30:04 -0800904 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
Jan Beulich44813742009-09-21 17:03:05 -0700905 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800906 return NULL;
907
Eric Dumazet711bdde62015-06-15 09:57:30 -0700908 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
909 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
910 if (!info) {
911 info = vmalloc(sz);
912 if (!info)
913 return NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800914 }
Eric Dumazet711bdde62015-06-15 09:57:30 -0700915 memset(info, 0, sizeof(*info));
916 info->size = size;
917 return info;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800918}
919EXPORT_SYMBOL(xt_alloc_table_info);
920
921void xt_free_table_info(struct xt_table_info *info)
922{
923 int cpu;
924
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200925 if (info->jumpstack != NULL) {
Eric Dumazetf6b50822014-06-24 02:15:35 -0700926 for_each_possible_cpu(cpu)
927 kvfree(info->jumpstack[cpu]);
928 kvfree(info->jumpstack);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200929 }
930
Eric Dumazet711bdde62015-06-15 09:57:30 -0700931 kvfree(info);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800932}
933EXPORT_SYMBOL(xt_free_table_info);
934
935/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200936struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
937 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800938{
Florian Westphalb9e69e12016-02-25 10:08:36 +0100939 struct xt_table *t, *found = NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800940
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200941 mutex_lock(&xt[af].mutex);
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800942 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800943 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
944 return t;
Florian Westphalb9e69e12016-02-25 10:08:36 +0100945
946 if (net == &init_net)
947 goto out;
948
949 /* Table doesn't exist in this netns, re-try init */
950 list_for_each_entry(t, &init_net.xt.tables[af], list) {
951 if (strcmp(t->name, name))
952 continue;
953 if (!try_module_get(t->me))
954 return NULL;
955
956 mutex_unlock(&xt[af].mutex);
957 if (t->table_init(net) != 0) {
958 module_put(t->me);
959 return NULL;
960 }
961
962 found = t;
963
964 mutex_lock(&xt[af].mutex);
965 break;
966 }
967
968 if (!found)
969 goto out;
970
971 /* and once again: */
972 list_for_each_entry(t, &net->xt.tables[af], list)
973 if (strcmp(t->name, name) == 0)
974 return t;
975
976 module_put(found->me);
977 out:
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800978 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800979 return NULL;
980}
981EXPORT_SYMBOL_GPL(xt_find_table_lock);
982
983void xt_table_unlock(struct xt_table *table)
984{
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800985 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800986}
987EXPORT_SYMBOL_GPL(xt_table_unlock);
988
Dmitry Mishin27229712006-04-01 02:25:19 -0800989#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200990void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800991{
992 mutex_lock(&xt[af].compat_mutex);
993}
994EXPORT_SYMBOL_GPL(xt_compat_lock);
995
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200996void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800997{
998 mutex_unlock(&xt[af].compat_mutex);
999}
1000EXPORT_SYMBOL_GPL(xt_compat_unlock);
1001#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001002
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001003DEFINE_PER_CPU(seqcount_t, xt_recseq);
1004EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001005
Florian Westphaldcebd312015-07-14 17:51:09 +02001006struct static_key xt_tee_enabled __read_mostly;
1007EXPORT_SYMBOL_GPL(xt_tee_enabled);
1008
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001009static int xt_jumpstack_alloc(struct xt_table_info *i)
1010{
1011 unsigned int size;
1012 int cpu;
1013
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001014 size = sizeof(void **) * nr_cpu_ids;
1015 if (size > PAGE_SIZE)
Joe Perches3dbd4432011-05-28 10:36:35 -07001016 i->jumpstack = vzalloc(size);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001017 else
Joe Perches3dbd4432011-05-28 10:36:35 -07001018 i->jumpstack = kzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001019 if (i->jumpstack == NULL)
1020 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001021
Florian Westphal98d1bd82015-07-14 17:51:06 +02001022 /* ruleset without jumps -- no stack needed */
1023 if (i->stacksize == 0)
1024 return 0;
1025
Florian Westphal7814b6e2015-07-14 17:51:08 +02001026 /* Jumpstack needs to be able to record two full callchains, one
1027 * from the first rule set traversal, plus one table reentrancy
1028 * via -j TEE without clobbering the callchain that brought us to
1029 * TEE target.
1030 *
1031 * This is done by allocating two jumpstacks per cpu, on reentry
1032 * the upper half of the stack is used.
1033 *
1034 * see the jumpstack setup in ipt_do_table() for more details.
1035 */
1036 size = sizeof(void *) * i->stacksize * 2u;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001037 for_each_possible_cpu(cpu) {
1038 if (size > PAGE_SIZE)
1039 i->jumpstack[cpu] = vmalloc_node(size,
1040 cpu_to_node(cpu));
1041 else
1042 i->jumpstack[cpu] = kmalloc_node(size,
1043 GFP_KERNEL, cpu_to_node(cpu));
1044 if (i->jumpstack[cpu] == NULL)
1045 /*
1046 * Freeing will be done later on by the callers. The
1047 * chain is: xt_replace_table -> __do_replace ->
1048 * do_replace -> xt_free_table_info.
1049 */
1050 return -ENOMEM;
1051 }
1052
1053 return 0;
1054}
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001055
Harald Welte2e4e6a12006-01-12 13:30:04 -08001056struct xt_table_info *
1057xt_replace_table(struct xt_table *table,
1058 unsigned int num_counters,
1059 struct xt_table_info *newinfo,
1060 int *error)
1061{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001062 struct xt_table_info *private;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001063 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001064
Jan Engelhardtd97a9e42010-04-21 14:45:51 +02001065 ret = xt_jumpstack_alloc(newinfo);
1066 if (ret < 0) {
1067 *error = ret;
1068 return NULL;
1069 }
1070
Harald Welte2e4e6a12006-01-12 13:30:04 -08001071 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001072 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001073 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001074
Harald Welte2e4e6a12006-01-12 13:30:04 -08001075 /* Check inside lock: is the old number correct? */
1076 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001077 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001078 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001079 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001080 *error = -EAGAIN;
1081 return NULL;
1082 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001083
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001084 newinfo->initial_entries = private->initial_entries;
Will Deaconb416c142013-10-21 13:14:53 +01001085 /*
1086 * Ensure contents of newinfo are visible before assigning to
1087 * private.
1088 */
1089 smp_wmb();
1090 table->private = newinfo;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001091
1092 /*
1093 * Even though table entries have now been swapped, other CPU's
1094 * may still be using the old entries. This is okay, because
1095 * resynchronization happens because of the locking done
1096 * during the get_counters() routine.
1097 */
1098 local_bh_enable();
1099
Thomas Graffbabf312011-01-16 18:12:59 +01001100#ifdef CONFIG_AUDIT
1101 if (audit_enabled) {
1102 struct audit_buffer *ab;
1103
1104 ab = audit_log_start(current->audit_context, GFP_KERNEL,
1105 AUDIT_NETFILTER_CFG);
1106 if (ab) {
1107 audit_log_format(ab, "table=%s family=%u entries=%u",
1108 table->name, table->af,
1109 private->number);
1110 audit_log_end(ab);
1111 }
1112 }
1113#endif
1114
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001115 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001116}
1117EXPORT_SYMBOL_GPL(xt_replace_table);
1118
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001119struct xt_table *xt_register_table(struct net *net,
1120 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -08001121 struct xt_table_info *bootstrap,
1122 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001123{
1124 int ret;
1125 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001126 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001127
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001128 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001129 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001130 if (!table) {
1131 ret = -ENOMEM;
1132 goto out;
1133 }
1134
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001135 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001136 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001137 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -07001138 if (strcmp(t->name, table->name) == 0) {
1139 ret = -EEXIST;
1140 goto unlock;
1141 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001142 }
1143
1144 /* Simplifies replace_table code. */
1145 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +01001146
Harald Welte2e4e6a12006-01-12 13:30:04 -08001147 if (!xt_replace_table(table, 0, newinfo, &ret))
1148 goto unlock;
1149
1150 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001151 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001152
1153 /* save number of initial entries */
1154 private->initial_entries = private->number;
1155
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001156 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001157 mutex_unlock(&xt[table->af].mutex);
1158 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001159
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001160unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001161 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001162 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001163out:
1164 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001165}
1166EXPORT_SYMBOL_GPL(xt_register_table);
1167
1168void *xt_unregister_table(struct xt_table *table)
1169{
1170 struct xt_table_info *private;
1171
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001172 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001173 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -07001174 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001175 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001176 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001177
1178 return private;
1179}
1180EXPORT_SYMBOL_GPL(xt_unregister_table);
1181
1182#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001183struct xt_names_priv {
1184 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001185 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001186};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001187static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001188{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001189 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001190 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001191 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001192
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001193 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001194 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001195}
1196
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001197static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001198{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001199 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001200 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001201 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001202
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001203 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001204}
1205
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001206static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001207{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001208 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001209 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001210
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001211 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001212}
1213
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001214static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001215{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001216 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001217
Joe Perches861fb102015-05-12 18:28:23 -07001218 if (*table->name)
Steven Rostedt (Red Hat)e71456ae2014-10-27 17:43:45 -04001219 seq_printf(seq, "%s\n", table->name);
Joe Perches861fb102015-05-12 18:28:23 -07001220 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001221}
1222
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001223static const struct seq_operations xt_table_seq_ops = {
1224 .start = xt_table_seq_start,
1225 .next = xt_table_seq_next,
1226 .stop = xt_table_seq_stop,
1227 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001228};
1229
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001230static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001231{
1232 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001233 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001234
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001235 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1236 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001237 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001238 priv = ((struct seq_file *)file->private_data)->private;
Al Virod9dda782013-03-31 18:16:14 -04001239 priv->af = (unsigned long)PDE_DATA(inode);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001240 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001241 return ret;
1242}
1243
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001244static const struct file_operations xt_table_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001245 .owner = THIS_MODULE,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001246 .open = xt_table_open,
1247 .read = seq_read,
1248 .llseek = seq_lseek,
Pavel Emelyanov0e93bb942008-04-29 03:15:35 -07001249 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001250};
1251
Jan Engelhardteb132202009-02-18 16:42:19 +01001252/*
1253 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1254 * the multi-AF mutexes.
1255 */
1256struct nf_mttg_trav {
1257 struct list_head *head, *curr;
1258 uint8_t class, nfproto;
1259};
1260
1261enum {
1262 MTTG_TRAV_INIT,
1263 MTTG_TRAV_NFP_UNSPEC,
1264 MTTG_TRAV_NFP_SPEC,
1265 MTTG_TRAV_DONE,
1266};
1267
1268static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1269 bool is_target)
1270{
1271 static const uint8_t next_class[] = {
1272 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1273 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1274 };
1275 struct nf_mttg_trav *trav = seq->private;
1276
1277 switch (trav->class) {
1278 case MTTG_TRAV_INIT:
1279 trav->class = MTTG_TRAV_NFP_UNSPEC;
1280 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1281 trav->head = trav->curr = is_target ?
1282 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1283 break;
1284 case MTTG_TRAV_NFP_UNSPEC:
1285 trav->curr = trav->curr->next;
1286 if (trav->curr != trav->head)
1287 break;
1288 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1289 mutex_lock(&xt[trav->nfproto].mutex);
1290 trav->head = trav->curr = is_target ?
1291 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1292 trav->class = next_class[trav->class];
1293 break;
1294 case MTTG_TRAV_NFP_SPEC:
1295 trav->curr = trav->curr->next;
1296 if (trav->curr != trav->head)
1297 break;
1298 /* fallthru, _stop will unlock */
1299 default:
1300 return NULL;
1301 }
1302
1303 if (ppos != NULL)
1304 ++*ppos;
1305 return trav;
1306}
1307
1308static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1309 bool is_target)
1310{
1311 struct nf_mttg_trav *trav = seq->private;
1312 unsigned int j;
1313
1314 trav->class = MTTG_TRAV_INIT;
1315 for (j = 0; j < *pos; ++j)
1316 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1317 return NULL;
1318 return trav;
1319}
1320
1321static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1322{
1323 struct nf_mttg_trav *trav = seq->private;
1324
1325 switch (trav->class) {
1326 case MTTG_TRAV_NFP_UNSPEC:
1327 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1328 break;
1329 case MTTG_TRAV_NFP_SPEC:
1330 mutex_unlock(&xt[trav->nfproto].mutex);
1331 break;
1332 }
1333}
1334
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001335static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1336{
Jan Engelhardteb132202009-02-18 16:42:19 +01001337 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001338}
1339
Jan Engelhardteb132202009-02-18 16:42:19 +01001340static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001341{
Jan Engelhardteb132202009-02-18 16:42:19 +01001342 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001343}
1344
1345static int xt_match_seq_show(struct seq_file *seq, void *v)
1346{
Jan Engelhardteb132202009-02-18 16:42:19 +01001347 const struct nf_mttg_trav *trav = seq->private;
1348 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001349
Jan Engelhardteb132202009-02-18 16:42:19 +01001350 switch (trav->class) {
1351 case MTTG_TRAV_NFP_UNSPEC:
1352 case MTTG_TRAV_NFP_SPEC:
1353 if (trav->curr == trav->head)
1354 return 0;
1355 match = list_entry(trav->curr, struct xt_match, list);
Joe Perches861fb102015-05-12 18:28:23 -07001356 if (*match->name)
1357 seq_printf(seq, "%s\n", match->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001358 }
1359 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001360}
1361
1362static const struct seq_operations xt_match_seq_ops = {
1363 .start = xt_match_seq_start,
1364 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001365 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001366 .show = xt_match_seq_show,
1367};
1368
1369static int xt_match_open(struct inode *inode, struct file *file)
1370{
Jan Engelhardteb132202009-02-18 16:42:19 +01001371 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001372 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1373 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001374 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001375
Al Virod9dda782013-03-31 18:16:14 -04001376 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001377 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001378}
1379
1380static const struct file_operations xt_match_ops = {
1381 .owner = THIS_MODULE,
1382 .open = xt_match_open,
1383 .read = seq_read,
1384 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001385 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001386};
1387
1388static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1389{
Jan Engelhardteb132202009-02-18 16:42:19 +01001390 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001391}
1392
Jan Engelhardteb132202009-02-18 16:42:19 +01001393static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001394{
Jan Engelhardteb132202009-02-18 16:42:19 +01001395 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001396}
1397
1398static int xt_target_seq_show(struct seq_file *seq, void *v)
1399{
Jan Engelhardteb132202009-02-18 16:42:19 +01001400 const struct nf_mttg_trav *trav = seq->private;
1401 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001402
Jan Engelhardteb132202009-02-18 16:42:19 +01001403 switch (trav->class) {
1404 case MTTG_TRAV_NFP_UNSPEC:
1405 case MTTG_TRAV_NFP_SPEC:
1406 if (trav->curr == trav->head)
1407 return 0;
1408 target = list_entry(trav->curr, struct xt_target, list);
Joe Perches861fb102015-05-12 18:28:23 -07001409 if (*target->name)
1410 seq_printf(seq, "%s\n", target->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001411 }
1412 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001413}
1414
1415static const struct seq_operations xt_target_seq_ops = {
1416 .start = xt_target_seq_start,
1417 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001418 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001419 .show = xt_target_seq_show,
1420};
1421
1422static int xt_target_open(struct inode *inode, struct file *file)
1423{
Jan Engelhardteb132202009-02-18 16:42:19 +01001424 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001425 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1426 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001427 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001428
Al Virod9dda782013-03-31 18:16:14 -04001429 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001430 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001431}
1432
1433static const struct file_operations xt_target_ops = {
1434 .owner = THIS_MODULE,
1435 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001436 .read = seq_read,
1437 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001438 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001439};
1440
1441#define FORMAT_TABLES "_tables_names"
1442#define FORMAT_MATCHES "_tables_matches"
1443#define FORMAT_TARGETS "_tables_targets"
1444
1445#endif /* CONFIG_PROC_FS */
1446
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001447/**
Florian Westphalb9e69e12016-02-25 10:08:36 +01001448 * xt_hook_ops_alloc - set up hooks for a new table
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001449 * @table: table with metadata needed to set up hooks
1450 * @fn: Hook function
1451 *
Florian Westphalb9e69e12016-02-25 10:08:36 +01001452 * This function will create the nf_hook_ops that the x_table needs
1453 * to hand to xt_hook_link_net().
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001454 */
Florian Westphalb9e69e12016-02-25 10:08:36 +01001455struct nf_hook_ops *
1456xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001457{
1458 unsigned int hook_mask = table->valid_hooks;
1459 uint8_t i, num_hooks = hweight32(hook_mask);
1460 uint8_t hooknum;
1461 struct nf_hook_ops *ops;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001462
1463 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1464 if (ops == NULL)
1465 return ERR_PTR(-ENOMEM);
1466
1467 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1468 hook_mask >>= 1, ++hooknum) {
1469 if (!(hook_mask & 1))
1470 continue;
1471 ops[i].hook = fn;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001472 ops[i].pf = table->af;
1473 ops[i].hooknum = hooknum;
1474 ops[i].priority = table->priority;
1475 ++i;
1476 }
1477
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001478 return ops;
1479}
Florian Westphalb9e69e12016-02-25 10:08:36 +01001480EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001481
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001482int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001483{
1484#ifdef CONFIG_PROC_FS
1485 char buf[XT_FUNCTION_MAXNAMELEN];
1486 struct proc_dir_entry *proc;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001487 kuid_t root_uid;
1488 kgid_t root_gid;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001489#endif
1490
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001491 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001492 return -EINVAL;
1493
1494
1495#ifdef CONFIG_PROC_FS
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001496 root_uid = make_kuid(net->user_ns, 0);
1497 root_gid = make_kgid(net->user_ns, 0);
1498
Tobias Klauserce18afe2007-03-14 16:36:16 -07001499 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001500 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001501 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1502 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001503 if (!proc)
1504 goto out;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001505 if (uid_valid(root_uid) && gid_valid(root_gid))
1506 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001507
Tobias Klauserce18afe2007-03-14 16:36:16 -07001508 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001509 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001510 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1511 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001512 if (!proc)
1513 goto out_remove_tables;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001514 if (uid_valid(root_uid) && gid_valid(root_gid))
1515 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001516
Tobias Klauserce18afe2007-03-14 16:36:16 -07001517 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001518 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001519 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1520 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001521 if (!proc)
1522 goto out_remove_matches;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001523 if (uid_valid(root_uid) && gid_valid(root_gid))
1524 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001525#endif
1526
1527 return 0;
1528
1529#ifdef CONFIG_PROC_FS
1530out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001531 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001532 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001533 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001534
1535out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001536 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001537 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001538 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001539out:
1540 return -1;
1541#endif
1542}
1543EXPORT_SYMBOL_GPL(xt_proto_init);
1544
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001545void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001546{
1547#ifdef CONFIG_PROC_FS
1548 char buf[XT_FUNCTION_MAXNAMELEN];
1549
Tobias Klauserce18afe2007-03-14 16:36:16 -07001550 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001551 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001552 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001553
Tobias Klauserce18afe2007-03-14 16:36:16 -07001554 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001555 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001556 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001557
Tobias Klauserce18afe2007-03-14 16:36:16 -07001558 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001559 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001560 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001561#endif /*CONFIG_PROC_FS*/
1562}
1563EXPORT_SYMBOL_GPL(xt_proto_fini);
1564
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001565static int __net_init xt_net_init(struct net *net)
1566{
1567 int i;
1568
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001569 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001570 INIT_LIST_HEAD(&net->xt.tables[i]);
1571 return 0;
1572}
1573
1574static struct pernet_operations xt_net_ops = {
1575 .init = xt_net_init,
1576};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001577
1578static int __init xt_init(void)
1579{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001580 unsigned int i;
1581 int rv;
1582
1583 for_each_possible_cpu(i) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001584 seqcount_init(&per_cpu(xt_recseq, i));
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001585 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001586
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001587 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001588 if (!xt)
1589 return -ENOMEM;
1590
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001591 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001592 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001593#ifdef CONFIG_COMPAT
1594 mutex_init(&xt[i].compat_mutex);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001595 xt[i].compat_tab = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001596#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001597 INIT_LIST_HEAD(&xt[i].target);
1598 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001599 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001600 rv = register_pernet_subsys(&xt_net_ops);
1601 if (rv < 0)
1602 kfree(xt);
1603 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001604}
1605
1606static void __exit xt_fini(void)
1607{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001608 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001609 kfree(xt);
1610}
1611
1612module_init(xt_init);
1613module_exit(xt_fini);
1614