blob: 8d987c3573fd4ca9e1708e188d487e53ebd7dfd7 [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>
5 *
6 * Based on existing ip_tables code which is
7 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
8 * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Harald Welte2e4e6a12006-01-12 13:30:04 -080016#include <linux/kernel.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040017#include <linux/module.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080018#include <linux/socket.h>
19#include <linux/net.h>
20#include <linux/proc_fs.h>
21#include <linux/seq_file.h>
22#include <linux/string.h>
23#include <linux/vmalloc.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080024#include <linux/mutex.h>
Al Virod7fe0f22006-12-03 23:15:30 -050025#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Thomas Graffbabf312011-01-16 18:12:59 +010027#include <linux/audit.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020028#include <net/net_namespace.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080029
30#include <linux/netfilter/x_tables.h>
31#include <linux/netfilter_arp.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020032#include <linux/netfilter_ipv4/ip_tables.h>
33#include <linux/netfilter_ipv6/ip6_tables.h>
34#include <linux/netfilter_arp/arp_tables.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080035
Harald Welte2e4e6a12006-01-12 13:30:04 -080036MODULE_LICENSE("GPL");
37MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt043ef462008-10-08 11:35:15 +020038MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080039
40#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
41
Patrick McHardyb386d9f2007-12-17 21:47:48 -080042struct compat_delta {
Eric Dumazet255d0dc2010-12-18 18:35:15 +010043 unsigned int offset; /* offset in kernel */
44 int delta; /* delta in 32bit user land */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080045};
46
Harald Welte2e4e6a12006-01-12 13:30:04 -080047struct xt_af {
Ingo Molnar9e19bb62006-03-25 01:41:10 -080048 struct mutex mutex;
Harald Welte2e4e6a12006-01-12 13:30:04 -080049 struct list_head match;
50 struct list_head target;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080051#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080052 struct mutex compat_mutex;
Eric Dumazet255d0dc2010-12-18 18:35:15 +010053 struct compat_delta *compat_tab;
54 unsigned int number; /* number of slots in compat_tab[] */
55 unsigned int cur; /* number of used slots in compat_tab[] */
Patrick McHardyb386d9f2007-12-17 21:47:48 -080056#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080057};
58
59static struct xt_af *xt;
60
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020061static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
62 [NFPROTO_UNSPEC] = "x",
63 [NFPROTO_IPV4] = "ip",
64 [NFPROTO_ARP] = "arp",
65 [NFPROTO_BRIDGE] = "eb",
66 [NFPROTO_IPV6] = "ip6",
Patrick McHardy37f9f732006-03-20 17:59:06 -080067};
68
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +020069/* Allow this many total (re)entries. */
70static const unsigned int xt_jumpstack_multiplier = 2;
71
Harald Welte2e4e6a12006-01-12 13:30:04 -080072/* Registration hooks for targets. */
73int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080074xt_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;
77 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -080078
Ingo Molnar9e19bb62006-03-25 01:41:10 -080079 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080080 if (ret != 0)
81 return ret;
82 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080083 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080084 return ret;
85}
86EXPORT_SYMBOL(xt_register_target);
87
88void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080089xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080090{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020091 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080092
Ingo Molnar9e19bb62006-03-25 01:41:10 -080093 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070094 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080095 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080096}
97EXPORT_SYMBOL(xt_unregister_target);
98
99int
Patrick McHardy52d9c422006-08-22 00:33:45 -0700100xt_register_targets(struct xt_target *target, unsigned int n)
101{
102 unsigned int i;
103 int err = 0;
104
105 for (i = 0; i < n; i++) {
106 err = xt_register_target(&target[i]);
107 if (err)
108 goto err;
109 }
110 return err;
111
112err:
113 if (i > 0)
114 xt_unregister_targets(target, i);
115 return err;
116}
117EXPORT_SYMBOL(xt_register_targets);
118
119void
120xt_unregister_targets(struct xt_target *target, unsigned int n)
121{
Changli Gaof68c5302010-10-04 22:24:12 +0200122 while (n-- > 0)
123 xt_unregister_target(&target[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700124}
125EXPORT_SYMBOL(xt_unregister_targets);
126
127int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800128xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800129{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200130 u_int8_t af = match->family;
131 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800132
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800133 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800134 if (ret != 0)
135 return ret;
136
137 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800138 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800139
140 return ret;
141}
142EXPORT_SYMBOL(xt_register_match);
143
144void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800145xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800146{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200147 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800148
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800149 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700150 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800151 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800152}
153EXPORT_SYMBOL(xt_unregister_match);
154
Patrick McHardy52d9c422006-08-22 00:33:45 -0700155int
156xt_register_matches(struct xt_match *match, unsigned int n)
157{
158 unsigned int i;
159 int err = 0;
160
161 for (i = 0; i < n; i++) {
162 err = xt_register_match(&match[i]);
163 if (err)
164 goto err;
165 }
166 return err;
167
168err:
169 if (i > 0)
170 xt_unregister_matches(match, i);
171 return err;
172}
173EXPORT_SYMBOL(xt_register_matches);
174
175void
176xt_unregister_matches(struct xt_match *match, unsigned int n)
177{
Changli Gaof68c5302010-10-04 22:24:12 +0200178 while (n-- > 0)
179 xt_unregister_match(&match[n]);
Patrick McHardy52d9c422006-08-22 00:33:45 -0700180}
181EXPORT_SYMBOL(xt_unregister_matches);
182
Harald Welte2e4e6a12006-01-12 13:30:04 -0800183
184/*
185 * These are weird, but module loading must not be done with mutex
186 * held (since they will register), and we have to have a single
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100187 * function to use.
Harald Welte2e4e6a12006-01-12 13:30:04 -0800188 */
189
190/* Find match, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200191struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800192{
193 struct xt_match *m;
Patrick McHardy42046e22011-03-14 19:11:44 +0100194 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800195
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800196 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800197 return ERR_PTR(-EINTR);
198
199 list_for_each_entry(m, &xt[af].match, list) {
200 if (strcmp(m->name, name) == 0) {
201 if (m->revision == revision) {
202 if (try_module_get(m->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800203 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800204 return m;
205 }
206 } else
207 err = -EPROTOTYPE; /* Found something. */
208 }
209 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800210 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200211
212 if (af != NFPROTO_UNSPEC)
213 /* Try searching again in the family-independent list */
214 return xt_find_match(NFPROTO_UNSPEC, name, revision);
215
Harald Welte2e4e6a12006-01-12 13:30:04 -0800216 return ERR_PTR(err);
217}
218EXPORT_SYMBOL(xt_find_match);
219
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200220struct xt_match *
221xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
222{
223 struct xt_match *match;
224
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100225 match = xt_find_match(nfproto, name, revision);
226 if (IS_ERR(match)) {
227 request_module("%st_%s", xt_prefix[nfproto], name);
228 match = xt_find_match(nfproto, name, revision);
229 }
230
231 return match;
Jan Engelhardtfd0ec0e2009-07-10 19:27:47 +0200232}
233EXPORT_SYMBOL_GPL(xt_request_find_match);
234
Harald Welte2e4e6a12006-01-12 13:30:04 -0800235/* Find target, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200236struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800237{
238 struct xt_target *t;
Patrick McHardy42046e22011-03-14 19:11:44 +0100239 int err = -ENOENT;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800240
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800241 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800242 return ERR_PTR(-EINTR);
243
244 list_for_each_entry(t, &xt[af].target, list) {
245 if (strcmp(t->name, name) == 0) {
246 if (t->revision == revision) {
247 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800248 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800249 return t;
250 }
251 } else
252 err = -EPROTOTYPE; /* Found something. */
253 }
254 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800255 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200256
257 if (af != NFPROTO_UNSPEC)
258 /* Try searching again in the family-independent list */
259 return xt_find_target(NFPROTO_UNSPEC, name, revision);
260
Harald Welte2e4e6a12006-01-12 13:30:04 -0800261 return ERR_PTR(err);
262}
263EXPORT_SYMBOL(xt_find_target);
264
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200265struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800266{
267 struct xt_target *target;
268
Stephen Hemmingeradb00ae2011-03-09 14:14:26 +0100269 target = xt_find_target(af, name, revision);
270 if (IS_ERR(target)) {
271 request_module("%st_%s", xt_prefix[af], name);
272 target = xt_find_target(af, name, revision);
273 }
274
275 return target;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800276}
277EXPORT_SYMBOL_GPL(xt_request_find_target);
278
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200279static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800280{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200281 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800282 int have_rev = 0;
283
284 list_for_each_entry(m, &xt[af].match, list) {
285 if (strcmp(m->name, name) == 0) {
286 if (m->revision > *bestp)
287 *bestp = m->revision;
288 if (m->revision == revision)
289 have_rev = 1;
290 }
291 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000292
293 if (af != NFPROTO_UNSPEC && !have_rev)
294 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
295
Harald Welte2e4e6a12006-01-12 13:30:04 -0800296 return have_rev;
297}
298
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200299static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800300{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200301 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800302 int have_rev = 0;
303
304 list_for_each_entry(t, &xt[af].target, list) {
305 if (strcmp(t->name, name) == 0) {
306 if (t->revision > *bestp)
307 *bestp = t->revision;
308 if (t->revision == revision)
309 have_rev = 1;
310 }
311 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000312
313 if (af != NFPROTO_UNSPEC && !have_rev)
314 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
315
Harald Welte2e4e6a12006-01-12 13:30:04 -0800316 return have_rev;
317}
318
319/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200320int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800321 int *err)
322{
323 int have_rev, best = -1;
324
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800325 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800326 *err = -EINTR;
327 return 1;
328 }
329 if (target == 1)
330 have_rev = target_revfn(af, name, revision, &best);
331 else
332 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800333 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800334
335 /* Nothing at all? Return 0 to try loading module. */
336 if (best == -1) {
337 *err = -ENOENT;
338 return 0;
339 }
340
341 *err = best;
342 if (!have_rev)
343 *err = -EPROTONOSUPPORT;
344 return 1;
345}
346EXPORT_SYMBOL_GPL(xt_find_revision);
347
Jan Engelhardt45185362009-04-05 10:43:09 +0200348static char *textify_hooks(char *buf, size_t size, unsigned int mask)
349{
350 static const char *const names[] = {
351 "PREROUTING", "INPUT", "FORWARD",
352 "OUTPUT", "POSTROUTING", "BROUTING",
353 };
354 unsigned int i;
355 char *p = buf;
356 bool np = false;
357 int res;
358
359 *p = '\0';
360 for (i = 0; i < ARRAY_SIZE(names); ++i) {
361 if (!(mask & (1 << i)))
362 continue;
363 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
364 if (res > 0) {
365 size -= res;
366 p += res;
367 }
368 np = true;
369 }
370
371 return buf;
372}
373
Jan Engelhardt916a9172008-10-08 11:35:20 +0200374int xt_check_match(struct xt_mtchk_param *par,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200375 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800376{
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100377 int ret;
378
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200379 if (XT_ALIGN(par->match->matchsize) != size &&
380 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200381 /*
382 * ebt_among is exempt from centralized matchsize checking
383 * because it uses a dynamic-size data set.
384 */
Jan Engelhardtb4024052009-06-25 18:32:12 +0200385 pr_err("%s_tables: %s.%u match: invalid size "
386 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200387 xt_prefix[par->family], par->match->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200388 par->match->revision,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200389 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800390 return -EINVAL;
391 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200392 if (par->match->table != NULL &&
393 strcmp(par->match->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200394 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200395 xt_prefix[par->family], par->match->name,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200396 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800397 return -EINVAL;
398 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200399 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200400 char used[64], allow[64];
401
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200402 pr_err("%s_tables: %s match: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200403 "valid from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200404 xt_prefix[par->family], par->match->name,
Jan Engelhardt45185362009-04-05 10:43:09 +0200405 textify_hooks(used, sizeof(used), par->hook_mask),
406 textify_hooks(allow, sizeof(allow), par->match->hooks));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800407 return -EINVAL;
408 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200409 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200410 pr_err("%s_tables: %s match: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200411 xt_prefix[par->family], par->match->name,
412 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800413 return -EINVAL;
414 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100415 if (par->match->checkentry != NULL) {
416 ret = par->match->checkentry(par);
417 if (ret < 0)
418 return ret;
419 else if (ret > 0)
420 /* Flag up potential errors. */
421 return -EIO;
422 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800423 return 0;
424}
425EXPORT_SYMBOL_GPL(xt_check_match);
426
Dmitry Mishin27229712006-04-01 02:25:19 -0800427#ifdef CONFIG_COMPAT
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100428int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800429{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100430 struct xt_af *xp = &xt[af];
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800431
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100432 if (!xp->compat_tab) {
433 if (!xp->number)
434 return -EINVAL;
435 xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
436 if (!xp->compat_tab)
437 return -ENOMEM;
438 xp->cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800439 }
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100440
441 if (xp->cur >= xp->number)
442 return -EINVAL;
443
444 if (xp->cur)
445 delta += xp->compat_tab[xp->cur - 1].delta;
446 xp->compat_tab[xp->cur].offset = offset;
447 xp->compat_tab[xp->cur].delta = delta;
448 xp->cur++;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800449 return 0;
450}
451EXPORT_SYMBOL_GPL(xt_compat_add_offset);
452
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200453void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800454{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100455 if (xt[af].compat_tab) {
456 vfree(xt[af].compat_tab);
457 xt[af].compat_tab = NULL;
458 xt[af].number = 0;
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200459 xt[af].cur = 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800460 }
461}
462EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
463
Florian Westphal3e5e5242010-02-15 18:17:10 +0100464int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800465{
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100466 struct compat_delta *tmp = xt[af].compat_tab;
467 int mid, left = 0, right = xt[af].cur - 1;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800468
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100469 while (left <= right) {
470 mid = (left + right) >> 1;
471 if (offset > tmp[mid].offset)
472 left = mid + 1;
473 else if (offset < tmp[mid].offset)
474 right = mid - 1;
475 else
476 return mid ? tmp[mid - 1].delta : 0;
477 }
Eric Dumazet5a6351e2011-04-21 10:57:21 +0200478 return left ? tmp[left - 1].delta : 0;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800479}
480EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
481
Eric Dumazet255d0dc2010-12-18 18:35:15 +0100482void xt_compat_init_offsets(u_int8_t af, unsigned int number)
483{
484 xt[af].number = number;
485 xt[af].cur = 0;
486}
487EXPORT_SYMBOL(xt_compat_init_offsets);
488
Jan Engelhardt5452e422008-04-14 11:15:35 +0200489int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800490{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700491 u_int16_t csize = match->compatsize ? : match->matchsize;
492 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800493}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700494EXPORT_SYMBOL_GPL(xt_compat_match_offset);
495
Patrick McHardy89566952007-12-17 21:46:40 -0800496int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800497 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700498{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200499 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700500 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
501 int pad, off = xt_compat_match_offset(match);
502 u_int16_t msize = cm->u.user.match_size;
503
504 m = *dstptr;
505 memcpy(m, cm, sizeof(*cm));
506 if (match->compat_from_user)
507 match->compat_from_user(m->data, cm->data);
508 else
509 memcpy(m->data, cm->data, msize - sizeof(*cm));
510 pad = XT_ALIGN(match->matchsize) - match->matchsize;
511 if (pad > 0)
512 memset(m->data + match->matchsize, 0, pad);
513
514 msize += off;
515 m->u.user.match_size = msize;
516
517 *size += off;
518 *dstptr += msize;
Patrick McHardy89566952007-12-17 21:46:40 -0800519 return 0;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700520}
521EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
522
Jan Engelhardt739674f2009-06-26 08:23:19 +0200523int xt_compat_match_to_user(const struct xt_entry_match *m,
524 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700525{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200526 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700527 struct compat_xt_entry_match __user *cm = *dstptr;
528 int off = xt_compat_match_offset(match);
529 u_int16_t msize = m->u.user.match_size - off;
530
531 if (copy_to_user(cm, m, sizeof(*cm)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800532 put_user(msize, &cm->u.user.match_size) ||
533 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
534 strlen(m->u.kernel.match->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800535 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700536
537 if (match->compat_to_user) {
538 if (match->compat_to_user((void __user *)cm->data, m->data))
539 return -EFAULT;
540 } else {
541 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
542 return -EFAULT;
543 }
544
545 *size -= off;
546 *dstptr += msize;
547 return 0;
548}
549EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
550#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800551
Jan Engelhardt916a9172008-10-08 11:35:20 +0200552int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200553 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800554{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100555 int ret;
556
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200557 if (XT_ALIGN(par->target->targetsize) != size) {
Jan Engelhardtb4024052009-06-25 18:32:12 +0200558 pr_err("%s_tables: %s.%u target: invalid size "
559 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200560 xt_prefix[par->family], par->target->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200561 par->target->revision,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200562 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800563 return -EINVAL;
564 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200565 if (par->target->table != NULL &&
566 strcmp(par->target->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200567 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200568 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200569 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800570 return -EINVAL;
571 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200572 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200573 char used[64], allow[64];
574
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200575 pr_err("%s_tables: %s target: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200576 "usable from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200577 xt_prefix[par->family], par->target->name,
Jan Engelhardt45185362009-04-05 10:43:09 +0200578 textify_hooks(used, sizeof(used), par->hook_mask),
579 textify_hooks(allow, sizeof(allow), par->target->hooks));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800580 return -EINVAL;
581 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200582 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200583 pr_err("%s_tables: %s target: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200584 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200585 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800586 return -EINVAL;
587 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100588 if (par->target->checkentry != NULL) {
589 ret = par->target->checkentry(par);
590 if (ret < 0)
591 return ret;
592 else if (ret > 0)
593 /* Flag up potential errors. */
594 return -EIO;
595 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800596 return 0;
597}
598EXPORT_SYMBOL_GPL(xt_check_target);
599
Dmitry Mishin27229712006-04-01 02:25:19 -0800600#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +0200601int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800602{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700603 u_int16_t csize = target->compatsize ? : target->targetsize;
604 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800605}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700606EXPORT_SYMBOL_GPL(xt_compat_target_offset);
607
608void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800609 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700610{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200611 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700612 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
613 int pad, off = xt_compat_target_offset(target);
614 u_int16_t tsize = ct->u.user.target_size;
615
616 t = *dstptr;
617 memcpy(t, ct, sizeof(*ct));
618 if (target->compat_from_user)
619 target->compat_from_user(t->data, ct->data);
620 else
621 memcpy(t->data, ct->data, tsize - sizeof(*ct));
622 pad = XT_ALIGN(target->targetsize) - target->targetsize;
623 if (pad > 0)
624 memset(t->data + target->targetsize, 0, pad);
625
626 tsize += off;
627 t->u.user.target_size = tsize;
628
629 *size += off;
630 *dstptr += tsize;
631}
632EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
633
Jan Engelhardt739674f2009-06-26 08:23:19 +0200634int xt_compat_target_to_user(const struct xt_entry_target *t,
635 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700636{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200637 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700638 struct compat_xt_entry_target __user *ct = *dstptr;
639 int off = xt_compat_target_offset(target);
640 u_int16_t tsize = t->u.user.target_size - off;
641
642 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800643 put_user(tsize, &ct->u.user.target_size) ||
644 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
645 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800646 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700647
648 if (target->compat_to_user) {
649 if (target->compat_to_user((void __user *)ct->data, t->data))
650 return -EFAULT;
651 } else {
652 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
653 return -EFAULT;
654 }
655
656 *size -= off;
657 *dstptr += tsize;
658 return 0;
659}
660EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -0800661#endif
662
Harald Welte2e4e6a12006-01-12 13:30:04 -0800663struct xt_table_info *xt_alloc_table_info(unsigned int size)
664{
665 struct xt_table_info *newinfo;
666 int cpu;
667
668 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
Jan Beulich44813742009-09-21 17:03:05 -0700669 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800670 return NULL;
671
Eric Dumazet259d4e42007-12-04 23:24:56 -0800672 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800673 if (!newinfo)
674 return NULL;
675
676 newinfo->size = size;
677
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700678 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800679 if (size <= PAGE_SIZE)
680 newinfo->entries[cpu] = kmalloc_node(size,
681 GFP_KERNEL,
682 cpu_to_node(cpu));
683 else
684 newinfo->entries[cpu] = vmalloc_node(size,
685 cpu_to_node(cpu));
686
687 if (newinfo->entries[cpu] == NULL) {
688 xt_free_table_info(newinfo);
689 return NULL;
690 }
691 }
692
693 return newinfo;
694}
695EXPORT_SYMBOL(xt_alloc_table_info);
696
697void xt_free_table_info(struct xt_table_info *info)
698{
699 int cpu;
700
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700701 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800702 if (info->size <= PAGE_SIZE)
703 kfree(info->entries[cpu]);
704 else
705 vfree(info->entries[cpu]);
706 }
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200707
708 if (info->jumpstack != NULL) {
709 if (sizeof(void *) * info->stacksize > PAGE_SIZE) {
710 for_each_possible_cpu(cpu)
711 vfree(info->jumpstack[cpu]);
712 } else {
713 for_each_possible_cpu(cpu)
714 kfree(info->jumpstack[cpu]);
715 }
716 }
717
718 if (sizeof(void **) * nr_cpu_ids > PAGE_SIZE)
719 vfree(info->jumpstack);
720 else
721 kfree(info->jumpstack);
Eric Dumazet7489aec2010-05-31 16:41:35 +0200722
723 free_percpu(info->stackptr);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200724
Harald Welte2e4e6a12006-01-12 13:30:04 -0800725 kfree(info);
726}
727EXPORT_SYMBOL(xt_free_table_info);
728
729/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200730struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
731 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800732{
733 struct xt_table *t;
734
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800735 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800736 return ERR_PTR(-EINTR);
737
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800738 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800739 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
740 return t;
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800741 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800742 return NULL;
743}
744EXPORT_SYMBOL_GPL(xt_find_table_lock);
745
746void xt_table_unlock(struct xt_table *table)
747{
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800748 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800749}
750EXPORT_SYMBOL_GPL(xt_table_unlock);
751
Dmitry Mishin27229712006-04-01 02:25:19 -0800752#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200753void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800754{
755 mutex_lock(&xt[af].compat_mutex);
756}
757EXPORT_SYMBOL_GPL(xt_compat_lock);
758
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200759void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800760{
761 mutex_unlock(&xt[af].compat_mutex);
762}
763EXPORT_SYMBOL_GPL(xt_compat_unlock);
764#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800765
Eric Dumazet7f5c6d42011-04-04 17:04:03 +0200766DEFINE_PER_CPU(seqcount_t, xt_recseq);
767EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700768
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200769static int xt_jumpstack_alloc(struct xt_table_info *i)
770{
771 unsigned int size;
772 int cpu;
773
Eric Dumazet7489aec2010-05-31 16:41:35 +0200774 i->stackptr = alloc_percpu(unsigned int);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200775 if (i->stackptr == NULL)
776 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200777
778 size = sizeof(void **) * nr_cpu_ids;
779 if (size > PAGE_SIZE)
Joe Perches3dbd4432011-05-28 10:36:35 -0700780 i->jumpstack = vzalloc(size);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200781 else
Joe Perches3dbd4432011-05-28 10:36:35 -0700782 i->jumpstack = kzalloc(size, GFP_KERNEL);
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200783 if (i->jumpstack == NULL)
784 return -ENOMEM;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200785
786 i->stacksize *= xt_jumpstack_multiplier;
787 size = sizeof(void *) * i->stacksize;
788 for_each_possible_cpu(cpu) {
789 if (size > PAGE_SIZE)
790 i->jumpstack[cpu] = vmalloc_node(size,
791 cpu_to_node(cpu));
792 else
793 i->jumpstack[cpu] = kmalloc_node(size,
794 GFP_KERNEL, cpu_to_node(cpu));
795 if (i->jumpstack[cpu] == NULL)
796 /*
797 * Freeing will be done later on by the callers. The
798 * chain is: xt_replace_table -> __do_replace ->
799 * do_replace -> xt_free_table_info.
800 */
801 return -ENOMEM;
802 }
803
804 return 0;
805}
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700806
Harald Welte2e4e6a12006-01-12 13:30:04 -0800807struct xt_table_info *
808xt_replace_table(struct xt_table *table,
809 unsigned int num_counters,
810 struct xt_table_info *newinfo,
811 int *error)
812{
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700813 struct xt_table_info *private;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200814 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800815
Jan Engelhardtd97a9e42010-04-21 14:45:51 +0200816 ret = xt_jumpstack_alloc(newinfo);
817 if (ret < 0) {
818 *error = ret;
819 return NULL;
820 }
821
Harald Welte2e4e6a12006-01-12 13:30:04 -0800822 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700823 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800824 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700825
Harald Welte2e4e6a12006-01-12 13:30:04 -0800826 /* Check inside lock: is the old number correct? */
827 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100828 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800829 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700830 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800831 *error = -EAGAIN;
832 return NULL;
833 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800834
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700835 table->private = newinfo;
836 newinfo->initial_entries = private->initial_entries;
837
838 /*
839 * Even though table entries have now been swapped, other CPU's
840 * may still be using the old entries. This is okay, because
841 * resynchronization happens because of the locking done
842 * during the get_counters() routine.
843 */
844 local_bh_enable();
845
Thomas Graffbabf312011-01-16 18:12:59 +0100846#ifdef CONFIG_AUDIT
847 if (audit_enabled) {
848 struct audit_buffer *ab;
849
850 ab = audit_log_start(current->audit_context, GFP_KERNEL,
851 AUDIT_NETFILTER_CFG);
852 if (ab) {
853 audit_log_format(ab, "table=%s family=%u entries=%u",
854 table->name, table->af,
855 private->number);
856 audit_log_end(ab);
857 }
858 }
859#endif
860
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700861 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800862}
863EXPORT_SYMBOL_GPL(xt_replace_table);
864
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200865struct xt_table *xt_register_table(struct net *net,
866 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -0800867 struct xt_table_info *bootstrap,
868 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800869{
870 int ret;
871 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200872 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800873
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800874 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200875 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800876 if (!table) {
877 ret = -ENOMEM;
878 goto out;
879 }
880
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800881 ret = mutex_lock_interruptible(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800882 if (ret != 0)
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800883 goto out_free;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800884
885 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800886 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700887 if (strcmp(t->name, table->name) == 0) {
888 ret = -EEXIST;
889 goto unlock;
890 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800891 }
892
893 /* Simplifies replace_table code. */
894 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +0100895
Harald Welte2e4e6a12006-01-12 13:30:04 -0800896 if (!xt_replace_table(table, 0, newinfo, &ret))
897 goto unlock;
898
899 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100900 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800901
902 /* save number of initial entries */
903 private->initial_entries = private->number;
904
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800905 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800906 mutex_unlock(&xt[table->af].mutex);
907 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800908
Harald Welte2e4e6a12006-01-12 13:30:04 -0800909 unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800910 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800911out_free:
912 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800913out:
914 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800915}
916EXPORT_SYMBOL_GPL(xt_register_table);
917
918void *xt_unregister_table(struct xt_table *table)
919{
920 struct xt_table_info *private;
921
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800922 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800923 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700924 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800925 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800926 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800927
928 return private;
929}
930EXPORT_SYMBOL_GPL(xt_unregister_table);
931
932#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800933struct xt_names_priv {
934 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200935 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800936};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800937static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800938{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800939 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900940 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200941 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800942
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800943 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800944 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800945}
946
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800947static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800948{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800949 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900950 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200951 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800952
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800953 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800954}
955
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800956static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800957{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800958 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200959 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800960
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800961 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800962}
963
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800964static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800965{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800966 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800967
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800968 if (strlen(table->name))
969 return seq_printf(seq, "%s\n", table->name);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800970 else
971 return 0;
972}
973
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800974static const struct seq_operations xt_table_seq_ops = {
975 .start = xt_table_seq_start,
976 .next = xt_table_seq_next,
977 .stop = xt_table_seq_stop,
978 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800979};
980
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800981static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800982{
983 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800984 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800985
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800986 ret = seq_open_net(inode, file, &xt_table_seq_ops,
987 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800988 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800989 priv = ((struct seq_file *)file->private_data)->private;
990 priv->af = (unsigned long)PDE(inode)->data;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800991 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800992 return ret;
993}
994
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800995static const struct file_operations xt_table_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800996 .owner = THIS_MODULE,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800997 .open = xt_table_open,
998 .read = seq_read,
999 .llseek = seq_lseek,
Pavel Emelyanov0e93bb92008-04-29 03:15:35 -07001000 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001001};
1002
Jan Engelhardteb132202009-02-18 16:42:19 +01001003/*
1004 * Traverse state for ip{,6}_{tables,matches} for helping crossing
1005 * the multi-AF mutexes.
1006 */
1007struct nf_mttg_trav {
1008 struct list_head *head, *curr;
1009 uint8_t class, nfproto;
1010};
1011
1012enum {
1013 MTTG_TRAV_INIT,
1014 MTTG_TRAV_NFP_UNSPEC,
1015 MTTG_TRAV_NFP_SPEC,
1016 MTTG_TRAV_DONE,
1017};
1018
1019static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
1020 bool is_target)
1021{
1022 static const uint8_t next_class[] = {
1023 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1024 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1025 };
1026 struct nf_mttg_trav *trav = seq->private;
1027
1028 switch (trav->class) {
1029 case MTTG_TRAV_INIT:
1030 trav->class = MTTG_TRAV_NFP_UNSPEC;
1031 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1032 trav->head = trav->curr = is_target ?
1033 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1034 break;
1035 case MTTG_TRAV_NFP_UNSPEC:
1036 trav->curr = trav->curr->next;
1037 if (trav->curr != trav->head)
1038 break;
1039 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1040 mutex_lock(&xt[trav->nfproto].mutex);
1041 trav->head = trav->curr = is_target ?
1042 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1043 trav->class = next_class[trav->class];
1044 break;
1045 case MTTG_TRAV_NFP_SPEC:
1046 trav->curr = trav->curr->next;
1047 if (trav->curr != trav->head)
1048 break;
1049 /* fallthru, _stop will unlock */
1050 default:
1051 return NULL;
1052 }
1053
1054 if (ppos != NULL)
1055 ++*ppos;
1056 return trav;
1057}
1058
1059static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1060 bool is_target)
1061{
1062 struct nf_mttg_trav *trav = seq->private;
1063 unsigned int j;
1064
1065 trav->class = MTTG_TRAV_INIT;
1066 for (j = 0; j < *pos; ++j)
1067 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1068 return NULL;
1069 return trav;
1070}
1071
1072static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1073{
1074 struct nf_mttg_trav *trav = seq->private;
1075
1076 switch (trav->class) {
1077 case MTTG_TRAV_NFP_UNSPEC:
1078 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1079 break;
1080 case MTTG_TRAV_NFP_SPEC:
1081 mutex_unlock(&xt[trav->nfproto].mutex);
1082 break;
1083 }
1084}
1085
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001086static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1087{
Jan Engelhardteb132202009-02-18 16:42:19 +01001088 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001089}
1090
Jan Engelhardteb132202009-02-18 16:42:19 +01001091static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001092{
Jan Engelhardteb132202009-02-18 16:42:19 +01001093 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001094}
1095
1096static int xt_match_seq_show(struct seq_file *seq, void *v)
1097{
Jan Engelhardteb132202009-02-18 16:42:19 +01001098 const struct nf_mttg_trav *trav = seq->private;
1099 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001100
Jan Engelhardteb132202009-02-18 16:42:19 +01001101 switch (trav->class) {
1102 case MTTG_TRAV_NFP_UNSPEC:
1103 case MTTG_TRAV_NFP_SPEC:
1104 if (trav->curr == trav->head)
1105 return 0;
1106 match = list_entry(trav->curr, struct xt_match, list);
1107 return (*match->name == '\0') ? 0 :
1108 seq_printf(seq, "%s\n", match->name);
1109 }
1110 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001111}
1112
1113static const struct seq_operations xt_match_seq_ops = {
1114 .start = xt_match_seq_start,
1115 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001116 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001117 .show = xt_match_seq_show,
1118};
1119
1120static int xt_match_open(struct inode *inode, struct file *file)
1121{
Jan Engelhardteb132202009-02-18 16:42:19 +01001122 struct seq_file *seq;
1123 struct nf_mttg_trav *trav;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001124 int ret;
1125
Jan Engelhardteb132202009-02-18 16:42:19 +01001126 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1127 if (trav == NULL)
1128 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001129
Jan Engelhardteb132202009-02-18 16:42:19 +01001130 ret = seq_open(file, &xt_match_seq_ops);
1131 if (ret < 0) {
1132 kfree(trav);
1133 return ret;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001134 }
Jan Engelhardteb132202009-02-18 16:42:19 +01001135
1136 seq = file->private_data;
1137 seq->private = trav;
1138 trav->nfproto = (unsigned long)PDE(inode)->data;
1139 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001140}
1141
1142static const struct file_operations xt_match_ops = {
1143 .owner = THIS_MODULE,
1144 .open = xt_match_open,
1145 .read = seq_read,
1146 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001147 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001148};
1149
1150static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1151{
Jan Engelhardteb132202009-02-18 16:42:19 +01001152 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001153}
1154
Jan Engelhardteb132202009-02-18 16:42:19 +01001155static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001156{
Jan Engelhardteb132202009-02-18 16:42:19 +01001157 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001158}
1159
1160static int xt_target_seq_show(struct seq_file *seq, void *v)
1161{
Jan Engelhardteb132202009-02-18 16:42:19 +01001162 const struct nf_mttg_trav *trav = seq->private;
1163 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001164
Jan Engelhardteb132202009-02-18 16:42:19 +01001165 switch (trav->class) {
1166 case MTTG_TRAV_NFP_UNSPEC:
1167 case MTTG_TRAV_NFP_SPEC:
1168 if (trav->curr == trav->head)
1169 return 0;
1170 target = list_entry(trav->curr, struct xt_target, list);
1171 return (*target->name == '\0') ? 0 :
1172 seq_printf(seq, "%s\n", target->name);
1173 }
1174 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001175}
1176
1177static const struct seq_operations xt_target_seq_ops = {
1178 .start = xt_target_seq_start,
1179 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001180 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001181 .show = xt_target_seq_show,
1182};
1183
1184static int xt_target_open(struct inode *inode, struct file *file)
1185{
Jan Engelhardteb132202009-02-18 16:42:19 +01001186 struct seq_file *seq;
1187 struct nf_mttg_trav *trav;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001188 int ret;
1189
Jan Engelhardteb132202009-02-18 16:42:19 +01001190 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1191 if (trav == NULL)
1192 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001193
Jan Engelhardteb132202009-02-18 16:42:19 +01001194 ret = seq_open(file, &xt_target_seq_ops);
1195 if (ret < 0) {
1196 kfree(trav);
1197 return ret;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001198 }
Jan Engelhardteb132202009-02-18 16:42:19 +01001199
1200 seq = file->private_data;
1201 seq->private = trav;
1202 trav->nfproto = (unsigned long)PDE(inode)->data;
1203 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001204}
1205
1206static const struct file_operations xt_target_ops = {
1207 .owner = THIS_MODULE,
1208 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001209 .read = seq_read,
1210 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001211 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001212};
1213
1214#define FORMAT_TABLES "_tables_names"
1215#define FORMAT_MATCHES "_tables_matches"
1216#define FORMAT_TARGETS "_tables_targets"
1217
1218#endif /* CONFIG_PROC_FS */
1219
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001220/**
1221 * xt_hook_link - set up hooks for a new table
1222 * @table: table with metadata needed to set up hooks
1223 * @fn: Hook function
1224 *
1225 * This function will take care of creating and registering the necessary
1226 * Netfilter hooks for XT tables.
1227 */
1228struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1229{
1230 unsigned int hook_mask = table->valid_hooks;
1231 uint8_t i, num_hooks = hweight32(hook_mask);
1232 uint8_t hooknum;
1233 struct nf_hook_ops *ops;
1234 int ret;
1235
1236 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1237 if (ops == NULL)
1238 return ERR_PTR(-ENOMEM);
1239
1240 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1241 hook_mask >>= 1, ++hooknum) {
1242 if (!(hook_mask & 1))
1243 continue;
1244 ops[i].hook = fn;
1245 ops[i].owner = table->me;
1246 ops[i].pf = table->af;
1247 ops[i].hooknum = hooknum;
1248 ops[i].priority = table->priority;
1249 ++i;
1250 }
1251
1252 ret = nf_register_hooks(ops, num_hooks);
1253 if (ret < 0) {
1254 kfree(ops);
1255 return ERR_PTR(ret);
1256 }
1257
1258 return ops;
1259}
1260EXPORT_SYMBOL_GPL(xt_hook_link);
1261
1262/**
1263 * xt_hook_unlink - remove hooks for a table
1264 * @ops: nf_hook_ops array as returned by nf_hook_link
1265 * @hook_mask: the very same mask that was passed to nf_hook_link
1266 */
1267void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1268{
1269 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1270 kfree(ops);
1271}
1272EXPORT_SYMBOL_GPL(xt_hook_unlink);
1273
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001274int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001275{
1276#ifdef CONFIG_PROC_FS
1277 char buf[XT_FUNCTION_MAXNAMELEN];
1278 struct proc_dir_entry *proc;
1279#endif
1280
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001281 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001282 return -EINVAL;
1283
1284
1285#ifdef CONFIG_PROC_FS
Tobias Klauserce18afe2007-03-14 16:36:16 -07001286 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001287 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001288 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1289 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001290 if (!proc)
1291 goto out;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001292
Tobias Klauserce18afe2007-03-14 16:36:16 -07001293 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001294 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001295 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1296 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001297 if (!proc)
1298 goto out_remove_tables;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001299
Tobias Klauserce18afe2007-03-14 16:36:16 -07001300 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001301 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001302 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1303 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001304 if (!proc)
1305 goto out_remove_matches;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001306#endif
1307
1308 return 0;
1309
1310#ifdef CONFIG_PROC_FS
1311out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001312 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001313 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001314 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001315
1316out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001317 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001318 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001319 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001320out:
1321 return -1;
1322#endif
1323}
1324EXPORT_SYMBOL_GPL(xt_proto_init);
1325
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001326void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001327{
1328#ifdef CONFIG_PROC_FS
1329 char buf[XT_FUNCTION_MAXNAMELEN];
1330
Tobias Klauserce18afe2007-03-14 16:36:16 -07001331 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001332 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001333 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001334
Tobias Klauserce18afe2007-03-14 16:36:16 -07001335 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001336 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001337 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001338
Tobias Klauserce18afe2007-03-14 16:36:16 -07001339 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001340 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001341 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001342#endif /*CONFIG_PROC_FS*/
1343}
1344EXPORT_SYMBOL_GPL(xt_proto_fini);
1345
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001346static int __net_init xt_net_init(struct net *net)
1347{
1348 int i;
1349
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001350 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001351 INIT_LIST_HEAD(&net->xt.tables[i]);
1352 return 0;
1353}
1354
1355static struct pernet_operations xt_net_ops = {
1356 .init = xt_net_init,
1357};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001358
1359static int __init xt_init(void)
1360{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001361 unsigned int i;
1362 int rv;
1363
1364 for_each_possible_cpu(i) {
Eric Dumazet7f5c6d42011-04-04 17:04:03 +02001365 seqcount_init(&per_cpu(xt_recseq, i));
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001366 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001367
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001368 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001369 if (!xt)
1370 return -ENOMEM;
1371
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001372 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001373 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001374#ifdef CONFIG_COMPAT
1375 mutex_init(&xt[i].compat_mutex);
Eric Dumazet255d0dc2010-12-18 18:35:15 +01001376 xt[i].compat_tab = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001377#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001378 INIT_LIST_HEAD(&xt[i].target);
1379 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001380 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001381 rv = register_pernet_subsys(&xt_net_ops);
1382 if (rv < 0)
1383 kfree(xt);
1384 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001385}
1386
1387static void __exit xt_fini(void)
1388{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001389 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001390 kfree(xt);
1391}
1392
1393module_init(xt_init);
1394module_exit(xt_fini);
1395