blob: 7ad1a863587ad4a25e0d0454477209d19c58dcff [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001/*
2 * x_tables core - Backend for {ip,ip6,arp}_tables
3 *
4 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02005 * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
Harald Welte2e4e6a12006-01-12 13:30:04 -08006 *
7 * Based on existing ip_tables code which is
8 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
9 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Harald Welte2e4e6a12006-01-12 13:30:04 -080017#include <linux/kernel.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040018#include <linux/module.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080019#include <linux/socket.h>
20#include <linux/net.h>
21#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
23#include <linux/string.h>
24#include <linux/vmalloc.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080025#include <linux/mutex.h>
Al Virod7fe0f22006-12-03 23:15:30 -050026#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Thomas Graffbabf312011-01-16 18:12:59 +010028#include <linux/audit.h>
Philip Whinerayf13f2ae2015-11-22 11:35:07 +000029#include <linux/user_namespace.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080031
32#include <linux/netfilter/x_tables.h>
33#include <linux/netfilter_arp.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020034#include <linux/netfilter_ipv4/ip_tables.h>
35#include <linux/netfilter_ipv6/ip6_tables.h>
36#include <linux/netfilter_arp/arp_tables.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080037
Harald Welte2e4e6a12006-01-12 13:30:04 -080038MODULE_LICENSE("GPL");
39MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt043ef462008-10-08 11:35:15 +020040MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080041
Florian Westphaldb6a0cb2016-11-22 14:44:19 +010042#define XT_PCPU_BLOCK_SIZE 4096
43
Patrick McHardyb386d9f2007-12-17 21:47:48 -080044struct compat_delta {
Eric Dumazet255d0dc2010-12-18 18:35:15 +010045 unsigned int offset; /* offset in kernel */
46 int delta; /* delta in 32bit user land */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080047};
48
Harald Welte2e4e6a12006-01-12 13:30:04 -080049struct xt_af {
Ingo Molnar9e19bb62006-03-25 01:41:10 -080050 struct mutex mutex;
Harald Welte2e4e6a12006-01-12 13:30:04 -080051 struct list_head match;
52 struct list_head target;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080053#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080054 struct mutex compat_mutex;
Eric Dumazet255d0dc2010-12-18 18:35:15 +010055 struct compat_delta *compat_tab;
56 unsigned int number; /* number of slots in compat_tab[] */
57 unsigned int cur; /* number of used slots in compat_tab[] */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080058#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080059};
60
61static struct xt_af *xt;
62
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020063static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
64 [NFPROTO_UNSPEC] = "x",
65 [NFPROTO_IPV4] = "ip",
66 [NFPROTO_ARP] = "arp",
67 [NFPROTO_BRIDGE] = "eb",
68 [NFPROTO_IPV6] = "ip6",
Patrick McHardy37f9f732006-03-20 17:59:06 -080069};
70
Harald Welte2e4e6a12006-01-12 13:30:04 -080071/* Registration hooks for targets. */
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020072int xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080073{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020074 u_int8_t af = target->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -080075
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020076 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080077 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080078 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020079 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -080080}
81EXPORT_SYMBOL(xt_register_target);
82
83void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080084xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080085{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020086 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080087
Ingo Molnar9e19bb62006-03-25 01:41:10 -080088 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070089 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080090 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080091}
92EXPORT_SYMBOL(xt_unregister_target);
93
94int
Patrick McHardy52d9c422006-08-22 00:33:45 -070095xt_register_targets(struct xt_target *target, unsigned int n)
96{
97 unsigned int i;
98 int err = 0;
99
100 for (i = 0; i < n; i++) {
101 err = xt_register_target(&target[i]);
102 if (err)
103 goto err;
104 }
105 return err;
106
107err:
108 if (i > 0)
109 xt_unregister_targets(target, i);
110 return err;
111}
112EXPORT_SYMBOL(xt_register_targets);
113
114void
115xt_unregister_targets(struct xt_target *target, unsigned int n)
116{
Changli Gaof68c5302010-10-04 22:24:12 +0200117 while (n-- > 0)
118 xt_unregister_target(&target[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700119}
120EXPORT_SYMBOL(xt_unregister_targets);
121
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200122int xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800123{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200124 u_int8_t af = match->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800125
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200126 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800127 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800128 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200129 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800130}
131EXPORT_SYMBOL(xt_register_match);
132
133void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800134xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800135{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200136 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800137
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800138 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700139 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800140 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800141}
142EXPORT_SYMBOL(xt_unregister_match);
143
Patrick McHardy52d9c422006-08-22 00:33:45 -0700144int
145xt_register_matches(struct xt_match *match, unsigned int n)
146{
147 unsigned int i;
148 int err = 0;
149
150 for (i = 0; i < n; i++) {
151 err = xt_register_match(&match[i]);
152 if (err)
153 goto err;
154 }
155 return err;
156
157err:
158 if (i > 0)
159 xt_unregister_matches(match, i);
160 return err;
161}
162EXPORT_SYMBOL(xt_register_matches);
163
164void
165xt_unregister_matches(struct xt_match *match, unsigned int n)
166{
Changli Gaof68c5302010-10-04 22:24:12 +0200167 while (n-- > 0)
168 xt_unregister_match(&match[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700169}
170EXPORT_SYMBOL(xt_unregister_matches);
171
Harald Welte2e4e6a12006-01-12 13:30:04 -0800172
173/*
174 * These are weird, but module loading must not be done with mutex
175 * held (since they will register), and we have to have a single
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100176 * function to use.
Harald Welte2e4e6a12006-01-12 13:30:04 -0800177 */
178
179/* Find match, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200180struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800181{
182 struct xt_match *m;
Patrick McHardy42046e22011-03-14 19:11:44 +0100183 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800184
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200185 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800186 list_for_each_entry(m, &xt[af].match, list) {
187 if (strcmp(m->name, name) == 0) {
188 if (m->revision == revision) {
189 if (try_module_get(m->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800190 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800191 return m;
192 }
193 } else
194 err = -EPROTOTYPE; /* Found something. */
195 }
196 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800197 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200198
199 if (af != NFPROTO_UNSPEC)
200 /* Try searching again in the family-independent list */
201 return xt_find_match(NFPROTO_UNSPEC, name, revision);
202
Harald Welte2e4e6a12006-01-12 13:30:04 -0800203 return ERR_PTR(err);
204}
205EXPORT_SYMBOL(xt_find_match);
206
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200207struct xt_match *
208xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
209{
210 struct xt_match *match;
211
Eric Dumazetb39f3f32018-01-24 17:16:09 -0800212 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
213 return ERR_PTR(-EINVAL);
214
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100215 match = xt_find_match(nfproto, name, revision);
216 if (IS_ERR(match)) {
217 request_module("%st_%s", xt_prefix[nfproto], name);
218 match = xt_find_match(nfproto, name, revision);
219 }
220
221 return match;
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200222}
223EXPORT_SYMBOL_GPL(xt_request_find_match);
224
Harald Welte2e4e6a12006-01-12 13:30:04 -0800225/* Find target, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200226struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800227{
228 struct xt_target *t;
Patrick McHardy42046e22011-03-14 19:11:44 +0100229 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800230
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200231 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800232 list_for_each_entry(t, &xt[af].target, list) {
233 if (strcmp(t->name, name) == 0) {
234 if (t->revision == revision) {
235 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800236 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800237 return t;
238 }
239 } else
240 err = -EPROTOTYPE; /* Found something. */
241 }
242 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800243 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200244
245 if (af != NFPROTO_UNSPEC)
246 /* Try searching again in the family-independent list */
247 return xt_find_target(NFPROTO_UNSPEC, name, revision);
248
Harald Welte2e4e6a12006-01-12 13:30:04 -0800249 return ERR_PTR(err);
250}
251EXPORT_SYMBOL(xt_find_target);
252
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200253struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800254{
255 struct xt_target *target;
256
Eric Dumazetb39f3f32018-01-24 17:16:09 -0800257 if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
258 return ERR_PTR(-EINVAL);
259
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100260 target = xt_find_target(af, name, revision);
261 if (IS_ERR(target)) {
262 request_module("%st_%s", xt_prefix[af], name);
263 target = xt_find_target(af, name, revision);
264 }
265
266 return target;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800267}
268EXPORT_SYMBOL_GPL(xt_request_find_target);
269
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200270static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800271{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200272 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800273 int have_rev = 0;
274
275 list_for_each_entry(m, &xt[af].match, list) {
276 if (strcmp(m->name, name) == 0) {
277 if (m->revision > *bestp)
278 *bestp = m->revision;
279 if (m->revision == revision)
280 have_rev = 1;
281 }
282 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000283
284 if (af != NFPROTO_UNSPEC && !have_rev)
285 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
286
Harald Welte2e4e6a12006-01-12 13:30:04 -0800287 return have_rev;
288}
289
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200290static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800291{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200292 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800293 int have_rev = 0;
294
295 list_for_each_entry(t, &xt[af].target, list) {
296 if (strcmp(t->name, name) == 0) {
297 if (t->revision > *bestp)
298 *bestp = t->revision;
299 if (t->revision == revision)
300 have_rev = 1;
301 }
302 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000303
304 if (af != NFPROTO_UNSPEC && !have_rev)
305 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
306
Harald Welte2e4e6a12006-01-12 13:30:04 -0800307 return have_rev;
308}
309
310/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200311int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800312 int *err)
313{
314 int have_rev, best = -1;
315
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200316 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800317 if (target == 1)
318 have_rev = target_revfn(af, name, revision, &best);
319 else
320 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800321 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800322
323 /* Nothing at all? Return 0 to try loading module. */
324 if (best == -1) {
325 *err = -ENOENT;
326 return 0;
327 }
328
329 *err = best;
330 if (!have_rev)
331 *err = -EPROTONOSUPPORT;
332 return 1;
333}
334EXPORT_SYMBOL_GPL(xt_find_revision);
335
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000336static char *
337textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
Jan Engelhardt45185362009-04-05 10:43:09 +0200338{
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000339 static const char *const inetbr_names[] = {
Jan Engelhardt45185362009-04-05 10:43:09 +0200340 "PREROUTING", "INPUT", "FORWARD",
341 "OUTPUT", "POSTROUTING", "BROUTING",
342 };
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000343 static const char *const arp_names[] = {
344 "INPUT", "FORWARD", "OUTPUT",
345 };
346 const char *const *names;
347 unsigned int i, max;
Jan Engelhardt45185362009-04-05 10:43:09 +0200348 char *p = buf;
349 bool np = false;
350 int res;
351
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000352 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
353 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
354 ARRAY_SIZE(inetbr_names);
Jan Engelhardt45185362009-04-05 10:43:09 +0200355 *p = '\0';
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000356 for (i = 0; i < max; ++i) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200357 if (!(mask & (1 << i)))
358 continue;
359 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
360 if (res > 0) {
361 size -= res;
362 p += res;
363 }
364 np = true;
365 }
366
367 return buf;
368}
369
Jan Engelhardt916a9172008-10-08 11:35:20 +0200370int xt_check_match(struct xt_mtchk_param *par,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200371 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800372{
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100373 int ret;
374
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200375 if (XT_ALIGN(par->match->matchsize) != size &&
376 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200377 /*
378 * ebt_among is exempt from centralized matchsize checking
379 * because it uses a dynamic-size data set.
380 */
Jan Engelhardtb4024052009-06-25 18:32:12 +0200381 pr_err("%s_tables: %s.%u match: invalid size "
382 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200383 xt_prefix[par->family], par->match->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200384 par->match->revision,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200385 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800386 return -EINVAL;
387 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200388 if (par->match->table != NULL &&
389 strcmp(par->match->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200390 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200391 xt_prefix[par->family], par->match->name,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200392 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800393 return -EINVAL;
394 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200395 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200396 char used[64], allow[64];
397
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200398 pr_err("%s_tables: %s match: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200399 "valid from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200400 xt_prefix[par->family], par->match->name,
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000401 textify_hooks(used, sizeof(used), par->hook_mask,
402 par->family),
403 textify_hooks(allow, sizeof(allow), par->match->hooks,
404 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800405 return -EINVAL;
406 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200407 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200408 pr_err("%s_tables: %s match: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200409 xt_prefix[par->family], par->match->name,
410 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800411 return -EINVAL;
412 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100413 if (par->match->checkentry != NULL) {
414 ret = par->match->checkentry(par);
415 if (ret < 0)
416 return ret;
417 else if (ret > 0)
418 /* Flag up potential errors. */
419 return -EIO;
420 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800421 return 0;
422}
423EXPORT_SYMBOL_GPL(xt_check_match);
424
Florian Westphal13631bf2016-04-01 14:17:29 +0200425/** xt_check_entry_match - check that matches end before start of target
426 *
427 * @match: beginning of xt_entry_match
428 * @target: beginning of this rules target (alleged end of matches)
429 * @alignment: alignment requirement of match structures
430 *
431 * Validates that all matches add up to the beginning of the target,
432 * and that each match covers at least the base structure size.
433 *
434 * Return: 0 on success, negative errno on failure.
435 */
436static int xt_check_entry_match(const char *match, const char *target,
437 const size_t alignment)
438{
439 const struct xt_entry_match *pos;
440 int length = target - match;
441
442 if (length == 0) /* no matches */
443 return 0;
444
445 pos = (struct xt_entry_match *)match;
446 do {
447 if ((unsigned long)pos % alignment)
448 return -EINVAL;
449
450 if (length < (int)sizeof(struct xt_entry_match))
451 return -EINVAL;
452
453 if (pos->u.match_size < sizeof(struct xt_entry_match))
454 return -EINVAL;
455
456 if (pos->u.match_size > length)
457 return -EINVAL;
458
459 length -= pos->u.match_size;
460 pos = ((void *)((char *)(pos) + (pos)->u.match_size));
461 } while (length > 0);
462
463 return 0;
464}
465
Dmitry Mishin27229712006-04-01 02:25:19 -0800466#ifdef CONFIG_COMPAT
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100467int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800468{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100469 struct xt_af *xp = &xt[af];
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800470
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100471 if (!xp->compat_tab) {
472 if (!xp->number)
473 return -EINVAL;
474 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
475 if (!xp->compat_tab)
476 return -ENOMEM;
477 xp->cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800478 }
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100479
480 if (xp->cur >= xp->number)
481 return -EINVAL;
482
483 if (xp->cur)
484 delta += xp->compat_tab[xp->cur - 1].delta;
485 xp->compat_tab[xp->cur].offset = offset;
486 xp->compat_tab[xp->cur].delta = delta;
487 xp->cur++;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800488 return 0;
489}
490EXPORT_SYMBOL_GPL(xt_compat_add_offset);
491
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200492void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800493{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100494 if (xt[af].compat_tab) {
495 vfree(xt[af].compat_tab);
496 xt[af].compat_tab = NULL;
497 xt[af].number = 0;
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200498 xt[af].cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800499 }
500}
501EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
502
Florian Westphal3e5e5242010-02-15 18:17:10 +0100503int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800504{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100505 struct compat_delta *tmp = xt[af].compat_tab;
506 int mid, left = 0, right = xt[af].cur - 1;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800507
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100508 while (left <= right) {
509 mid = (left + right) >> 1;
510 if (offset > tmp[mid].offset)
511 left = mid + 1;
512 else if (offset < tmp[mid].offset)
513 right = mid - 1;
514 else
515 return mid ? tmp[mid - 1].delta : 0;
516 }
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200517 return left ? tmp[left - 1].delta : 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800518}
519EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
520
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100521void xt_compat_init_offsets(u_int8_t af, unsigned int number)
522{
523 xt[af].number = number;
524 xt[af].cur = 0;
525}
526EXPORT_SYMBOL(xt_compat_init_offsets);
527
Jan Engelhardt5452e422008-04-14 11:15:35 +0200528int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800529{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700530 u_int16_t csize = match->compatsize ? : match->matchsize;
531 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800532}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700533EXPORT_SYMBOL_GPL(xt_compat_match_offset);
534
Florian Westphal01883462016-04-01 14:17:33 +0200535void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
536 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700537{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200538 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700539 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
540 int pad, off = xt_compat_match_offset(match);
541 u_int16_t msize = cm->u.user.match_size;
Florian Westphal09d96862016-04-01 14:17:34 +0200542 char name[sizeof(m->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700543
544 m = *dstptr;
545 memcpy(m, cm, sizeof(*cm));
546 if (match->compat_from_user)
547 match->compat_from_user(m->data, cm->data);
548 else
549 memcpy(m->data, cm->data, msize - sizeof(*cm));
550 pad = XT_ALIGN(match->matchsize) - match->matchsize;
551 if (pad > 0)
552 memset(m->data + match->matchsize, 0, pad);
553
554 msize += off;
555 m->u.user.match_size = msize;
Florian Westphal09d96862016-04-01 14:17:34 +0200556 strlcpy(name, match->name, sizeof(name));
557 module_put(match->me);
558 strncpy(m->u.user.name, name, sizeof(m->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700559
560 *size += off;
561 *dstptr += msize;
562}
563EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
564
Jan Engelhardt739674f2009-06-26 08:23:19 +0200565int xt_compat_match_to_user(const struct xt_entry_match *m,
566 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700567{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200568 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700569 struct compat_xt_entry_match __user *cm = *dstptr;
570 int off = xt_compat_match_offset(match);
571 u_int16_t msize = m->u.user.match_size - off;
572
573 if (copy_to_user(cm, m, sizeof(*cm)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800574 put_user(msize, &cm->u.user.match_size) ||
575 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
576 strlen(m->u.kernel.match->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800577 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700578
579 if (match->compat_to_user) {
580 if (match->compat_to_user((void __user *)cm->data, m->data))
581 return -EFAULT;
582 } else {
583 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
584 return -EFAULT;
585 }
586
587 *size -= off;
588 *dstptr += msize;
589 return 0;
590}
591EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
Florian Westphalfc1221b2016-04-01 14:17:26 +0200592
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200593/* non-compat version may have padding after verdict */
594struct compat_xt_standard_target {
595 struct compat_xt_entry_target t;
596 compat_uint_t verdict;
597};
598
Florian Westphalce683e52016-04-01 14:17:28 +0200599int xt_compat_check_entry_offsets(const void *base, const char *elems,
Florian Westphalfc1221b2016-04-01 14:17:26 +0200600 unsigned int target_offset,
601 unsigned int next_offset)
602{
Florian Westphalce683e52016-04-01 14:17:28 +0200603 long size_of_base_struct = elems - (const char *)base;
Florian Westphalfc1221b2016-04-01 14:17:26 +0200604 const struct compat_xt_entry_target *t;
605 const char *e = base;
606
Florian Westphalce683e52016-04-01 14:17:28 +0200607 if (target_offset < size_of_base_struct)
608 return -EINVAL;
609
Florian Westphalfc1221b2016-04-01 14:17:26 +0200610 if (target_offset + sizeof(*t) > next_offset)
611 return -EINVAL;
612
613 t = (void *)(e + target_offset);
614 if (t->u.target_size < sizeof(*t))
615 return -EINVAL;
616
617 if (target_offset + t->u.target_size > next_offset)
618 return -EINVAL;
619
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200620 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
Florian Westphal7b7eba02016-06-01 02:04:44 +0200621 COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200622 return -EINVAL;
623
Florian Westphal13631bf2016-04-01 14:17:29 +0200624 /* compat_xt_entry match has less strict aligment requirements,
625 * otherwise they are identical. In case of padding differences
626 * we need to add compat version of xt_check_entry_match.
627 */
628 BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
629
630 return xt_check_entry_match(elems, base + target_offset,
631 __alignof__(struct compat_xt_entry_match));
Florian Westphalfc1221b2016-04-01 14:17:26 +0200632}
633EXPORT_SYMBOL(xt_compat_check_entry_offsets);
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700634#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800635
Florian Westphal7d358122016-04-01 14:17:23 +0200636/**
637 * xt_check_entry_offsets - validate arp/ip/ip6t_entry
638 *
639 * @base: pointer to arp/ip/ip6t_entry
Florian Westphalce683e52016-04-01 14:17:28 +0200640 * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
Florian Westphal7d358122016-04-01 14:17:23 +0200641 * @target_offset: the arp/ip/ip6_t->target_offset
642 * @next_offset: the arp/ip/ip6_t->next_offset
643 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200644 * validates that target_offset and next_offset are sane and that all
645 * match sizes (if any) align with the target offset.
Florian Westphal7d358122016-04-01 14:17:23 +0200646 *
Florian Westphalce683e52016-04-01 14:17:28 +0200647 * This function does not validate the targets or matches themselves, it
Florian Westphal13631bf2016-04-01 14:17:29 +0200648 * only tests that all the offsets and sizes are correct, that all
649 * match structures are aligned, and that the last structure ends where
650 * the target structure begins.
651 *
652 * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
Florian Westphalce683e52016-04-01 14:17:28 +0200653 *
Florian Westphal7d358122016-04-01 14:17:23 +0200654 * The arp/ip/ip6t_entry structure @base must have passed following tests:
655 * - it must point to a valid memory location
656 * - base to base + next_offset must be accessible, i.e. not exceed allocated
657 * length.
658 *
Florian Westphal13631bf2016-04-01 14:17:29 +0200659 * A well-formed entry looks like this:
660 *
661 * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
662 * e->elems[]-----' | |
663 * matchsize | |
664 * matchsize | |
665 * | |
666 * target_offset---------------------------------' |
667 * next_offset---------------------------------------------------'
668 *
669 * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
670 * This is where matches (if any) and the target reside.
671 * target_offset: beginning of target.
672 * next_offset: start of the next rule; also: size of this rule.
673 * Since targets have a minimum size, target_offset + minlen <= next_offset.
674 *
675 * Every match stores its size, sum of sizes must not exceed target_offset.
676 *
Florian Westphal7d358122016-04-01 14:17:23 +0200677 * Return: 0 on success, negative errno on failure.
678 */
679int xt_check_entry_offsets(const void *base,
Florian Westphalce683e52016-04-01 14:17:28 +0200680 const char *elems,
Florian Westphal7d358122016-04-01 14:17:23 +0200681 unsigned int target_offset,
682 unsigned int next_offset)
683{
Florian Westphalce683e52016-04-01 14:17:28 +0200684 long size_of_base_struct = elems - (const char *)base;
Florian Westphal7d358122016-04-01 14:17:23 +0200685 const struct xt_entry_target *t;
686 const char *e = base;
687
Florian Westphalce683e52016-04-01 14:17:28 +0200688 /* target start is within the ip/ip6/arpt_entry struct */
689 if (target_offset < size_of_base_struct)
690 return -EINVAL;
691
Florian Westphal7d358122016-04-01 14:17:23 +0200692 if (target_offset + sizeof(*t) > next_offset)
693 return -EINVAL;
694
695 t = (void *)(e + target_offset);
Florian Westphala08e4e12016-04-01 14:17:25 +0200696 if (t->u.target_size < sizeof(*t))
697 return -EINVAL;
698
Florian Westphal7d358122016-04-01 14:17:23 +0200699 if (target_offset + t->u.target_size > next_offset)
700 return -EINVAL;
701
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200702 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
Florian Westphal7b7eba02016-06-01 02:04:44 +0200703 XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
Florian Westphal7ed2abd2016-04-01 14:17:27 +0200704 return -EINVAL;
705
Florian Westphal13631bf2016-04-01 14:17:29 +0200706 return xt_check_entry_match(elems, base + target_offset,
707 __alignof__(struct xt_entry_match));
Florian Westphal7d358122016-04-01 14:17:23 +0200708}
709EXPORT_SYMBOL(xt_check_entry_offsets);
710
Florian Westphalf4dc7772016-07-14 17:51:26 +0200711/**
712 * xt_alloc_entry_offsets - allocate array to store rule head offsets
713 *
714 * @size: number of entries
715 *
716 * Return: NULL or kmalloc'd or vmalloc'd array
717 */
718unsigned int *xt_alloc_entry_offsets(unsigned int size)
719{
720 unsigned int *off;
721
722 off = kcalloc(size, sizeof(unsigned int), GFP_KERNEL | __GFP_NOWARN);
723
724 if (off)
725 return off;
726
727 if (size < (SIZE_MAX / sizeof(unsigned int)))
728 off = vmalloc(size * sizeof(unsigned int));
729
730 return off;
731}
732EXPORT_SYMBOL(xt_alloc_entry_offsets);
733
734/**
735 * xt_find_jump_offset - check if target is a valid jump offset
736 *
737 * @offsets: array containing all valid rule start offsets of a rule blob
738 * @target: the jump target to search for
739 * @size: entries in @offset
740 */
741bool xt_find_jump_offset(const unsigned int *offsets,
742 unsigned int target, unsigned int size)
743{
744 int m, low = 0, hi = size;
745
746 while (hi > low) {
747 m = (low + hi) / 2u;
748
749 if (offsets[m] > target)
750 hi = m;
751 else if (offsets[m] < target)
752 low = m + 1;
753 else
754 return true;
755 }
756
757 return false;
758}
759EXPORT_SYMBOL(xt_find_jump_offset);
760
Jan Engelhardt916a9172008-10-08 11:35:20 +0200761int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200762 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800763{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100764 int ret;
765
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200766 if (XT_ALIGN(par->target->targetsize) != size) {
Jan Engelhardtb4024052009-06-25 18:32:12 +0200767 pr_err("%s_tables: %s.%u target: invalid size "
768 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200769 xt_prefix[par->family], par->target->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200770 par->target->revision,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200771 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800772 return -EINVAL;
773 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200774 if (par->target->table != NULL &&
775 strcmp(par->target->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200776 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200777 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200778 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800779 return -EINVAL;
780 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200781 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200782 char used[64], allow[64];
783
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200784 pr_err("%s_tables: %s target: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200785 "usable from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200786 xt_prefix[par->family], par->target->name,
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000787 textify_hooks(used, sizeof(used), par->hook_mask,
788 par->family),
789 textify_hooks(allow, sizeof(allow), par->target->hooks,
790 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800791 return -EINVAL;
792 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200793 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200794 pr_err("%s_tables: %s target: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200795 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200796 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800797 return -EINVAL;
798 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100799 if (par->target->checkentry != NULL) {
800 ret = par->target->checkentry(par);
801 if (ret < 0)
802 return ret;
803 else if (ret > 0)
804 /* Flag up potential errors. */
805 return -EIO;
806 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800807 return 0;
808}
809EXPORT_SYMBOL_GPL(xt_check_target);
810
Florian Westphald7591f02016-04-01 15:37:59 +0200811/**
812 * xt_copy_counters_from_user - copy counters and metadata from userspace
813 *
814 * @user: src pointer to userspace memory
815 * @len: alleged size of userspace memory
816 * @info: where to store the xt_counters_info metadata
817 * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
818 *
819 * Copies counter meta data from @user and stores it in @info.
820 *
821 * vmallocs memory to hold the counters, then copies the counter data
822 * from @user to the new memory and returns a pointer to it.
823 *
824 * If @compat is true, @info gets converted automatically to the 64bit
825 * representation.
826 *
827 * The metadata associated with the counters is stored in @info.
828 *
829 * Return: returns pointer that caller has to test via IS_ERR().
830 * If IS_ERR is false, caller has to vfree the pointer.
831 */
832void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
833 struct xt_counters_info *info, bool compat)
834{
835 void *mem;
836 u64 size;
837
838#ifdef CONFIG_COMPAT
839 if (compat) {
840 /* structures only differ in size due to alignment */
841 struct compat_xt_counters_info compat_tmp;
842
843 if (len <= sizeof(compat_tmp))
844 return ERR_PTR(-EINVAL);
845
846 len -= sizeof(compat_tmp);
847 if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
848 return ERR_PTR(-EFAULT);
849
850 strlcpy(info->name, compat_tmp.name, sizeof(info->name));
851 info->num_counters = compat_tmp.num_counters;
852 user += sizeof(compat_tmp);
853 } else
854#endif
855 {
856 if (len <= sizeof(*info))
857 return ERR_PTR(-EINVAL);
858
859 len -= sizeof(*info);
860 if (copy_from_user(info, user, sizeof(*info)) != 0)
861 return ERR_PTR(-EFAULT);
862
863 info->name[sizeof(info->name) - 1] = '\0';
864 user += sizeof(*info);
865 }
866
867 size = sizeof(struct xt_counters);
868 size *= info->num_counters;
869
870 if (size != (u64)len)
871 return ERR_PTR(-EINVAL);
872
873 mem = vmalloc(len);
874 if (!mem)
875 return ERR_PTR(-ENOMEM);
876
877 if (copy_from_user(mem, user, len) == 0)
878 return mem;
879
880 vfree(mem);
881 return ERR_PTR(-EFAULT);
882}
883EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
884
Dmitry Mishin27229712006-04-01 02:25:19 -0800885#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +0200886int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800887{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700888 u_int16_t csize = target->compatsize ? : target->targetsize;
889 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800890}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700891EXPORT_SYMBOL_GPL(xt_compat_target_offset);
892
893void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800894 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700895{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200896 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700897 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
898 int pad, off = xt_compat_target_offset(target);
899 u_int16_t tsize = ct->u.user.target_size;
Florian Westphal09d96862016-04-01 14:17:34 +0200900 char name[sizeof(t->u.user.name)];
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700901
902 t = *dstptr;
903 memcpy(t, ct, sizeof(*ct));
904 if (target->compat_from_user)
905 target->compat_from_user(t->data, ct->data);
906 else
907 memcpy(t->data, ct->data, tsize - sizeof(*ct));
908 pad = XT_ALIGN(target->targetsize) - target->targetsize;
909 if (pad > 0)
910 memset(t->data + target->targetsize, 0, pad);
911
912 tsize += off;
913 t->u.user.target_size = tsize;
Florian Westphal09d96862016-04-01 14:17:34 +0200914 strlcpy(name, target->name, sizeof(name));
915 module_put(target->me);
916 strncpy(t->u.user.name, name, sizeof(t->u.user.name));
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700917
918 *size += off;
919 *dstptr += tsize;
920}
921EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
922
Jan Engelhardt739674f2009-06-26 08:23:19 +0200923int xt_compat_target_to_user(const struct xt_entry_target *t,
924 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700925{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200926 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700927 struct compat_xt_entry_target __user *ct = *dstptr;
928 int off = xt_compat_target_offset(target);
929 u_int16_t tsize = t->u.user.target_size - off;
930
931 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800932 put_user(tsize, &ct->u.user.target_size) ||
933 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
934 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800935 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700936
937 if (target->compat_to_user) {
938 if (target->compat_to_user((void __user *)ct->data, t->data))
939 return -EFAULT;
940 } else {
941 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
942 return -EFAULT;
943 }
944
945 *size -= off;
946 *dstptr += tsize;
947 return 0;
948}
949EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -0800950#endif
951
Harald Welte2e4e6a12006-01-12 13:30:04 -0800952struct xt_table_info *xt_alloc_table_info(unsigned int size)
953{
Eric Dumazet711bdde2015-06-15 09:57:30 -0700954 struct xt_table_info *info = NULL;
955 size_t sz = sizeof(*info) + size;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800956
Florian Westphald157bd72016-03-10 01:56:23 +0100957 if (sz < sizeof(*info))
958 return NULL;
959
Harald Welte2e4e6a12006-01-12 13:30:04 -0800960 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
Dmitry Vyukov1099c702017-12-28 09:48:54 +0100961 if ((size >> PAGE_SHIFT) + 2 > totalram_pages)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800962 return NULL;
963
Eric Dumazet711bdde2015-06-15 09:57:30 -0700964 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
965 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
966 if (!info) {
967 info = vmalloc(sz);
968 if (!info)
969 return NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800970 }
Eric Dumazet711bdde2015-06-15 09:57:30 -0700971 memset(info, 0, sizeof(*info));
972 info->size = size;
973 return info;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800974}
975EXPORT_SYMBOL(xt_alloc_table_info);
976
977void xt_free_table_info(struct xt_table_info *info)
978{
979 int cpu;
980
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200981 if (info->jumpstack != NULL) {
Eric Dumazetf6b50822014-06-24 02:15:35 -0700982 for_each_possible_cpu(cpu)
983 kvfree(info->jumpstack[cpu]);
984 kvfree(info->jumpstack);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200985 }
986
Eric Dumazet711bdde2015-06-15 09:57:30 -0700987 kvfree(info);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800988}
989EXPORT_SYMBOL(xt_free_table_info);
990
991/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200992struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
993 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800994{
Florian Westphalb9e69e12016-02-25 10:08:36 +0100995 struct xt_table *t, *found = NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800996
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200997 mutex_lock(&xt[af].mutex);
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800998 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800999 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
1000 return t;
Florian Westphalb9e69e12016-02-25 10:08:36 +01001001
1002 if (net == &init_net)
1003 goto out;
1004
1005 /* Table doesn't exist in this netns, re-try init */
1006 list_for_each_entry(t, &init_net.xt.tables[af], list) {
1007 if (strcmp(t->name, name))
1008 continue;
Dan Carpenterc70c7be2017-04-28 15:57:56 +03001009 if (!try_module_get(t->me)) {
1010 mutex_unlock(&xt[af].mutex);
Florian Westphalb9e69e12016-02-25 10:08:36 +01001011 return NULL;
Dan Carpenterc70c7be2017-04-28 15:57:56 +03001012 }
Florian Westphalb9e69e12016-02-25 10:08:36 +01001013
1014 mutex_unlock(&xt[af].mutex);
1015 if (t->table_init(net) != 0) {
1016 module_put(t->me);
1017 return NULL;
1018 }
1019
1020 found = t;
1021
1022 mutex_lock(&xt[af].mutex);
1023 break;
1024 }
1025
1026 if (!found)
1027 goto out;
1028
1029 /* and once again: */
1030 list_for_each_entry(t, &net->xt.tables[af], list)
1031 if (strcmp(t->name, name) == 0)
1032 return t;
1033
1034 module_put(found->me);
1035 out:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001036 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001037 return NULL;
1038}
1039EXPORT_SYMBOL_GPL(xt_find_table_lock);
1040
1041void xt_table_unlock(struct xt_table *table)
1042{
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001043 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001044}
1045EXPORT_SYMBOL_GPL(xt_table_unlock);
1046
Dmitry Mishin27229712006-04-01 02:25:19 -08001047#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001048void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001049{
1050 mutex_lock(&xt[af].compat_mutex);
1051}
1052EXPORT_SYMBOL_GPL(xt_compat_lock);
1053
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001054void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -08001055{
1056 mutex_unlock(&xt[af].compat_mutex);
1057}
1058EXPORT_SYMBOL_GPL(xt_compat_unlock);
1059#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001060
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001061DEFINE_PER_CPU(seqcount_t, xt_recseq);
1062EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001063
Florian Westphaldcebd312015-07-14 17:51:09 +02001064struct static_key xt_tee_enabled __read_mostly;
1065EXPORT_SYMBOL_GPL(xt_tee_enabled);
1066
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001067static int xt_jumpstack_alloc(struct xt_table_info *i)
1068{
1069 unsigned int size;
1070 int cpu;
1071
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001072 size = sizeof(void **) * nr_cpu_ids;
1073 if (size > PAGE_SIZE)
Joe Perches3dbd4432011-05-28 10:36:35 -07001074 i->jumpstack = vzalloc(size);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001075 else
Joe Perches3dbd4432011-05-28 10:36:35 -07001076 i->jumpstack = kzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001077 if (i->jumpstack == NULL)
1078 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001079
Florian Westphal98d1bd82015-07-14 17:51:06 +02001080 /* ruleset without jumps -- no stack needed */
1081 if (i->stacksize == 0)
1082 return 0;
1083
Florian Westphal7814b6e2015-07-14 17:51:08 +02001084 /* Jumpstack needs to be able to record two full callchains, one
1085 * from the first rule set traversal, plus one table reentrancy
1086 * via -j TEE without clobbering the callchain that brought us to
1087 * TEE target.
1088 *
1089 * This is done by allocating two jumpstacks per cpu, on reentry
1090 * the upper half of the stack is used.
1091 *
1092 * see the jumpstack setup in ipt_do_table() for more details.
1093 */
1094 size = sizeof(void *) * i->stacksize * 2u;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001095 for_each_possible_cpu(cpu) {
1096 if (size > PAGE_SIZE)
1097 i->jumpstack[cpu] = vmalloc_node(size,
1098 cpu_to_node(cpu));
1099 else
1100 i->jumpstack[cpu] = kmalloc_node(size,
1101 GFP_KERNEL, cpu_to_node(cpu));
1102 if (i->jumpstack[cpu] == NULL)
1103 /*
1104 * Freeing will be done later on by the callers. The
1105 * chain is: xt_replace_table -> __do_replace ->
1106 * do_replace -> xt_free_table_info.
1107 */
1108 return -ENOMEM;
1109 }
1110
1111 return 0;
1112}
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001113
Harald Welte2e4e6a12006-01-12 13:30:04 -08001114struct xt_table_info *
1115xt_replace_table(struct xt_table *table,
1116 unsigned int num_counters,
1117 struct xt_table_info *newinfo,
1118 int *error)
1119{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001120 struct xt_table_info *private;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +02001121 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001122
Jan Engelhardtd97a9e42010-04-21 14:45:51 +02001123 ret = xt_jumpstack_alloc(newinfo);
1124 if (ret < 0) {
1125 *error = ret;
1126 return NULL;
1127 }
1128
Harald Welte2e4e6a12006-01-12 13:30:04 -08001129 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001130 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001131 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001132
Harald Welte2e4e6a12006-01-12 13:30:04 -08001133 /* Check inside lock: is the old number correct? */
1134 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001135 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -08001136 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001137 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -08001138 *error = -EAGAIN;
1139 return NULL;
1140 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001141
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001142 newinfo->initial_entries = private->initial_entries;
Will Deaconb416c142013-10-21 13:14:53 +01001143 /*
1144 * Ensure contents of newinfo are visible before assigning to
1145 * private.
1146 */
1147 smp_wmb();
1148 table->private = newinfo;
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001149
1150 /*
1151 * Even though table entries have now been swapped, other CPU's
1152 * may still be using the old entries. This is okay, because
1153 * resynchronization happens because of the locking done
1154 * during the get_counters() routine.
1155 */
1156 local_bh_enable();
1157
Thomas Graffbabf312011-01-16 18:12:59 +01001158#ifdef CONFIG_AUDIT
1159 if (audit_enabled) {
1160 struct audit_buffer *ab;
1161
1162 ab = audit_log_start(current->audit_context, GFP_KERNEL,
1163 AUDIT_NETFILTER_CFG);
1164 if (ab) {
1165 audit_log_format(ab, "table=%s family=%u entries=%u",
1166 table->name, table->af,
1167 private->number);
1168 audit_log_end(ab);
1169 }
1170 }
1171#endif
1172
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001173 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001174}
1175EXPORT_SYMBOL_GPL(xt_replace_table);
1176
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001177struct xt_table *xt_register_table(struct net *net,
1178 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -08001179 struct xt_table_info *bootstrap,
1180 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001181{
1182 int ret;
1183 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001184 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001185
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001186 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +02001187 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001188 if (!table) {
1189 ret = -ENOMEM;
1190 goto out;
1191 }
1192
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001193 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001194 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001195 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -07001196 if (strcmp(t->name, table->name) == 0) {
1197 ret = -EEXIST;
1198 goto unlock;
1199 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001200 }
1201
1202 /* Simplifies replace_table code. */
1203 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +01001204
Harald Welte2e4e6a12006-01-12 13:30:04 -08001205 if (!xt_replace_table(table, 0, newinfo, &ret))
1206 goto unlock;
1207
1208 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001209 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001210
1211 /* save number of initial entries */
1212 private->initial_entries = private->number;
1213
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001214 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001215 mutex_unlock(&xt[table->af].mutex);
1216 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001217
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +02001218unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001219 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001220 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -08001221out:
1222 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001223}
1224EXPORT_SYMBOL_GPL(xt_register_table);
1225
1226void *xt_unregister_table(struct xt_table *table)
1227{
1228 struct xt_table_info *private;
1229
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001230 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001231 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -07001232 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001233 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -08001234 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001235
1236 return private;
1237}
1238EXPORT_SYMBOL_GPL(xt_unregister_table);
1239
1240#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001241struct xt_names_priv {
1242 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001243 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001244};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001245static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001246{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001247 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001248 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001249 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001250
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001251 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001252 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001253}
1254
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001255static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001256{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001257 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09001258 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001259 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001260
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001261 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001262}
1263
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001264static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001265{
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001266 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001267 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001268
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001269 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001270}
1271
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001272static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001273{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001274 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001275
Joe Perches861fb102015-05-12 18:28:23 -07001276 if (*table->name)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -04001277 seq_printf(seq, "%s\n", table->name);
Joe Perches861fb102015-05-12 18:28:23 -07001278 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001279}
1280
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001281static const struct seq_operations xt_table_seq_ops = {
1282 .start = xt_table_seq_start,
1283 .next = xt_table_seq_next,
1284 .stop = xt_table_seq_stop,
1285 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001286};
1287
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001288static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001289{
1290 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001291 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001292
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001293 ret = seq_open_net(inode, file, &xt_table_seq_ops,
1294 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001295 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -08001296 priv = ((struct seq_file *)file->private_data)->private;
Al Virod9dda782013-03-31 18:16:14 -04001297 priv->af = (unsigned long)PDE_DATA(inode);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001298 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001299 return ret;
1300}
1301
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001302static const struct file_operations xt_table_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -08001303 .owner = THIS_MODULE,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001304 .open = xt_table_open,
1305 .read = seq_read,
1306 .llseek = seq_lseek,
Pavel Emelyanov0e93bb92008-04-29 03:15:35 -07001307 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001308};
1309
Jan Engelhardteb132202009-02-18 16:42:19 +01001310/*
1311 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1312 * the multi-AF mutexes.
1313 */
1314struct nf_mttg_trav {
1315 struct list_head *head, *curr;
1316 uint8_t class, nfproto;
1317};
1318
1319enum {
1320 MTTG_TRAV_INIT,
1321 MTTG_TRAV_NFP_UNSPEC,
1322 MTTG_TRAV_NFP_SPEC,
1323 MTTG_TRAV_DONE,
1324};
1325
1326static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1327 bool is_target)
1328{
1329 static const uint8_t next_class[] = {
1330 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1331 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1332 };
1333 struct nf_mttg_trav *trav = seq->private;
1334
1335 switch (trav->class) {
1336 case MTTG_TRAV_INIT:
1337 trav->class = MTTG_TRAV_NFP_UNSPEC;
1338 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1339 trav->head = trav->curr = is_target ?
1340 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1341 break;
1342 case MTTG_TRAV_NFP_UNSPEC:
1343 trav->curr = trav->curr->next;
1344 if (trav->curr != trav->head)
1345 break;
1346 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1347 mutex_lock(&xt[trav->nfproto].mutex);
1348 trav->head = trav->curr = is_target ?
1349 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1350 trav->class = next_class[trav->class];
1351 break;
1352 case MTTG_TRAV_NFP_SPEC:
1353 trav->curr = trav->curr->next;
1354 if (trav->curr != trav->head)
1355 break;
1356 /* fallthru, _stop will unlock */
1357 default:
1358 return NULL;
1359 }
1360
1361 if (ppos != NULL)
1362 ++*ppos;
1363 return trav;
1364}
1365
1366static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1367 bool is_target)
1368{
1369 struct nf_mttg_trav *trav = seq->private;
1370 unsigned int j;
1371
1372 trav->class = MTTG_TRAV_INIT;
1373 for (j = 0; j < *pos; ++j)
1374 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1375 return NULL;
1376 return trav;
1377}
1378
1379static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1380{
1381 struct nf_mttg_trav *trav = seq->private;
1382
1383 switch (trav->class) {
1384 case MTTG_TRAV_NFP_UNSPEC:
1385 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1386 break;
1387 case MTTG_TRAV_NFP_SPEC:
1388 mutex_unlock(&xt[trav->nfproto].mutex);
1389 break;
1390 }
1391}
1392
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001393static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1394{
Jan Engelhardteb132202009-02-18 16:42:19 +01001395 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001396}
1397
Jan Engelhardteb132202009-02-18 16:42:19 +01001398static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001399{
Jan Engelhardteb132202009-02-18 16:42:19 +01001400 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001401}
1402
1403static int xt_match_seq_show(struct seq_file *seq, void *v)
1404{
Jan Engelhardteb132202009-02-18 16:42:19 +01001405 const struct nf_mttg_trav *trav = seq->private;
1406 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001407
Jan Engelhardteb132202009-02-18 16:42:19 +01001408 switch (trav->class) {
1409 case MTTG_TRAV_NFP_UNSPEC:
1410 case MTTG_TRAV_NFP_SPEC:
1411 if (trav->curr == trav->head)
1412 return 0;
1413 match = list_entry(trav->curr, struct xt_match, list);
Joe Perches861fb102015-05-12 18:28:23 -07001414 if (*match->name)
1415 seq_printf(seq, "%s\n", match->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001416 }
1417 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001418}
1419
1420static const struct seq_operations xt_match_seq_ops = {
1421 .start = xt_match_seq_start,
1422 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001423 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001424 .show = xt_match_seq_show,
1425};
1426
1427static int xt_match_open(struct inode *inode, struct file *file)
1428{
Jan Engelhardteb132202009-02-18 16:42:19 +01001429 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001430 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1431 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001432 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001433
Al Virod9dda782013-03-31 18:16:14 -04001434 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001435 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001436}
1437
1438static const struct file_operations xt_match_ops = {
1439 .owner = THIS_MODULE,
1440 .open = xt_match_open,
1441 .read = seq_read,
1442 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001443 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001444};
1445
1446static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1447{
Jan Engelhardteb132202009-02-18 16:42:19 +01001448 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001449}
1450
Jan Engelhardteb132202009-02-18 16:42:19 +01001451static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001452{
Jan Engelhardteb132202009-02-18 16:42:19 +01001453 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001454}
1455
1456static int xt_target_seq_show(struct seq_file *seq, void *v)
1457{
Jan Engelhardteb132202009-02-18 16:42:19 +01001458 const struct nf_mttg_trav *trav = seq->private;
1459 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001460
Jan Engelhardteb132202009-02-18 16:42:19 +01001461 switch (trav->class) {
1462 case MTTG_TRAV_NFP_UNSPEC:
1463 case MTTG_TRAV_NFP_SPEC:
1464 if (trav->curr == trav->head)
1465 return 0;
1466 target = list_entry(trav->curr, struct xt_target, list);
Joe Perches861fb102015-05-12 18:28:23 -07001467 if (*target->name)
1468 seq_printf(seq, "%s\n", target->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001469 }
1470 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001471}
1472
1473static const struct seq_operations xt_target_seq_ops = {
1474 .start = xt_target_seq_start,
1475 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001476 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001477 .show = xt_target_seq_show,
1478};
1479
1480static int xt_target_open(struct inode *inode, struct file *file)
1481{
Jan Engelhardteb132202009-02-18 16:42:19 +01001482 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001483 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1484 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001485 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001486
Al Virod9dda782013-03-31 18:16:14 -04001487 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001488 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001489}
1490
1491static const struct file_operations xt_target_ops = {
1492 .owner = THIS_MODULE,
1493 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001494 .read = seq_read,
1495 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001496 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001497};
1498
1499#define FORMAT_TABLES "_tables_names"
1500#define FORMAT_MATCHES "_tables_matches"
1501#define FORMAT_TARGETS "_tables_targets"
1502
1503#endif /* CONFIG_PROC_FS */
1504
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001505/**
Florian Westphalb9e69e12016-02-25 10:08:36 +01001506 * xt_hook_ops_alloc - set up hooks for a new table
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001507 * @table: table with metadata needed to set up hooks
1508 * @fn: Hook function
1509 *
Florian Westphalb9e69e12016-02-25 10:08:36 +01001510 * This function will create the nf_hook_ops that the x_table needs
1511 * to hand to xt_hook_link_net().
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001512 */
Florian Westphalb9e69e12016-02-25 10:08:36 +01001513struct nf_hook_ops *
1514xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001515{
1516 unsigned int hook_mask = table->valid_hooks;
1517 uint8_t i, num_hooks = hweight32(hook_mask);
1518 uint8_t hooknum;
1519 struct nf_hook_ops *ops;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001520
Xiubo Lia6d0bae2016-06-02 10:59:56 +08001521 if (!num_hooks)
1522 return ERR_PTR(-EINVAL);
1523
Florian Westphal1ecc2812016-10-17 21:50:23 +02001524 ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001525 if (ops == NULL)
1526 return ERR_PTR(-ENOMEM);
1527
1528 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1529 hook_mask >>= 1, ++hooknum) {
1530 if (!(hook_mask & 1))
1531 continue;
1532 ops[i].hook = fn;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001533 ops[i].pf = table->af;
1534 ops[i].hooknum = hooknum;
1535 ops[i].priority = table->priority;
1536 ++i;
1537 }
1538
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001539 return ops;
1540}
Florian Westphalb9e69e12016-02-25 10:08:36 +01001541EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001542
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001543int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001544{
1545#ifdef CONFIG_PROC_FS
1546 char buf[XT_FUNCTION_MAXNAMELEN];
1547 struct proc_dir_entry *proc;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001548 kuid_t root_uid;
1549 kgid_t root_gid;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001550#endif
1551
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001552 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001553 return -EINVAL;
1554
1555
1556#ifdef CONFIG_PROC_FS
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001557 root_uid = make_kuid(net->user_ns, 0);
1558 root_gid = make_kgid(net->user_ns, 0);
1559
Tobias Klauserce18afe2007-03-14 16:36:16 -07001560 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001561 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001562 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1563 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001564 if (!proc)
1565 goto out;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001566 if (uid_valid(root_uid) && gid_valid(root_gid))
1567 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001568
Tobias Klauserce18afe2007-03-14 16:36:16 -07001569 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001570 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001571 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1572 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001573 if (!proc)
1574 goto out_remove_tables;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001575 if (uid_valid(root_uid) && gid_valid(root_gid))
1576 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001577
Tobias Klauserce18afe2007-03-14 16:36:16 -07001578 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001579 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001580 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1581 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001582 if (!proc)
1583 goto out_remove_matches;
Philip Whinerayf13f2ae2015-11-22 11:35:07 +00001584 if (uid_valid(root_uid) && gid_valid(root_gid))
1585 proc_set_user(proc, root_uid, root_gid);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001586#endif
1587
1588 return 0;
1589
1590#ifdef CONFIG_PROC_FS
1591out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001592 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001593 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001594 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001595
1596out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001597 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001598 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001599 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001600out:
1601 return -1;
1602#endif
1603}
1604EXPORT_SYMBOL_GPL(xt_proto_init);
1605
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001606void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001607{
1608#ifdef CONFIG_PROC_FS
1609 char buf[XT_FUNCTION_MAXNAMELEN];
1610
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_TABLES, 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
Tobias Klauserce18afe2007-03-14 16:36:16 -07001615 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001616 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001617 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001618
Tobias Klauserce18afe2007-03-14 16:36:16 -07001619 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001620 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001621 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001622#endif /*CONFIG_PROC_FS*/
1623}
1624EXPORT_SYMBOL_GPL(xt_proto_fini);
1625
Florian Westphaldac44482016-11-22 14:44:18 +01001626/**
1627 * xt_percpu_counter_alloc - allocate x_tables rule counter
1628 *
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001629 * @state: pointer to xt_percpu allocation state
Florian Westphaldac44482016-11-22 14:44:18 +01001630 * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1631 *
1632 * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1633 * contain the address of the real (percpu) counter.
1634 *
1635 * Rule evaluation needs to use xt_get_this_cpu_counter() helper
1636 * to fetch the real percpu counter.
1637 *
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001638 * To speed up allocation and improve data locality, a 4kb block is
1639 * allocated.
1640 *
1641 * xt_percpu_counter_alloc_state contains the base address of the
1642 * allocated page and the current sub-offset.
1643 *
Florian Westphaldac44482016-11-22 14:44:18 +01001644 * returns false on error.
1645 */
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001646bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
1647 struct xt_counters *counter)
Florian Westphaldac44482016-11-22 14:44:18 +01001648{
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001649 BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
Florian Westphaldac44482016-11-22 14:44:18 +01001650
1651 if (nr_cpu_ids <= 1)
1652 return true;
1653
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001654 if (!state->mem) {
1655 state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
1656 XT_PCPU_BLOCK_SIZE);
1657 if (!state->mem)
1658 return false;
1659 }
1660 counter->pcnt = (__force unsigned long)(state->mem + state->off);
1661 state->off += sizeof(*counter);
1662 if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
1663 state->mem = NULL;
1664 state->off = 0;
1665 }
Florian Westphaldac44482016-11-22 14:44:18 +01001666 return true;
1667}
1668EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1669
Florian Westphal61346e22016-11-22 14:44:17 +01001670void xt_percpu_counter_free(struct xt_counters *counters)
1671{
1672 unsigned long pcnt = counters->pcnt;
1673
Florian Westphaldb6a0cb2016-11-22 14:44:19 +01001674 if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
Florian Westphal61346e22016-11-22 14:44:17 +01001675 free_percpu((void __percpu *)pcnt);
1676}
1677EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
1678
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001679static int __net_init xt_net_init(struct net *net)
1680{
1681 int i;
1682
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001683 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001684 INIT_LIST_HEAD(&net->xt.tables[i]);
1685 return 0;
1686}
1687
1688static struct pernet_operations xt_net_ops = {
1689 .init = xt_net_init,
1690};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001691
1692static int __init xt_init(void)
1693{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001694 unsigned int i;
1695 int rv;
1696
1697 for_each_possible_cpu(i) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001698 seqcount_init(&per_cpu(xt_recseq, i));
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001699 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001700
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001701 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001702 if (!xt)
1703 return -ENOMEM;
1704
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001705 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001706 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001707#ifdef CONFIG_COMPAT
1708 mutex_init(&xt[i].compat_mutex);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001709 xt[i].compat_tab = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001710#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001711 INIT_LIST_HEAD(&xt[i].target);
1712 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001713 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001714 rv = register_pernet_subsys(&xt_net_ops);
1715 if (rv < 0)
1716 kfree(xt);
1717 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001718}
1719
1720static void __exit xt_fini(void)
1721{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001722 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001723 kfree(xt);
1724}
1725
1726module_init(xt_init);
1727module_exit(xt_fini);
1728