blob: d324fe71260c9f24b02507e4f429c0ba1e328d98 [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>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020029#include <net/net_namespace.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080030
31#include <linux/netfilter/x_tables.h>
32#include <linux/netfilter_arp.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020033#include <linux/netfilter_ipv4/ip_tables.h>
34#include <linux/netfilter_ipv6/ip6_tables.h>
35#include <linux/netfilter_arp/arp_tables.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080036
Harald Welte2e4e6a12006-01-12 13:30:04 -080037MODULE_LICENSE("GPL");
38MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt043ef462008-10-08 11:35:15 +020039MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080040
41#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
42
Patrick McHardyb386d9f2007-12-17 21:47:48 -080043struct compat_delta {
Eric Dumazet255d0dc2010-12-18 18:35:15 +010044 unsigned int offset; /* offset in kernel */
45 int delta; /* delta in 32bit user land */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080046};
47
Harald Welte2e4e6a12006-01-12 13:30:04 -080048struct xt_af {
Ingo Molnar9e19bb62006-03-25 01:41:10 -080049 struct mutex mutex;
Harald Welte2e4e6a12006-01-12 13:30:04 -080050 struct list_head match;
51 struct list_head target;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080052#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080053 struct mutex compat_mutex;
Eric Dumazet255d0dc2010-12-18 18:35:15 +010054 struct compat_delta *compat_tab;
55 unsigned int number; /* number of slots in compat_tab[] */
56 unsigned int cur; /* number of used slots in compat_tab[] */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080057#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080058};
59
60static struct xt_af *xt;
61
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020062static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
63 [NFPROTO_UNSPEC] = "x",
64 [NFPROTO_IPV4] = "ip",
65 [NFPROTO_ARP] = "arp",
66 [NFPROTO_BRIDGE] = "eb",
67 [NFPROTO_IPV6] = "ip6",
Patrick McHardy37f9f732006-03-20 17:59:06 -080068};
69
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +020070/* Allow this many total (re)entries. */
71static const unsigned int xt_jumpstack_multiplier = 2;
72
Harald Welte2e4e6a12006-01-12 13:30:04 -080073/* Registration hooks for targets. */
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020074int xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080075{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020076 u_int8_t af = target->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -080077
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020078 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080079 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080080 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +020081 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -080082}
83EXPORT_SYMBOL(xt_register_target);
84
85void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080086xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080087{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020088 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080089
Ingo Molnar9e19bb62006-03-25 01:41:10 -080090 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070091 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080092 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080093}
94EXPORT_SYMBOL(xt_unregister_target);
95
96int
Patrick McHardy52d9c422006-08-22 00:33:45 -070097xt_register_targets(struct xt_target *target, unsigned int n)
98{
99 unsigned int i;
100 int err = 0;
101
102 for (i = 0; i < n; i++) {
103 err = xt_register_target(&target[i]);
104 if (err)
105 goto err;
106 }
107 return err;
108
109err:
110 if (i > 0)
111 xt_unregister_targets(target, i);
112 return err;
113}
114EXPORT_SYMBOL(xt_register_targets);
115
116void
117xt_unregister_targets(struct xt_target *target, unsigned int n)
118{
Changli Gaof68c5302010-10-04 22:24:12 +0200119 while (n-- > 0)
120 xt_unregister_target(&target[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700121}
122EXPORT_SYMBOL(xt_unregister_targets);
123
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200124int xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800125{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200126 u_int8_t af = match->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800127
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200128 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800129 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800130 mutex_unlock(&xt[af].mutex);
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200131 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800132}
133EXPORT_SYMBOL(xt_register_match);
134
135void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800136xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800137{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200138 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800139
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800140 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700141 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800142 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800143}
144EXPORT_SYMBOL(xt_unregister_match);
145
Patrick McHardy52d9c422006-08-22 00:33:45 -0700146int
147xt_register_matches(struct xt_match *match, unsigned int n)
148{
149 unsigned int i;
150 int err = 0;
151
152 for (i = 0; i < n; i++) {
153 err = xt_register_match(&match[i]);
154 if (err)
155 goto err;
156 }
157 return err;
158
159err:
160 if (i > 0)
161 xt_unregister_matches(match, i);
162 return err;
163}
164EXPORT_SYMBOL(xt_register_matches);
165
166void
167xt_unregister_matches(struct xt_match *match, unsigned int n)
168{
Changli Gaof68c5302010-10-04 22:24:12 +0200169 while (n-- > 0)
170 xt_unregister_match(&match[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700171}
172EXPORT_SYMBOL(xt_unregister_matches);
173
Harald Welte2e4e6a12006-01-12 13:30:04 -0800174
175/*
176 * These are weird, but module loading must not be done with mutex
177 * held (since they will register), and we have to have a single
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100178 * function to use.
Harald Welte2e4e6a12006-01-12 13:30:04 -0800179 */
180
181/* Find match, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200182struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800183{
184 struct xt_match *m;
Patrick McHardy42046e22011-03-14 19:11:44 +0100185 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800186
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200187 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800188 list_for_each_entry(m, &xt[af].match, list) {
189 if (strcmp(m->name, name) == 0) {
190 if (m->revision == revision) {
191 if (try_module_get(m->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800192 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800193 return m;
194 }
195 } else
196 err = -EPROTOTYPE; /* Found something. */
197 }
198 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800199 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200200
201 if (af != NFPROTO_UNSPEC)
202 /* Try searching again in the family-independent list */
203 return xt_find_match(NFPROTO_UNSPEC, name, revision);
204
Harald Welte2e4e6a12006-01-12 13:30:04 -0800205 return ERR_PTR(err);
206}
207EXPORT_SYMBOL(xt_find_match);
208
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200209struct xt_match *
210xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
211{
212 struct xt_match *match;
213
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100214 match = xt_find_match(nfproto, name, revision);
215 if (IS_ERR(match)) {
216 request_module("%st_%s", xt_prefix[nfproto], name);
217 match = xt_find_match(nfproto, name, revision);
218 }
219
220 return match;
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200221}
222EXPORT_SYMBOL_GPL(xt_request_find_match);
223
Harald Welte2e4e6a12006-01-12 13:30:04 -0800224/* Find target, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200225struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800226{
227 struct xt_target *t;
Patrick McHardy42046e22011-03-14 19:11:44 +0100228 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800229
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200230 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800231 list_for_each_entry(t, &xt[af].target, list) {
232 if (strcmp(t->name, name) == 0) {
233 if (t->revision == revision) {
234 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800235 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800236 return t;
237 }
238 } else
239 err = -EPROTOTYPE; /* Found something. */
240 }
241 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800242 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200243
244 if (af != NFPROTO_UNSPEC)
245 /* Try searching again in the family-independent list */
246 return xt_find_target(NFPROTO_UNSPEC, name, revision);
247
Harald Welte2e4e6a12006-01-12 13:30:04 -0800248 return ERR_PTR(err);
249}
250EXPORT_SYMBOL(xt_find_target);
251
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200252struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800253{
254 struct xt_target *target;
255
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100256 target = xt_find_target(af, name, revision);
257 if (IS_ERR(target)) {
258 request_module("%st_%s", xt_prefix[af], name);
259 target = xt_find_target(af, name, revision);
260 }
261
262 return target;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800263}
264EXPORT_SYMBOL_GPL(xt_request_find_target);
265
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200266static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800267{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200268 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800269 int have_rev = 0;
270
271 list_for_each_entry(m, &xt[af].match, list) {
272 if (strcmp(m->name, name) == 0) {
273 if (m->revision > *bestp)
274 *bestp = m->revision;
275 if (m->revision == revision)
276 have_rev = 1;
277 }
278 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000279
280 if (af != NFPROTO_UNSPEC && !have_rev)
281 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
282
Harald Welte2e4e6a12006-01-12 13:30:04 -0800283 return have_rev;
284}
285
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200286static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800287{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200288 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800289 int have_rev = 0;
290
291 list_for_each_entry(t, &xt[af].target, list) {
292 if (strcmp(t->name, name) == 0) {
293 if (t->revision > *bestp)
294 *bestp = t->revision;
295 if (t->revision == revision)
296 have_rev = 1;
297 }
298 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000299
300 if (af != NFPROTO_UNSPEC && !have_rev)
301 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
302
Harald Welte2e4e6a12006-01-12 13:30:04 -0800303 return have_rev;
304}
305
306/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200307int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800308 int *err)
309{
310 int have_rev, best = -1;
311
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200312 mutex_lock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800313 if (target == 1)
314 have_rev = target_revfn(af, name, revision, &best);
315 else
316 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800317 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800318
319 /* Nothing at all? Return 0 to try loading module. */
320 if (best == -1) {
321 *err = -ENOENT;
322 return 0;
323 }
324
325 *err = best;
326 if (!have_rev)
327 *err = -EPROTONOSUPPORT;
328 return 1;
329}
330EXPORT_SYMBOL_GPL(xt_find_revision);
331
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000332static char *
333textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
Jan Engelhardt45185362009-04-05 10:43:09 +0200334{
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000335 static const char *const inetbr_names[] = {
Jan Engelhardt45185362009-04-05 10:43:09 +0200336 "PREROUTING", "INPUT", "FORWARD",
337 "OUTPUT", "POSTROUTING", "BROUTING",
338 };
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000339 static const char *const arp_names[] = {
340 "INPUT", "FORWARD", "OUTPUT",
341 };
342 const char *const *names;
343 unsigned int i, max;
Jan Engelhardt45185362009-04-05 10:43:09 +0200344 char *p = buf;
345 bool np = false;
346 int res;
347
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000348 names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
349 max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
350 ARRAY_SIZE(inetbr_names);
Jan Engelhardt45185362009-04-05 10:43:09 +0200351 *p = '\0';
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000352 for (i = 0; i < max; ++i) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200353 if (!(mask & (1 << i)))
354 continue;
355 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
356 if (res > 0) {
357 size -= res;
358 p += res;
359 }
360 np = true;
361 }
362
363 return buf;
364}
365
Jan Engelhardt916a9172008-10-08 11:35:20 +0200366int xt_check_match(struct xt_mtchk_param *par,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200367 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800368{
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100369 int ret;
370
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200371 if (XT_ALIGN(par->match->matchsize) != size &&
372 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200373 /*
374 * ebt_among is exempt from centralized matchsize checking
375 * because it uses a dynamic-size data set.
376 */
Jan Engelhardtb4024052009-06-25 18:32:12 +0200377 pr_err("%s_tables: %s.%u match: invalid size "
378 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200379 xt_prefix[par->family], par->match->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200380 par->match->revision,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200381 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800382 return -EINVAL;
383 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200384 if (par->match->table != NULL &&
385 strcmp(par->match->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200386 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200387 xt_prefix[par->family], par->match->name,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200388 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800389 return -EINVAL;
390 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200391 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200392 char used[64], allow[64];
393
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200394 pr_err("%s_tables: %s match: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200395 "valid from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200396 xt_prefix[par->family], par->match->name,
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000397 textify_hooks(used, sizeof(used), par->hook_mask,
398 par->family),
399 textify_hooks(allow, sizeof(allow), par->match->hooks,
400 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800401 return -EINVAL;
402 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200403 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200404 pr_err("%s_tables: %s match: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200405 xt_prefix[par->family], par->match->name,
406 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800407 return -EINVAL;
408 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100409 if (par->match->checkentry != NULL) {
410 ret = par->match->checkentry(par);
411 if (ret < 0)
412 return ret;
413 else if (ret > 0)
414 /* Flag up potential errors. */
415 return -EIO;
416 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800417 return 0;
418}
419EXPORT_SYMBOL_GPL(xt_check_match);
420
Dmitry Mishin27229712006-04-01 02:25:19 -0800421#ifdef CONFIG_COMPAT
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100422int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800423{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100424 struct xt_af *xp = &xt[af];
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800425
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100426 if (!xp->compat_tab) {
427 if (!xp->number)
428 return -EINVAL;
429 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
430 if (!xp->compat_tab)
431 return -ENOMEM;
432 xp->cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800433 }
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100434
435 if (xp->cur >= xp->number)
436 return -EINVAL;
437
438 if (xp->cur)
439 delta += xp->compat_tab[xp->cur - 1].delta;
440 xp->compat_tab[xp->cur].offset = offset;
441 xp->compat_tab[xp->cur].delta = delta;
442 xp->cur++;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800443 return 0;
444}
445EXPORT_SYMBOL_GPL(xt_compat_add_offset);
446
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200447void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800448{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100449 if (xt[af].compat_tab) {
450 vfree(xt[af].compat_tab);
451 xt[af].compat_tab = NULL;
452 xt[af].number = 0;
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200453 xt[af].cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800454 }
455}
456EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
457
Florian Westphal3e5e5242010-02-15 18:17:10 +0100458int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800459{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100460 struct compat_delta *tmp = xt[af].compat_tab;
461 int mid, left = 0, right = xt[af].cur - 1;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800462
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100463 while (left <= right) {
464 mid = (left + right) >> 1;
465 if (offset > tmp[mid].offset)
466 left = mid + 1;
467 else if (offset < tmp[mid].offset)
468 right = mid - 1;
469 else
470 return mid ? tmp[mid - 1].delta : 0;
471 }
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200472 return left ? tmp[left - 1].delta : 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800473}
474EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
475
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100476void xt_compat_init_offsets(u_int8_t af, unsigned int number)
477{
478 xt[af].number = number;
479 xt[af].cur = 0;
480}
481EXPORT_SYMBOL(xt_compat_init_offsets);
482
Jan Engelhardt5452e422008-04-14 11:15:35 +0200483int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800484{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700485 u_int16_t csize = match->compatsize ? : match->matchsize;
486 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800487}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700488EXPORT_SYMBOL_GPL(xt_compat_match_offset);
489
Patrick McHardy89566952007-12-17 21:46:40 -0800490int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800491 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700492{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200493 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700494 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
495 int pad, off = xt_compat_match_offset(match);
496 u_int16_t msize = cm->u.user.match_size;
497
498 m = *dstptr;
499 memcpy(m, cm, sizeof(*cm));
500 if (match->compat_from_user)
501 match->compat_from_user(m->data, cm->data);
502 else
503 memcpy(m->data, cm->data, msize - sizeof(*cm));
504 pad = XT_ALIGN(match->matchsize) - match->matchsize;
505 if (pad > 0)
506 memset(m->data + match->matchsize, 0, pad);
507
508 msize += off;
509 m->u.user.match_size = msize;
510
511 *size += off;
512 *dstptr += msize;
Patrick McHardy89566952007-12-17 21:46:40 -0800513 return 0;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700514}
515EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
516
Jan Engelhardt739674f2009-06-26 08:23:19 +0200517int xt_compat_match_to_user(const struct xt_entry_match *m,
518 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700519{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200520 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700521 struct compat_xt_entry_match __user *cm = *dstptr;
522 int off = xt_compat_match_offset(match);
523 u_int16_t msize = m->u.user.match_size - off;
524
525 if (copy_to_user(cm, m, sizeof(*cm)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800526 put_user(msize, &cm->u.user.match_size) ||
527 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
528 strlen(m->u.kernel.match->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800529 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700530
531 if (match->compat_to_user) {
532 if (match->compat_to_user((void __user *)cm->data, m->data))
533 return -EFAULT;
534 } else {
535 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
536 return -EFAULT;
537 }
538
539 *size -= off;
540 *dstptr += msize;
541 return 0;
542}
543EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
544#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800545
Jan Engelhardt916a9172008-10-08 11:35:20 +0200546int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200547 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800548{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100549 int ret;
550
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200551 if (XT_ALIGN(par->target->targetsize) != size) {
Jan Engelhardtb4024052009-06-25 18:32:12 +0200552 pr_err("%s_tables: %s.%u target: invalid size "
553 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200554 xt_prefix[par->family], par->target->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200555 par->target->revision,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200556 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800557 return -EINVAL;
558 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200559 if (par->target->table != NULL &&
560 strcmp(par->target->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200561 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200562 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200563 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800564 return -EINVAL;
565 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200566 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200567 char used[64], allow[64];
568
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200569 pr_err("%s_tables: %s target: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200570 "usable from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200571 xt_prefix[par->family], par->target->name,
Jan Engelhardt5b76c492013-01-10 12:30:05 +0000572 textify_hooks(used, sizeof(used), par->hook_mask,
573 par->family),
574 textify_hooks(allow, sizeof(allow), par->target->hooks,
575 par->family));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800576 return -EINVAL;
577 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200578 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200579 pr_err("%s_tables: %s target: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200580 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200581 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800582 return -EINVAL;
583 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100584 if (par->target->checkentry != NULL) {
585 ret = par->target->checkentry(par);
586 if (ret < 0)
587 return ret;
588 else if (ret > 0)
589 /* Flag up potential errors. */
590 return -EIO;
591 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800592 return 0;
593}
594EXPORT_SYMBOL_GPL(xt_check_target);
595
Dmitry Mishin27229712006-04-01 02:25:19 -0800596#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +0200597int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800598{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700599 u_int16_t csize = target->compatsize ? : target->targetsize;
600 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800601}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700602EXPORT_SYMBOL_GPL(xt_compat_target_offset);
603
604void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800605 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700606{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200607 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700608 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
609 int pad, off = xt_compat_target_offset(target);
610 u_int16_t tsize = ct->u.user.target_size;
611
612 t = *dstptr;
613 memcpy(t, ct, sizeof(*ct));
614 if (target->compat_from_user)
615 target->compat_from_user(t->data, ct->data);
616 else
617 memcpy(t->data, ct->data, tsize - sizeof(*ct));
618 pad = XT_ALIGN(target->targetsize) - target->targetsize;
619 if (pad > 0)
620 memset(t->data + target->targetsize, 0, pad);
621
622 tsize += off;
623 t->u.user.target_size = tsize;
624
625 *size += off;
626 *dstptr += tsize;
627}
628EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
629
Jan Engelhardt739674f2009-06-26 08:23:19 +0200630int xt_compat_target_to_user(const struct xt_entry_target *t,
631 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700632{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200633 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700634 struct compat_xt_entry_target __user *ct = *dstptr;
635 int off = xt_compat_target_offset(target);
636 u_int16_t tsize = t->u.user.target_size - off;
637
638 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800639 put_user(tsize, &ct->u.user.target_size) ||
640 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
641 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800642 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700643
644 if (target->compat_to_user) {
645 if (target->compat_to_user((void __user *)ct->data, t->data))
646 return -EFAULT;
647 } else {
648 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
649 return -EFAULT;
650 }
651
652 *size -= off;
653 *dstptr += tsize;
654 return 0;
655}
656EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -0800657#endif
658
Harald Welte2e4e6a12006-01-12 13:30:04 -0800659struct xt_table_info *xt_alloc_table_info(unsigned int size)
660{
Eric Dumazet711bdde2015-06-15 09:57:30 -0700661 struct xt_table_info *info = NULL;
662 size_t sz = sizeof(*info) + size;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800663
664 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
Jan Beulich44813742009-09-21 17:03:05 -0700665 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800666 return NULL;
667
Eric Dumazet711bdde2015-06-15 09:57:30 -0700668 if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
669 info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
670 if (!info) {
671 info = vmalloc(sz);
672 if (!info)
673 return NULL;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800674 }
Eric Dumazet711bdde2015-06-15 09:57:30 -0700675 memset(info, 0, sizeof(*info));
676 info->size = size;
677 return info;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800678}
679EXPORT_SYMBOL(xt_alloc_table_info);
680
681void xt_free_table_info(struct xt_table_info *info)
682{
683 int cpu;
684
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200685 if (info->jumpstack != NULL) {
Eric Dumazetf6b50822014-06-24 02:15:35 -0700686 for_each_possible_cpu(cpu)
687 kvfree(info->jumpstack[cpu]);
688 kvfree(info->jumpstack);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200689 }
690
Eric Dumazet7489aec2010-05-31 16:41:35 +0200691 free_percpu(info->stackptr);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200692
Eric Dumazet711bdde2015-06-15 09:57:30 -0700693 kvfree(info);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800694}
695EXPORT_SYMBOL(xt_free_table_info);
696
697/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200698struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
699 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800700{
701 struct xt_table *t;
702
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200703 mutex_lock(&xt[af].mutex);
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800704 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800705 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
706 return t;
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800707 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800708 return NULL;
709}
710EXPORT_SYMBOL_GPL(xt_find_table_lock);
711
712void xt_table_unlock(struct xt_table *table)
713{
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800714 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800715}
716EXPORT_SYMBOL_GPL(xt_table_unlock);
717
Dmitry Mishin27229712006-04-01 02:25:19 -0800718#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200719void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800720{
721 mutex_lock(&xt[af].compat_mutex);
722}
723EXPORT_SYMBOL_GPL(xt_compat_lock);
724
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200725void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800726{
727 mutex_unlock(&xt[af].compat_mutex);
728}
729EXPORT_SYMBOL_GPL(xt_compat_unlock);
730#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800731
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200732DEFINE_PER_CPU(seqcount_t, xt_recseq);
733EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700734
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200735static int xt_jumpstack_alloc(struct xt_table_info *i)
736{
737 unsigned int size;
738 int cpu;
739
Eric Dumazet7489aec2010-05-31 16:41:35 +0200740 i->stackptr = alloc_percpu(unsigned int);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200741 if (i->stackptr == NULL)
742 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200743
744 size = sizeof(void **) * nr_cpu_ids;
745 if (size > PAGE_SIZE)
Joe Perches3dbd4432011-05-28 10:36:35 -0700746 i->jumpstack = vzalloc(size);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200747 else
Joe Perches3dbd4432011-05-28 10:36:35 -0700748 i->jumpstack = kzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200749 if (i->jumpstack == NULL)
750 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200751
752 i->stacksize *= xt_jumpstack_multiplier;
753 size = sizeof(void *) * i->stacksize;
754 for_each_possible_cpu(cpu) {
755 if (size > PAGE_SIZE)
756 i->jumpstack[cpu] = vmalloc_node(size,
757 cpu_to_node(cpu));
758 else
759 i->jumpstack[cpu] = kmalloc_node(size,
760 GFP_KERNEL, cpu_to_node(cpu));
761 if (i->jumpstack[cpu] == NULL)
762 /*
763 * Freeing will be done later on by the callers. The
764 * chain is: xt_replace_table -> __do_replace ->
765 * do_replace -> xt_free_table_info.
766 */
767 return -ENOMEM;
768 }
769
770 return 0;
771}
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700772
Harald Welte2e4e6a12006-01-12 13:30:04 -0800773struct xt_table_info *
774xt_replace_table(struct xt_table *table,
775 unsigned int num_counters,
776 struct xt_table_info *newinfo,
777 int *error)
778{
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700779 struct xt_table_info *private;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200780 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800781
Jan Engelhardtd97a9e42010-04-21 14:45:51 +0200782 ret = xt_jumpstack_alloc(newinfo);
783 if (ret < 0) {
784 *error = ret;
785 return NULL;
786 }
787
Harald Welte2e4e6a12006-01-12 13:30:04 -0800788 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700789 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800790 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700791
Harald Welte2e4e6a12006-01-12 13:30:04 -0800792 /* Check inside lock: is the old number correct? */
793 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100794 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800795 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700796 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800797 *error = -EAGAIN;
798 return NULL;
799 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800800
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700801 newinfo->initial_entries = private->initial_entries;
Will Deaconb416c142013-10-21 13:14:53 +0100802 /*
803 * Ensure contents of newinfo are visible before assigning to
804 * private.
805 */
806 smp_wmb();
807 table->private = newinfo;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700808
809 /*
810 * Even though table entries have now been swapped, other CPU's
811 * may still be using the old entries. This is okay, because
812 * resynchronization happens because of the locking done
813 * during the get_counters() routine.
814 */
815 local_bh_enable();
816
Thomas Graffbabf312011-01-16 18:12:59 +0100817#ifdef CONFIG_AUDIT
818 if (audit_enabled) {
819 struct audit_buffer *ab;
820
821 ab = audit_log_start(current->audit_context, GFP_KERNEL,
822 AUDIT_NETFILTER_CFG);
823 if (ab) {
824 audit_log_format(ab, "table=%s family=%u entries=%u",
825 table->name, table->af,
826 private->number);
827 audit_log_end(ab);
828 }
829 }
830#endif
831
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700832 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800833}
834EXPORT_SYMBOL_GPL(xt_replace_table);
835
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200836struct xt_table *xt_register_table(struct net *net,
837 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -0800838 struct xt_table_info *bootstrap,
839 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800840{
841 int ret;
842 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200843 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800844
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800845 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200846 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800847 if (!table) {
848 ret = -ENOMEM;
849 goto out;
850 }
851
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200852 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800853 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800854 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700855 if (strcmp(t->name, table->name) == 0) {
856 ret = -EEXIST;
857 goto unlock;
858 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800859 }
860
861 /* Simplifies replace_table code. */
862 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +0100863
Harald Welte2e4e6a12006-01-12 13:30:04 -0800864 if (!xt_replace_table(table, 0, newinfo, &ret))
865 goto unlock;
866
867 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100868 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800869
870 /* save number of initial entries */
871 private->initial_entries = private->number;
872
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800873 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800874 mutex_unlock(&xt[table->af].mutex);
875 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800876
Pablo Neira Ayuso7926dbf2014-07-31 20:38:46 +0200877unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800878 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800879 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800880out:
881 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800882}
883EXPORT_SYMBOL_GPL(xt_register_table);
884
885void *xt_unregister_table(struct xt_table *table)
886{
887 struct xt_table_info *private;
888
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800889 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800890 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700891 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800892 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800893 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800894
895 return private;
896}
897EXPORT_SYMBOL_GPL(xt_unregister_table);
898
899#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800900struct xt_names_priv {
901 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200902 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800903};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800904static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800905{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800906 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900907 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200908 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800909
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800910 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800911 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800912}
913
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800914static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800915{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800916 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900917 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200918 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800919
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800920 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800921}
922
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800923static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800924{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800925 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200926 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800927
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800928 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800929}
930
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800931static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800932{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800933 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800934
Joe Perches861fb102015-05-12 18:28:23 -0700935 if (*table->name)
Steven Rostedt (Red Hat)e71456a2014-10-27 17:43:45 -0400936 seq_printf(seq, "%s\n", table->name);
Joe Perches861fb102015-05-12 18:28:23 -0700937 return 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800938}
939
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800940static const struct seq_operations xt_table_seq_ops = {
941 .start = xt_table_seq_start,
942 .next = xt_table_seq_next,
943 .stop = xt_table_seq_stop,
944 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800945};
946
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800947static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800948{
949 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800950 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800951
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800952 ret = seq_open_net(inode, file, &xt_table_seq_ops,
953 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800954 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800955 priv = ((struct seq_file *)file->private_data)->private;
Al Virod9dda782013-03-31 18:16:14 -0400956 priv->af = (unsigned long)PDE_DATA(inode);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800957 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800958 return ret;
959}
960
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800961static const struct file_operations xt_table_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800962 .owner = THIS_MODULE,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800963 .open = xt_table_open,
964 .read = seq_read,
965 .llseek = seq_lseek,
Pavel Emelyanov0e93bb92008-04-29 03:15:35 -0700966 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800967};
968
Jan Engelhardteb132202009-02-18 16:42:19 +0100969/*
970 * Traverse state for ip{,6}_{tables,matches} for helping crossing
971 * the multi-AF mutexes.
972 */
973struct nf_mttg_trav {
974 struct list_head *head, *curr;
975 uint8_t class, nfproto;
976};
977
978enum {
979 MTTG_TRAV_INIT,
980 MTTG_TRAV_NFP_UNSPEC,
981 MTTG_TRAV_NFP_SPEC,
982 MTTG_TRAV_DONE,
983};
984
985static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
986 bool is_target)
987{
988 static const uint8_t next_class[] = {
989 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
990 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
991 };
992 struct nf_mttg_trav *trav = seq->private;
993
994 switch (trav->class) {
995 case MTTG_TRAV_INIT:
996 trav->class = MTTG_TRAV_NFP_UNSPEC;
997 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
998 trav->head = trav->curr = is_target ?
999 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1000 break;
1001 case MTTG_TRAV_NFP_UNSPEC:
1002 trav->curr = trav->curr->next;
1003 if (trav->curr != trav->head)
1004 break;
1005 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1006 mutex_lock(&xt[trav->nfproto].mutex);
1007 trav->head = trav->curr = is_target ?
1008 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1009 trav->class = next_class[trav->class];
1010 break;
1011 case MTTG_TRAV_NFP_SPEC:
1012 trav->curr = trav->curr->next;
1013 if (trav->curr != trav->head)
1014 break;
1015 /* fallthru, _stop will unlock */
1016 default:
1017 return NULL;
1018 }
1019
1020 if (ppos != NULL)
1021 ++*ppos;
1022 return trav;
1023}
1024
1025static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1026 bool is_target)
1027{
1028 struct nf_mttg_trav *trav = seq->private;
1029 unsigned int j;
1030
1031 trav->class = MTTG_TRAV_INIT;
1032 for (j = 0; j < *pos; ++j)
1033 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1034 return NULL;
1035 return trav;
1036}
1037
1038static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1039{
1040 struct nf_mttg_trav *trav = seq->private;
1041
1042 switch (trav->class) {
1043 case MTTG_TRAV_NFP_UNSPEC:
1044 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1045 break;
1046 case MTTG_TRAV_NFP_SPEC:
1047 mutex_unlock(&xt[trav->nfproto].mutex);
1048 break;
1049 }
1050}
1051
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001052static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1053{
Jan Engelhardteb132202009-02-18 16:42:19 +01001054 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001055}
1056
Jan Engelhardteb132202009-02-18 16:42:19 +01001057static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001058{
Jan Engelhardteb132202009-02-18 16:42:19 +01001059 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001060}
1061
1062static int xt_match_seq_show(struct seq_file *seq, void *v)
1063{
Jan Engelhardteb132202009-02-18 16:42:19 +01001064 const struct nf_mttg_trav *trav = seq->private;
1065 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001066
Jan Engelhardteb132202009-02-18 16:42:19 +01001067 switch (trav->class) {
1068 case MTTG_TRAV_NFP_UNSPEC:
1069 case MTTG_TRAV_NFP_SPEC:
1070 if (trav->curr == trav->head)
1071 return 0;
1072 match = list_entry(trav->curr, struct xt_match, list);
Joe Perches861fb102015-05-12 18:28:23 -07001073 if (*match->name)
1074 seq_printf(seq, "%s\n", match->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001075 }
1076 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001077}
1078
1079static const struct seq_operations xt_match_seq_ops = {
1080 .start = xt_match_seq_start,
1081 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001082 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001083 .show = xt_match_seq_show,
1084};
1085
1086static int xt_match_open(struct inode *inode, struct file *file)
1087{
Jan Engelhardteb132202009-02-18 16:42:19 +01001088 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001089 trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
1090 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001091 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001092
Al Virod9dda782013-03-31 18:16:14 -04001093 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001094 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001095}
1096
1097static const struct file_operations xt_match_ops = {
1098 .owner = THIS_MODULE,
1099 .open = xt_match_open,
1100 .read = seq_read,
1101 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001102 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001103};
1104
1105static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1106{
Jan Engelhardteb132202009-02-18 16:42:19 +01001107 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001108}
1109
Jan Engelhardteb132202009-02-18 16:42:19 +01001110static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001111{
Jan Engelhardteb132202009-02-18 16:42:19 +01001112 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001113}
1114
1115static int xt_target_seq_show(struct seq_file *seq, void *v)
1116{
Jan Engelhardteb132202009-02-18 16:42:19 +01001117 const struct nf_mttg_trav *trav = seq->private;
1118 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001119
Jan Engelhardteb132202009-02-18 16:42:19 +01001120 switch (trav->class) {
1121 case MTTG_TRAV_NFP_UNSPEC:
1122 case MTTG_TRAV_NFP_SPEC:
1123 if (trav->curr == trav->head)
1124 return 0;
1125 target = list_entry(trav->curr, struct xt_target, list);
Joe Perches861fb102015-05-12 18:28:23 -07001126 if (*target->name)
1127 seq_printf(seq, "%s\n", target->name);
Jan Engelhardteb132202009-02-18 16:42:19 +01001128 }
1129 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001130}
1131
1132static const struct seq_operations xt_target_seq_ops = {
1133 .start = xt_target_seq_start,
1134 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001135 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001136 .show = xt_target_seq_show,
1137};
1138
1139static int xt_target_open(struct inode *inode, struct file *file)
1140{
Jan Engelhardteb132202009-02-18 16:42:19 +01001141 struct nf_mttg_trav *trav;
Rob Jones772476d2014-09-19 11:27:51 +01001142 trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
1143 if (!trav)
Jan Engelhardteb132202009-02-18 16:42:19 +01001144 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001145
Al Virod9dda782013-03-31 18:16:14 -04001146 trav->nfproto = (unsigned long)PDE_DATA(inode);
Jan Engelhardteb132202009-02-18 16:42:19 +01001147 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001148}
1149
1150static const struct file_operations xt_target_ops = {
1151 .owner = THIS_MODULE,
1152 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001153 .read = seq_read,
1154 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001155 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001156};
1157
1158#define FORMAT_TABLES "_tables_names"
1159#define FORMAT_MATCHES "_tables_matches"
1160#define FORMAT_TARGETS "_tables_targets"
1161
1162#endif /* CONFIG_PROC_FS */
1163
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001164/**
1165 * xt_hook_link - set up hooks for a new table
1166 * @table: table with metadata needed to set up hooks
1167 * @fn: Hook function
1168 *
1169 * This function will take care of creating and registering the necessary
1170 * Netfilter hooks for XT tables.
1171 */
1172struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1173{
1174 unsigned int hook_mask = table->valid_hooks;
1175 uint8_t i, num_hooks = hweight32(hook_mask);
1176 uint8_t hooknum;
1177 struct nf_hook_ops *ops;
1178 int ret;
1179
1180 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1181 if (ops == NULL)
1182 return ERR_PTR(-ENOMEM);
1183
1184 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1185 hook_mask >>= 1, ++hooknum) {
1186 if (!(hook_mask & 1))
1187 continue;
1188 ops[i].hook = fn;
1189 ops[i].owner = table->me;
1190 ops[i].pf = table->af;
1191 ops[i].hooknum = hooknum;
1192 ops[i].priority = table->priority;
1193 ++i;
1194 }
1195
1196 ret = nf_register_hooks(ops, num_hooks);
1197 if (ret < 0) {
1198 kfree(ops);
1199 return ERR_PTR(ret);
1200 }
1201
1202 return ops;
1203}
1204EXPORT_SYMBOL_GPL(xt_hook_link);
1205
1206/**
1207 * xt_hook_unlink - remove hooks for a table
1208 * @ops: nf_hook_ops array as returned by nf_hook_link
1209 * @hook_mask: the very same mask that was passed to nf_hook_link
1210 */
1211void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1212{
1213 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1214 kfree(ops);
1215}
1216EXPORT_SYMBOL_GPL(xt_hook_unlink);
1217
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001218int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001219{
1220#ifdef CONFIG_PROC_FS
1221 char buf[XT_FUNCTION_MAXNAMELEN];
1222 struct proc_dir_entry *proc;
1223#endif
1224
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001225 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001226 return -EINVAL;
1227
1228
1229#ifdef CONFIG_PROC_FS
Tobias Klauserce18afe2007-03-14 16:36:16 -07001230 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001231 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001232 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1233 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001234 if (!proc)
1235 goto out;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001236
Tobias Klauserce18afe2007-03-14 16:36:16 -07001237 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001238 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001239 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1240 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001241 if (!proc)
1242 goto out_remove_tables;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001243
Tobias Klauserce18afe2007-03-14 16:36:16 -07001244 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001245 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001246 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1247 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001248 if (!proc)
1249 goto out_remove_matches;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001250#endif
1251
1252 return 0;
1253
1254#ifdef CONFIG_PROC_FS
1255out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001256 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001257 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001258 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001259
1260out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001261 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001262 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001263 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001264out:
1265 return -1;
1266#endif
1267}
1268EXPORT_SYMBOL_GPL(xt_proto_init);
1269
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001270void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001271{
1272#ifdef CONFIG_PROC_FS
1273 char buf[XT_FUNCTION_MAXNAMELEN];
1274
Tobias Klauserce18afe2007-03-14 16:36:16 -07001275 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001276 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001277 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001278
Tobias Klauserce18afe2007-03-14 16:36:16 -07001279 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001280 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001281 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001282
Tobias Klauserce18afe2007-03-14 16:36:16 -07001283 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001284 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Gao fengece31ff2013-02-18 01:34:56 +00001285 remove_proc_entry(buf, net->proc_net);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001286#endif /*CONFIG_PROC_FS*/
1287}
1288EXPORT_SYMBOL_GPL(xt_proto_fini);
1289
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001290static int __net_init xt_net_init(struct net *net)
1291{
1292 int i;
1293
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001294 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001295 INIT_LIST_HEAD(&net->xt.tables[i]);
1296 return 0;
1297}
1298
1299static struct pernet_operations xt_net_ops = {
1300 .init = xt_net_init,
1301};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001302
1303static int __init xt_init(void)
1304{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001305 unsigned int i;
1306 int rv;
1307
1308 for_each_possible_cpu(i) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001309 seqcount_init(&per_cpu(xt_recseq, i));
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001310 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001311
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001312 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001313 if (!xt)
1314 return -ENOMEM;
1315
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001316 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001317 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001318#ifdef CONFIG_COMPAT
1319 mutex_init(&xt[i].compat_mutex);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001320 xt[i].compat_tab = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001321#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001322 INIT_LIST_HEAD(&xt[i].target);
1323 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001324 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001325 rv = register_pernet_subsys(&xt_net_ops);
1326 if (rv < 0)
1327 kfree(xt);
1328 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001329}
1330
1331static void __exit xt_fini(void)
1332{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001333 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001334 kfree(xt);
1335}
1336
1337module_init(xt_init);
1338module_exit(xt_fini);
1339