blob: fc4977456c30e098197b4f987b758072c9cf60d9 [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 Westphala08e4e12016-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
Florian Westphalf4dc7772016-07-14 17:51:26 +0200705/**
706 * xt_alloc_entry_offsets - allocate array to store rule head offsets
707 *
708 * @size: number of entries
709 *
710 * Return: NULL or kmalloc'd or vmalloc'd array
711 */
712unsigned int *xt_alloc_entry_offsets(unsigned int size)
713{
714 unsigned int *off;
715
716 off = kcalloc(size, sizeof(unsigned int), GFP_KERNEL | __GFP_NOWARN);
717
718 if (off)
719 return off;
720
721 if (size < (SIZE_MAX / sizeof(unsigned int)))
722 off = vmalloc(size * sizeof(unsigned int));
723
724 return off;
725}
726EXPORT_SYMBOL(xt_alloc_entry_offsets);
727
728/**
729 * xt_find_jump_offset - check if target is a valid jump offset
730 *
731 * @offsets: array containing all valid rule start offsets of a rule blob
732 * @target: the jump target to search for
733 * @size: entries in @offset
734 */
735bool xt_find_jump_offset(const unsigned int *offsets,
736 unsigned int target, unsigned int size)
737{
738 int m, low = 0, hi = size;
739
740 while (hi > low) {
741 m = (low + hi) / 2u;
742
743 if (offsets[m] > target)
744 hi = m;
745 else if (offsets[m] < target)
746 low = m + 1;
747 else
748 return true;
749 }
750
751 return false;
752}
753EXPORT_SYMBOL(xt_find_jump_offset);
754
Jan Engelhardt916a9172008-10-08 11:35:20 +0200755int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200756 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800757{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100758 int ret;
759
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200760 if (XT_ALIGN(par->target->targetsize) != size) {
Jan Engelhardtb4024052009-06-25 18:32:12 +0200761 pr_err("%s_tables: %s.%u target: invalid size "
762 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200763 xt_prefix[par->family], par->target->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200764 par->target->revision,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200765 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800766 return -EINVAL;
767 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200768 if (par->target->table != NULL &&
769 strcmp(par->target->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200770 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200771 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200772 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800773 return -EINVAL;
774 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200775 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200776 char used[64], allow[64];
777
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200778 pr_err("%s_tables: %s target: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200779 "usable from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200780 xt_prefix[par->family], par->target->name,
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000781 textify_hooks(used, sizeof(used), par->hook_mask,
782 par->family),
783 textify_hooks(allow, sizeof(allow), par->target->hooks,
784 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800785 return -EINVAL;
786 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200787 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200788 pr_err("%s_tables: %s target: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200789 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200790 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800791 return -EINVAL;
792 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100793 if (par->target->checkentry != NULL) {
794 ret = par->target->checkentry(par);
795 if (ret < 0)
796 return ret;
797 else if (ret > 0)
798 /* Flag up potential errors. */
799 return -EIO;
800 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800801 return 0;
802}
803EXPORT_SYMBOL_GPL(xt_check_target);
804
Florian Westphald7591f02016-04-01 15:37:59 +0200805/**
806 * xt_copy_counters_from_user - copy counters and metadata from userspace
807 *
808 * @user: src pointer to userspace memory
809 * @len: alleged size of userspace memory
810 * @info: where to store the xt_counters_info metadata
811 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
812 *
813 * Copies counter meta data from @user and stores it in @info.
814 *
815 * vmallocs memory to hold the counters, then copies the counter data
816 * from @user to the new memory and returns a pointer to it.
817 *
818 * If @compat is true, @info gets converted automatically to the 64bit
819 * representation.
820 *
821 * The metadata associated with the counters is stored in @info.
822 *
823 * Return: returns pointer that caller has to test via IS_ERR().
824 * If IS_ERR is false, caller has to vfree the pointer.
825 */
826void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
827 struct xt_counters_info *info, bool compat)
828{
829 void *mem;
830 u64 size;
831
832#ifdef CONFIG_COMPAT
833 if (compat) {
834 /* structures only differ in size due to alignment */
835 struct compat_xt_counters_info compat_tmp;
836
837 if (len <= sizeof(compat_tmp))
838 return ERR_PTR(-EINVAL);
839
840 len -= sizeof(compat_tmp);
841 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
842 return ERR_PTR(-EFAULT);
843
844 strlcpy(info->name, compat_tmp.name, sizeof(info->name));
845 info->num_counters = compat_tmp.num_counters;
846 user += sizeof(compat_tmp);
847 } else
848#endif
849 {
850 if (len <= sizeof(*info))
851 return ERR_PTR(-EINVAL);
852
853 len -= sizeof(*info);
854 if (copy_from_user(info, user, sizeof(*info)) != 0)
855 return ERR_PTR(-EFAULT);
856
857 info->name[sizeof(info->name) - 1] = '\0';
858 user += sizeof(*info);
859 }
860
861 size = sizeof(struct xt_counters);
862 size *= info->num_counters;
863
864 if (size != (u64)len)
865 return ERR_PTR(-EINVAL);
866
867 mem = vmalloc(len);
868 if (!mem)
869 return ERR_PTR(-ENOMEM);
870
871 if (copy_from_user(mem, user, len) == 0)
872 return mem;
873
874 vfree(mem);
875 return ERR_PTR(-EFAULT);
876}
877EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
878
Dmitry Mishin27229712006-04-01 02:25:19 -0800879#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +0200880int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800881{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700882 u_int16_t csize = target->compatsize ? : target->targetsize;
883 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800884}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700885EXPORT_SYMBOL_GPL(xt_compat_target_offset);
886
887void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800888 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700889{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200890 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700891 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
892 int pad, off = xt_compat_target_offset(target);
893 u_int16_t tsize = ct->u.user.target_size;
Florian Westphal09d96862016-04-01 14:17:34 +0200894 char name[sizeof(t->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700895
896 t = *dstptr;
897 memcpy(t, ct, sizeof(*ct));
898 if (target->compat_from_user)
899 target->compat_from_user(t->data, ct->data);
900 else
901 memcpy(t->data, ct->data, tsize - sizeof(*ct));
902 pad = XT_ALIGN(target->targetsize) - target->targetsize;
903 if (pad > 0)
904 memset(t->data + target->targetsize, 0, pad);
905
906 tsize += off;
907 t->u.user.target_size = tsize;
Florian Westphal09d96862016-04-01 14:17:34 +0200908 strlcpy(name, target->name, sizeof(name));
909 module_put(target->me);
910 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700911
912 *size += off;
913 *dstptr += tsize;
914}
915EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
916
Jan Engelhardt739674f2009-06-26 08:23:19 +0200917int xt_compat_target_to_user(const struct xt_entry_target *t,
918 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700919{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200920 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700921 struct compat_xt_entry_target __user *ct = *dstptr;
922 int off = xt_compat_target_offset(target);
923 u_int16_t tsize = t->u.user.target_size - off;
924
925 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800926 put_user(tsize, &ct->u.user.target_size) ||
927 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
928 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800929 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700930
931 if (target->compat_to_user) {
932 if (target->compat_to_user((void __user *)ct->data, t->data))
933 return -EFAULT;
934 } else {
935 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
936 return -EFAULT;
937 }
938
939 *size -= off;
940 *dstptr += tsize;
941 return 0;
942}
943EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -0800944#endif
945
Harald Welte2e4e6a12006-01-12 13:30:04 -0800946struct xt_table_info *xt_alloc_table_info(unsigned int size)
947{
Eric Dumazet711bdde2015-06-15 09:57:30 -0700948 struct xt_table_info *info = NULL;
949 size_t sz = sizeof(*info) + size;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800950
Florian Westphald157bd72016-03-10 01:56:23 +0100951 if (sz < sizeof(*info))
952 return NULL;
953
Harald Welte2e4e6a12006-01-12 13:30:04 -0800954 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
Jan Beulich44813742009-09-21 17:03:05 -0700955 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800956 return NULL;
957
Eric Dumazet711bdde2015-06-15 09:57:30 -0700958 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
959 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
960 if (!info) {
961 info = vmalloc(sz);
962 if (!info)
963 return NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800964 }
Eric Dumazet711bdde2015-06-15 09:57:30 -0700965 memset(info, 0, sizeof(*info));
966 info->size = size;
967 return info;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800968}
969EXPORT_SYMBOL(xt_alloc_table_info);
970
971void xt_free_table_info(struct xt_table_info *info)
972{
973 int cpu;
974
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200975 if (info->jumpstack != NULL) {
Eric Dumazetf6b50822014-06-24 02:15:35 -0700976 for_each_possible_cpu(cpu)
977 kvfree(info->jumpstack[cpu]);
978 kvfree(info->jumpstack);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200979 }
980
Eric Dumazet711bdde2015-06-15 09:57:30 -0700981 kvfree(info);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800982}
983EXPORT_SYMBOL(xt_free_table_info);
984
985/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200986struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
987 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800988{
Florian Westphalb9e69e12016-02-25 10:08:36 +0100989 struct xt_table *t, *found = NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800990
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200991 mutex_lock(&xt[af].mutex);
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800992 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800993 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
994 return t;
Florian Westphalb9e69e12016-02-25 10:08:36 +0100995
996 if (net == &init_net)
997 goto out;
998
999 /* Table doesn't exist in this netns, re-try init */
1000 list_for_each_entry(t, &init_net.xt.tables[af], list) {
1001 if (strcmp(t->name, name))
1002 continue;
1003 if (!try_module_get(t->me))
1004 return NULL;
1005
1006 mutex_unlock(&xt[af].mutex);
1007 if (t->table_init(net) != 0) {
1008 module_put(t->me);
1009 return NULL;
1010 }
1011
1012 found = t;
1013
1014 mutex_lock(&xt[af].mutex);
1015 break;
1016 }
1017
1018 if (!found)
1019 goto out;
1020
1021 /* and once again: */
1022 list_for_each_entry(t, &net->xt.tables[af], list)
1023 if (strcmp(t->name, name) == 0)
1024 return t;
1025
1026 module_put(found->me);
1027 out:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001028 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001029 return NULL;
1030}
1031EXPORT_SYMBOL_GPL(xt_find_table_lock);
1032
1033void xt_table_unlock(struct xt_table *table)
1034{
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001035 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001036}
1037EXPORT_SYMBOL_GPL(xt_table_unlock);
1038
Dmitry Mishin27229712006-04-01 02:25:19 -08001039#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001040void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001041{
1042 mutex_lock(&xt[af].compat_mutex);
1043}
1044EXPORT_SYMBOL_GPL(xt_compat_lock);
1045
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001046void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001047{
1048 mutex_unlock(&xt[af].compat_mutex);
1049}
1050EXPORT_SYMBOL_GPL(xt_compat_unlock);
1051#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001052
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001053DEFINE_PER_CPU(seqcount_t, xt_recseq);
1054EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001055
Florian Westphaldcebd312015-07-14 17:51:09 +02001056struct static_key xt_tee_enabled __read_mostly;
1057EXPORT_SYMBOL_GPL(xt_tee_enabled);
1058
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001059static int xt_jumpstack_alloc(struct xt_table_info *i)
1060{
1061 unsigned int size;
1062 int cpu;
1063
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001064 size = sizeof(void **) * nr_cpu_ids;
1065 if (size > PAGE_SIZE)
Joe Perches3dbd4432011-05-28 10:36:35 -07001066 i->jumpstack = vzalloc(size);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001067 else
Joe Perches3dbd4432011-05-28 10:36:35 -07001068 i->jumpstack = kzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001069 if (i->jumpstack == NULL)
1070 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001071
Florian Westphal98d1bd82015-07-14 17:51:06 +02001072 /* ruleset without jumps -- no stack needed */
1073 if (i->stacksize == 0)
1074 return 0;
1075
Florian Westphal7814b6e2015-07-14 17:51:08 +02001076 /* Jumpstack needs to be able to record two full callchains, one
1077 * from the first rule set traversal, plus one table reentrancy
1078 * via -j TEE without clobbering the callchain that brought us to
1079 * TEE target.
1080 *
1081 * This is done by allocating two jumpstacks per cpu, on reentry
1082 * the upper half of the stack is used.
1083 *
1084 * see the jumpstack setup in ipt_do_table() for more details.
1085 */
1086 size = sizeof(void *) * i->stacksize * 2u;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001087 for_each_possible_cpu(cpu) {
1088 if (size > PAGE_SIZE)
1089 i->jumpstack[cpu] = vmalloc_node(size,
1090 cpu_to_node(cpu));
1091 else
1092 i->jumpstack[cpu] = kmalloc_node(size,
1093 GFP_KERNEL, cpu_to_node(cpu));
1094 if (i->jumpstack[cpu] == NULL)
1095 /*
1096 * Freeing will be done later on by the callers. The
1097 * chain is: xt_replace_table -> __do_replace ->
1098 * do_replace -> xt_free_table_info.
1099 */
1100 return -ENOMEM;
1101 }
1102
1103 return 0;
1104}
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001105
Harald Welte2e4e6a12006-01-12 13:30:04 -08001106struct xt_table_info *
1107xt_replace_table(struct xt_table *table,
1108 unsigned int num_counters,
1109 struct xt_table_info *newinfo,
1110 int *error)
1111{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001112 struct xt_table_info *private;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001113 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001114
Jan Engelhardtd97a9e42010-04-21 14:45:51 +02001115 ret = xt_jumpstack_alloc(newinfo);
1116 if (ret < 0) {
1117 *error = ret;
1118 return NULL;
1119 }
1120
Harald Welte2e4e6a12006-01-12 13:30:04 -08001121 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001122 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001123 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001124
Harald Welte2e4e6a12006-01-12 13:30:04 -08001125 /* Check inside lock: is the old number correct? */
1126 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001127 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001128 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001129 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001130 *error = -EAGAIN;
1131 return NULL;
1132 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001133
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001134 newinfo->initial_entries = private->initial_entries;
Will Deaconb416c142013-10-21 13:14:53 +01001135 /*
1136 * Ensure contents of newinfo are visible before assigning to
1137 * private.
1138 */
1139 smp_wmb();
1140 table->private = newinfo;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001141
1142 /*
1143 * Even though table entries have now been swapped, other CPU's
1144 * may still be using the old entries. This is okay, because
1145 * resynchronization happens because of the locking done
1146 * during the get_counters() routine.
1147 */
1148 local_bh_enable();
1149
Thomas Graffbabf312011-01-16 18:12:59 +01001150#ifdef CONFIG_AUDIT
1151 if (audit_enabled) {
1152 struct audit_buffer *ab;
1153
1154 ab = audit_log_start(current->audit_context, GFP_KERNEL,
1155 AUDIT_NETFILTER_CFG);
1156 if (ab) {
1157 audit_log_format(ab, "table=%s family=%u entries=%u",
1158 table->name, table->af,
1159 private->number);
1160 audit_log_end(ab);
1161 }
1162 }
1163#endif
1164
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001165 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001166}
1167EXPORT_SYMBOL_GPL(xt_replace_table);
1168
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001169struct xt_table *xt_register_table(struct net *net,
1170 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -08001171 struct xt_table_info *bootstrap,
1172 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001173{
1174 int ret;
1175 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001176 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001177
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001178 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001179 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001180 if (!table) {
1181 ret = -ENOMEM;
1182 goto out;
1183 }
1184
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001185 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001186 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001187 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -07001188 if (strcmp(t->name, table->name) == 0) {
1189 ret = -EEXIST;
1190 goto unlock;
1191 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001192 }
1193
1194 /* Simplifies replace_table code. */
1195 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +01001196
Harald Welte2e4e6a12006-01-12 13:30:04 -08001197 if (!xt_replace_table(table, 0, newinfo, &ret))
1198 goto unlock;
1199
1200 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001201 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001202
1203 /* save number of initial entries */
1204 private->initial_entries = private->number;
1205
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001206 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001207 mutex_unlock(&xt[table->af].mutex);
1208 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001209
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001210unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001211 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001212 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001213out:
1214 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001215}
1216EXPORT_SYMBOL_GPL(xt_register_table);
1217
1218void *xt_unregister_table(struct xt_table *table)
1219{
1220 struct xt_table_info *private;
1221
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001222 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001223 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -07001224 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001225 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001226 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001227
1228 return private;
1229}
1230EXPORT_SYMBOL_GPL(xt_unregister_table);
1231
1232#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001233struct xt_names_priv {
1234 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001235 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001236};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001237static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001238{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001239 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001240 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001241 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001242
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001243 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001244 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001245}
1246
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001247static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001248{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001249 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001250 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001251 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001252
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001253 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001254}
1255
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001256static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001257{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001258 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001259 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001260
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001261 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001262}
1263
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001264static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001265{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001266 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001267
Joe Perches861fb102015-05-12 18:28:23 -07001268 if (*table->name)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -04001269 seq_printf(seq, "%s\n", table->name);
Joe Perches861fb102015-05-12 18:28:23 -07001270 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001271}
1272
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001273static const struct seq_operations xt_table_seq_ops = {
1274 .start = xt_table_seq_start,
1275 .next = xt_table_seq_next,
1276 .stop = xt_table_seq_stop,
1277 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001278};
1279
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001280static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001281{
1282 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001283 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001284
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001285 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1286 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001287 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001288 priv = ((struct seq_file *)file->private_data)->private;
Al Virod9dda782013-03-31 18:16:14 -04001289 priv->af = (unsigned long)PDE_DATA(inode);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001290 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001291 return ret;
1292}
1293
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001294static const struct file_operations xt_table_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001295 .owner = THIS_MODULE,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001296 .open = xt_table_open,
1297 .read = seq_read,
1298 .llseek = seq_lseek,
Pavel Emelyanov0e93bb92008-04-29 03:15:35 -07001299 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001300};
1301
Jan Engelhardteb132202009-02-18 16:42:19 +01001302/*
1303 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1304 * the multi-AF mutexes.
1305 */
1306struct nf_mttg_trav {
1307 struct list_head *head, *curr;
1308 uint8_t class, nfproto;
1309};
1310
1311enum {
1312 MTTG_TRAV_INIT,
1313 MTTG_TRAV_NFP_UNSPEC,
1314 MTTG_TRAV_NFP_SPEC,
1315 MTTG_TRAV_DONE,
1316};
1317
1318static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1319 bool is_target)
1320{
1321 static const uint8_t next_class[] = {
1322 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1323 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1324 };
1325 struct nf_mttg_trav *trav = seq->private;
1326
1327 switch (trav->class) {
1328 case MTTG_TRAV_INIT:
1329 trav->class = MTTG_TRAV_NFP_UNSPEC;
1330 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1331 trav->head = trav->curr = is_target ?
1332 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1333 break;
1334 case MTTG_TRAV_NFP_UNSPEC:
1335 trav->curr = trav->curr->next;
1336 if (trav->curr != trav->head)
1337 break;
1338 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1339 mutex_lock(&xt[trav->nfproto].mutex);
1340 trav->head = trav->curr = is_target ?
1341 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1342 trav->class = next_class[trav->class];
1343 break;
1344 case MTTG_TRAV_NFP_SPEC:
1345 trav->curr = trav->curr->next;
1346 if (trav->curr != trav->head)
1347 break;
1348 /* fallthru, _stop will unlock */
1349 default:
1350 return NULL;
1351 }
1352
1353 if (ppos != NULL)
1354 ++*ppos;
1355 return trav;
1356}
1357
1358static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1359 bool is_target)
1360{
1361 struct nf_mttg_trav *trav = seq->private;
1362 unsigned int j;
1363
1364 trav->class = MTTG_TRAV_INIT;
1365 for (j = 0; j < *pos; ++j)
1366 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1367 return NULL;
1368 return trav;
1369}
1370
1371static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1372{
1373 struct nf_mttg_trav *trav = seq->private;
1374
1375 switch (trav->class) {
1376 case MTTG_TRAV_NFP_UNSPEC:
1377 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1378 break;
1379 case MTTG_TRAV_NFP_SPEC:
1380 mutex_unlock(&xt[trav->nfproto].mutex);
1381 break;
1382 }
1383}
1384
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001385static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1386{
Jan Engelhardteb132202009-02-18 16:42:19 +01001387 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001388}
1389
Jan Engelhardteb132202009-02-18 16:42:19 +01001390static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001391{
Jan Engelhardteb132202009-02-18 16:42:19 +01001392 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001393}
1394
1395static int xt_match_seq_show(struct seq_file *seq, void *v)
1396{
Jan Engelhardteb132202009-02-18 16:42:19 +01001397 const struct nf_mttg_trav *trav = seq->private;
1398 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001399
Jan Engelhardteb132202009-02-18 16:42:19 +01001400 switch (trav->class) {
1401 case MTTG_TRAV_NFP_UNSPEC:
1402 case MTTG_TRAV_NFP_SPEC:
1403 if (trav->curr == trav->head)
1404 return 0;
1405 match = list_entry(trav->curr, struct xt_match, list);
Joe Perches861fb102015-05-12 18:28:23 -07001406 if (*match->name)
1407 seq_printf(seq, "%s\n", match->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001408 }
1409 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001410}
1411
1412static const struct seq_operations xt_match_seq_ops = {
1413 .start = xt_match_seq_start,
1414 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001415 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001416 .show = xt_match_seq_show,
1417};
1418
1419static int xt_match_open(struct inode *inode, struct file *file)
1420{
Jan Engelhardteb132202009-02-18 16:42:19 +01001421 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001422 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1423 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001424 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001425
Al Virod9dda782013-03-31 18:16:14 -04001426 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001427 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001428}
1429
1430static const struct file_operations xt_match_ops = {
1431 .owner = THIS_MODULE,
1432 .open = xt_match_open,
1433 .read = seq_read,
1434 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001435 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001436};
1437
1438static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1439{
Jan Engelhardteb132202009-02-18 16:42:19 +01001440 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001441}
1442
Jan Engelhardteb132202009-02-18 16:42:19 +01001443static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001444{
Jan Engelhardteb132202009-02-18 16:42:19 +01001445 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001446}
1447
1448static int xt_target_seq_show(struct seq_file *seq, void *v)
1449{
Jan Engelhardteb132202009-02-18 16:42:19 +01001450 const struct nf_mttg_trav *trav = seq->private;
1451 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001452
Jan Engelhardteb132202009-02-18 16:42:19 +01001453 switch (trav->class) {
1454 case MTTG_TRAV_NFP_UNSPEC:
1455 case MTTG_TRAV_NFP_SPEC:
1456 if (trav->curr == trav->head)
1457 return 0;
1458 target = list_entry(trav->curr, struct xt_target, list);
Joe Perches861fb102015-05-12 18:28:23 -07001459 if (*target->name)
1460 seq_printf(seq, "%s\n", target->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001461 }
1462 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001463}
1464
1465static const struct seq_operations xt_target_seq_ops = {
1466 .start = xt_target_seq_start,
1467 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001468 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001469 .show = xt_target_seq_show,
1470};
1471
1472static int xt_target_open(struct inode *inode, struct file *file)
1473{
Jan Engelhardteb132202009-02-18 16:42:19 +01001474 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001475 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1476 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001477 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001478
Al Virod9dda782013-03-31 18:16:14 -04001479 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001480 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001481}
1482
1483static const struct file_operations xt_target_ops = {
1484 .owner = THIS_MODULE,
1485 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001486 .read = seq_read,
1487 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001488 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001489};
1490
1491#define FORMAT_TABLES "_tables_names"
1492#define FORMAT_MATCHES "_tables_matches"
1493#define FORMAT_TARGETS "_tables_targets"
1494
1495#endif /* CONFIG_PROC_FS */
1496
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001497/**
Florian Westphalb9e69e12016-02-25 10:08:36 +01001498 * xt_hook_ops_alloc - set up hooks for a new table
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001499 * @table: table with metadata needed to set up hooks
1500 * @fn: Hook function
1501 *
Florian Westphalb9e69e12016-02-25 10:08:36 +01001502 * This function will create the nf_hook_ops that the x_table needs
1503 * to hand to xt_hook_link_net().
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001504 */
Florian Westphalb9e69e12016-02-25 10:08:36 +01001505struct nf_hook_ops *
1506xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001507{
1508 unsigned int hook_mask = table->valid_hooks;
1509 uint8_t i, num_hooks = hweight32(hook_mask);
1510 uint8_t hooknum;
1511 struct nf_hook_ops *ops;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001512
Xiubo Lia6d0bae2016-06-02 10:59:56 +08001513 if (!num_hooks)
1514 return ERR_PTR(-EINVAL);
1515
Florian Westphal1ecc2812016-10-17 21:50:23 +02001516 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001517 if (ops == NULL)
1518 return ERR_PTR(-ENOMEM);
1519
1520 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1521 hook_mask >>= 1, ++hooknum) {
1522 if (!(hook_mask & 1))
1523 continue;
1524 ops[i].hook = fn;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001525 ops[i].pf = table->af;
1526 ops[i].hooknum = hooknum;
1527 ops[i].priority = table->priority;
1528 ++i;
1529 }
1530
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001531 return ops;
1532}
Florian Westphalb9e69e12016-02-25 10:08:36 +01001533EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001534
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001535int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001536{
1537#ifdef CONFIG_PROC_FS
1538 char buf[XT_FUNCTION_MAXNAMELEN];
1539 struct proc_dir_entry *proc;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001540 kuid_t root_uid;
1541 kgid_t root_gid;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001542#endif
1543
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001544 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001545 return -EINVAL;
1546
1547
1548#ifdef CONFIG_PROC_FS
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001549 root_uid = make_kuid(net->user_ns, 0);
1550 root_gid = make_kgid(net->user_ns, 0);
1551
Tobias Klauserce18afe2007-03-14 16:36:16 -07001552 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001553 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001554 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1555 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001556 if (!proc)
1557 goto out;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001558 if (uid_valid(root_uid) && gid_valid(root_gid))
1559 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001560
Tobias Klauserce18afe2007-03-14 16:36:16 -07001561 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001562 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001563 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1564 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001565 if (!proc)
1566 goto out_remove_tables;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001567 if (uid_valid(root_uid) && gid_valid(root_gid))
1568 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001569
Tobias Klauserce18afe2007-03-14 16:36:16 -07001570 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001571 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001572 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1573 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001574 if (!proc)
1575 goto out_remove_matches;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001576 if (uid_valid(root_uid) && gid_valid(root_gid))
1577 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001578#endif
1579
1580 return 0;
1581
1582#ifdef CONFIG_PROC_FS
1583out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001584 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001585 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001586 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001587
1588out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001589 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001590 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001591 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001592out:
1593 return -1;
1594#endif
1595}
1596EXPORT_SYMBOL_GPL(xt_proto_init);
1597
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001598void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001599{
1600#ifdef CONFIG_PROC_FS
1601 char buf[XT_FUNCTION_MAXNAMELEN];
1602
Tobias Klauserce18afe2007-03-14 16:36:16 -07001603 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001604 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001605 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001606
Tobias Klauserce18afe2007-03-14 16:36:16 -07001607 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001608 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001609 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001610
Tobias Klauserce18afe2007-03-14 16:36:16 -07001611 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001612 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001613 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001614#endif /*CONFIG_PROC_FS*/
1615}
1616EXPORT_SYMBOL_GPL(xt_proto_fini);
1617
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001618static int __net_init xt_net_init(struct net *net)
1619{
1620 int i;
1621
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001622 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001623 INIT_LIST_HEAD(&net->xt.tables[i]);
1624 return 0;
1625}
1626
1627static struct pernet_operations xt_net_ops = {
1628 .init = xt_net_init,
1629};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001630
1631static int __init xt_init(void)
1632{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001633 unsigned int i;
1634 int rv;
1635
1636 for_each_possible_cpu(i) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001637 seqcount_init(&per_cpu(xt_recseq, i));
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001638 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001639
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001640 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001641 if (!xt)
1642 return -ENOMEM;
1643
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001644 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001645 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001646#ifdef CONFIG_COMPAT
1647 mutex_init(&xt[i].compat_mutex);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001648 xt[i].compat_tab = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001649#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001650 INIT_LIST_HEAD(&xt[i].target);
1651 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001652 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001653 rv = register_pernet_subsys(&xt_net_ops);
1654 if (rv < 0)
1655 kfree(xt);
1656 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001657}
1658
1659static void __exit xt_fini(void)
1660{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001661 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001662 kfree(xt);
1663}
1664
1665module_init(xt_init);
1666module_exit(xt_fini);
1667