blob: edde5c602890602c1957164181db87453b33aeeb [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>
17#include <linux/socket.h>
18#include <linux/net.h>
19#include <linux/proc_fs.h>
20#include <linux/seq_file.h>
21#include <linux/string.h>
22#include <linux/vmalloc.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080023#include <linux/mutex.h>
Al Virod7fe0f22006-12-03 23:15:30 -050024#include <linux/mm.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020025#include <net/net_namespace.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080026
27#include <linux/netfilter/x_tables.h>
28#include <linux/netfilter_arp.h>
Jan Engelhardte3eaa992009-06-17 22:14:54 +020029#include <linux/netfilter_ipv4/ip_tables.h>
30#include <linux/netfilter_ipv6/ip6_tables.h>
31#include <linux/netfilter_arp/arp_tables.h>
Ingo Molnar9e19bb62006-03-25 01:41:10 -080032
Harald Welte2e4e6a12006-01-12 13:30:04 -080033MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt043ef462008-10-08 11:35:15 +020035MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080036
37#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
38
Patrick McHardyb386d9f2007-12-17 21:47:48 -080039struct compat_delta {
40 struct compat_delta *next;
41 unsigned int offset;
Florian Westphal3e5e5242010-02-15 18:17:10 +010042 int delta;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080043};
44
Harald Welte2e4e6a12006-01-12 13:30:04 -080045struct xt_af {
Ingo Molnar9e19bb62006-03-25 01:41:10 -080046 struct mutex mutex;
Harald Welte2e4e6a12006-01-12 13:30:04 -080047 struct list_head match;
48 struct list_head target;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080049#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080050 struct mutex compat_mutex;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080051 struct compat_delta *compat_offsets;
52#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080053};
54
55static struct xt_af *xt;
56
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020057static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
58 [NFPROTO_UNSPEC] = "x",
59 [NFPROTO_IPV4] = "ip",
60 [NFPROTO_ARP] = "arp",
61 [NFPROTO_BRIDGE] = "eb",
62 [NFPROTO_IPV6] = "ip6",
Patrick McHardy37f9f732006-03-20 17:59:06 -080063};
64
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +020065/* Allow this many total (re)entries. */
66static const unsigned int xt_jumpstack_multiplier = 2;
67
Harald Welte2e4e6a12006-01-12 13:30:04 -080068/* Registration hooks for targets. */
69int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080070xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080071{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020072 u_int8_t af = target->family;
73 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -080074
Ingo Molnar9e19bb62006-03-25 01:41:10 -080075 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080076 if (ret != 0)
77 return ret;
78 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080079 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080080 return ret;
81}
82EXPORT_SYMBOL(xt_register_target);
83
84void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080085xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080086{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020087 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080088
Ingo Molnar9e19bb62006-03-25 01:41:10 -080089 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070090 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080091 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080092}
93EXPORT_SYMBOL(xt_unregister_target);
94
95int
Patrick McHardy52d9c422006-08-22 00:33:45 -070096xt_register_targets(struct xt_target *target, unsigned int n)
97{
98 unsigned int i;
99 int err = 0;
100
101 for (i = 0; i < n; i++) {
102 err = xt_register_target(&target[i]);
103 if (err)
104 goto err;
105 }
106 return err;
107
108err:
109 if (i > 0)
110 xt_unregister_targets(target, i);
111 return err;
112}
113EXPORT_SYMBOL(xt_register_targets);
114
115void
116xt_unregister_targets(struct xt_target *target, unsigned int n)
117{
118 unsigned int i;
119
120 for (i = 0; i < n; i++)
121 xt_unregister_target(&target[i]);
122}
123EXPORT_SYMBOL(xt_unregister_targets);
124
125int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800126xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800127{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200128 u_int8_t af = match->family;
129 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800130
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800131 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800132 if (ret != 0)
133 return ret;
134
135 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800136 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800137
138 return ret;
139}
140EXPORT_SYMBOL(xt_register_match);
141
142void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800143xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800144{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200145 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800146
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800147 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700148 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800149 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800150}
151EXPORT_SYMBOL(xt_unregister_match);
152
Patrick McHardy52d9c422006-08-22 00:33:45 -0700153int
154xt_register_matches(struct xt_match *match, unsigned int n)
155{
156 unsigned int i;
157 int err = 0;
158
159 for (i = 0; i < n; i++) {
160 err = xt_register_match(&match[i]);
161 if (err)
162 goto err;
163 }
164 return err;
165
166err:
167 if (i > 0)
168 xt_unregister_matches(match, i);
169 return err;
170}
171EXPORT_SYMBOL(xt_register_matches);
172
173void
174xt_unregister_matches(struct xt_match *match, unsigned int n)
175{
176 unsigned int i;
177
178 for (i = 0; i < n; i++)
179 xt_unregister_match(&match[i]);
180}
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
187 * function to use try_then_request_module().
188 */
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;
194 int err = 0;
195
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
225 match = try_then_request_module(xt_find_match(nfproto, name, revision),
226 "%st_%s", xt_prefix[nfproto], name);
227 return (match != NULL) ? match : ERR_PTR(-ENOENT);
228}
229EXPORT_SYMBOL_GPL(xt_request_find_match);
230
Harald Welte2e4e6a12006-01-12 13:30:04 -0800231/* Find target, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200232struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800233{
234 struct xt_target *t;
235 int err = 0;
236
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800237 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800238 return ERR_PTR(-EINTR);
239
240 list_for_each_entry(t, &xt[af].target, list) {
241 if (strcmp(t->name, name) == 0) {
242 if (t->revision == revision) {
243 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800244 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800245 return t;
246 }
247 } else
248 err = -EPROTOTYPE; /* Found something. */
249 }
250 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800251 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200252
253 if (af != NFPROTO_UNSPEC)
254 /* Try searching again in the family-independent list */
255 return xt_find_target(NFPROTO_UNSPEC, name, revision);
256
Harald Welte2e4e6a12006-01-12 13:30:04 -0800257 return ERR_PTR(err);
258}
259EXPORT_SYMBOL(xt_find_target);
260
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200261struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800262{
263 struct xt_target *target;
264
265 target = try_then_request_module(xt_find_target(af, name, revision),
Patrick McHardy37f9f732006-03-20 17:59:06 -0800266 "%st_%s", xt_prefix[af], name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200267 return (target != NULL) ? target : ERR_PTR(-ENOENT);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800268}
269EXPORT_SYMBOL_GPL(xt_request_find_target);
270
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200271static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800272{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200273 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800274 int have_rev = 0;
275
276 list_for_each_entry(m, &xt[af].match, list) {
277 if (strcmp(m->name, name) == 0) {
278 if (m->revision > *bestp)
279 *bestp = m->revision;
280 if (m->revision == revision)
281 have_rev = 1;
282 }
283 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000284
285 if (af != NFPROTO_UNSPEC && !have_rev)
286 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
287
Harald Welte2e4e6a12006-01-12 13:30:04 -0800288 return have_rev;
289}
290
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200291static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800292{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200293 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800294 int have_rev = 0;
295
296 list_for_each_entry(t, &xt[af].target, list) {
297 if (strcmp(t->name, name) == 0) {
298 if (t->revision > *bestp)
299 *bestp = t->revision;
300 if (t->revision == revision)
301 have_rev = 1;
302 }
303 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000304
305 if (af != NFPROTO_UNSPEC && !have_rev)
306 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
307
Harald Welte2e4e6a12006-01-12 13:30:04 -0800308 return have_rev;
309}
310
311/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200312int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800313 int *err)
314{
315 int have_rev, best = -1;
316
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800317 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800318 *err = -EINTR;
319 return 1;
320 }
321 if (target == 1)
322 have_rev = target_revfn(af, name, revision, &best);
323 else
324 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800325 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800326
327 /* Nothing at all? Return 0 to try loading module. */
328 if (best == -1) {
329 *err = -ENOENT;
330 return 0;
331 }
332
333 *err = best;
334 if (!have_rev)
335 *err = -EPROTONOSUPPORT;
336 return 1;
337}
338EXPORT_SYMBOL_GPL(xt_find_revision);
339
Jan Engelhardt45185362009-04-05 10:43:09 +0200340static char *textify_hooks(char *buf, size_t size, unsigned int mask)
341{
342 static const char *const names[] = {
343 "PREROUTING", "INPUT", "FORWARD",
344 "OUTPUT", "POSTROUTING", "BROUTING",
345 };
346 unsigned int i;
347 char *p = buf;
348 bool np = false;
349 int res;
350
351 *p = '\0';
352 for (i = 0; i < ARRAY_SIZE(names); ++i) {
353 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 Engelhardt45185362009-04-05 10:43:09 +0200397 textify_hooks(used, sizeof(used), par->hook_mask),
398 textify_hooks(allow, sizeof(allow), par->match->hooks));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800399 return -EINVAL;
400 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200401 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200402 pr_err("%s_tables: %s match: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200403 xt_prefix[par->family], par->match->name,
404 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800405 return -EINVAL;
406 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100407 if (par->match->checkentry != NULL) {
408 ret = par->match->checkentry(par);
409 if (ret < 0)
410 return ret;
411 else if (ret > 0)
412 /* Flag up potential errors. */
413 return -EIO;
414 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800415 return 0;
416}
417EXPORT_SYMBOL_GPL(xt_check_match);
418
Dmitry Mishin27229712006-04-01 02:25:19 -0800419#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200420int xt_compat_add_offset(u_int8_t af, unsigned int offset, short delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800421{
422 struct compat_delta *tmp;
423
424 tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
425 if (!tmp)
426 return -ENOMEM;
427
428 tmp->offset = offset;
429 tmp->delta = delta;
430
431 if (xt[af].compat_offsets) {
432 tmp->next = xt[af].compat_offsets->next;
433 xt[af].compat_offsets->next = tmp;
434 } else {
435 xt[af].compat_offsets = tmp;
436 tmp->next = NULL;
437 }
438 return 0;
439}
440EXPORT_SYMBOL_GPL(xt_compat_add_offset);
441
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200442void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800443{
444 struct compat_delta *tmp, *next;
445
446 if (xt[af].compat_offsets) {
447 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
448 next = tmp->next;
449 kfree(tmp);
450 }
451 xt[af].compat_offsets = NULL;
452 }
453}
454EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
455
Florian Westphal3e5e5242010-02-15 18:17:10 +0100456int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800457{
458 struct compat_delta *tmp;
Florian Westphal3e5e5242010-02-15 18:17:10 +0100459 int delta;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800460
461 for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
462 if (tmp->offset < offset)
463 delta += tmp->delta;
464 return delta;
465}
466EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
467
Jan Engelhardt5452e422008-04-14 11:15:35 +0200468int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800469{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700470 u_int16_t csize = match->compatsize ? : match->matchsize;
471 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800472}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700473EXPORT_SYMBOL_GPL(xt_compat_match_offset);
474
Patrick McHardy89566952007-12-17 21:46:40 -0800475int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800476 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700477{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200478 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700479 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
480 int pad, off = xt_compat_match_offset(match);
481 u_int16_t msize = cm->u.user.match_size;
482
483 m = *dstptr;
484 memcpy(m, cm, sizeof(*cm));
485 if (match->compat_from_user)
486 match->compat_from_user(m->data, cm->data);
487 else
488 memcpy(m->data, cm->data, msize - sizeof(*cm));
489 pad = XT_ALIGN(match->matchsize) - match->matchsize;
490 if (pad > 0)
491 memset(m->data + match->matchsize, 0, pad);
492
493 msize += off;
494 m->u.user.match_size = msize;
495
496 *size += off;
497 *dstptr += msize;
Patrick McHardy89566952007-12-17 21:46:40 -0800498 return 0;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700499}
500EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
501
Jan Engelhardt739674f2009-06-26 08:23:19 +0200502int xt_compat_match_to_user(const struct xt_entry_match *m,
503 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700504{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200505 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700506 struct compat_xt_entry_match __user *cm = *dstptr;
507 int off = xt_compat_match_offset(match);
508 u_int16_t msize = m->u.user.match_size - off;
509
510 if (copy_to_user(cm, m, sizeof(*cm)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800511 put_user(msize, &cm->u.user.match_size) ||
512 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
513 strlen(m->u.kernel.match->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800514 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700515
516 if (match->compat_to_user) {
517 if (match->compat_to_user((void __user *)cm->data, m->data))
518 return -EFAULT;
519 } else {
520 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
521 return -EFAULT;
522 }
523
524 *size -= off;
525 *dstptr += msize;
526 return 0;
527}
528EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
529#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800530
Jan Engelhardt916a9172008-10-08 11:35:20 +0200531int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200532 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800533{
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100534 int ret;
535
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200536 if (XT_ALIGN(par->target->targetsize) != size) {
Jan Engelhardtb4024052009-06-25 18:32:12 +0200537 pr_err("%s_tables: %s.%u target: invalid size "
538 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200539 xt_prefix[par->family], par->target->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200540 par->target->revision,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200541 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800542 return -EINVAL;
543 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200544 if (par->target->table != NULL &&
545 strcmp(par->target->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200546 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200547 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200548 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800549 return -EINVAL;
550 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200551 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200552 char used[64], allow[64];
553
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200554 pr_err("%s_tables: %s target: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200555 "usable from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200556 xt_prefix[par->family], par->target->name,
Jan Engelhardt45185362009-04-05 10:43:09 +0200557 textify_hooks(used, sizeof(used), par->hook_mask),
558 textify_hooks(allow, sizeof(allow), par->target->hooks));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800559 return -EINVAL;
560 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200561 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200562 pr_err("%s_tables: %s target: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200563 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200564 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800565 return -EINVAL;
566 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100567 if (par->target->checkentry != NULL) {
568 ret = par->target->checkentry(par);
569 if (ret < 0)
570 return ret;
571 else if (ret > 0)
572 /* Flag up potential errors. */
573 return -EIO;
574 }
Patrick McHardy37f9f732006-03-20 17:59:06 -0800575 return 0;
576}
577EXPORT_SYMBOL_GPL(xt_check_target);
578
Dmitry Mishin27229712006-04-01 02:25:19 -0800579#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +0200580int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800581{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700582 u_int16_t csize = target->compatsize ? : target->targetsize;
583 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800584}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700585EXPORT_SYMBOL_GPL(xt_compat_target_offset);
586
587void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800588 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700589{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200590 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700591 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
592 int pad, off = xt_compat_target_offset(target);
593 u_int16_t tsize = ct->u.user.target_size;
594
595 t = *dstptr;
596 memcpy(t, ct, sizeof(*ct));
597 if (target->compat_from_user)
598 target->compat_from_user(t->data, ct->data);
599 else
600 memcpy(t->data, ct->data, tsize - sizeof(*ct));
601 pad = XT_ALIGN(target->targetsize) - target->targetsize;
602 if (pad > 0)
603 memset(t->data + target->targetsize, 0, pad);
604
605 tsize += off;
606 t->u.user.target_size = tsize;
607
608 *size += off;
609 *dstptr += tsize;
610}
611EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
612
Jan Engelhardt739674f2009-06-26 08:23:19 +0200613int xt_compat_target_to_user(const struct xt_entry_target *t,
614 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700615{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200616 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700617 struct compat_xt_entry_target __user *ct = *dstptr;
618 int off = xt_compat_target_offset(target);
619 u_int16_t tsize = t->u.user.target_size - off;
620
621 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800622 put_user(tsize, &ct->u.user.target_size) ||
623 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
624 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800625 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700626
627 if (target->compat_to_user) {
628 if (target->compat_to_user((void __user *)ct->data, t->data))
629 return -EFAULT;
630 } else {
631 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
632 return -EFAULT;
633 }
634
635 *size -= off;
636 *dstptr += tsize;
637 return 0;
638}
639EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -0800640#endif
641
Harald Welte2e4e6a12006-01-12 13:30:04 -0800642struct xt_table_info *xt_alloc_table_info(unsigned int size)
643{
644 struct xt_table_info *newinfo;
645 int cpu;
646
647 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
Jan Beulich44813742009-09-21 17:03:05 -0700648 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800649 return NULL;
650
Eric Dumazet259d4e42007-12-04 23:24:56 -0800651 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800652 if (!newinfo)
653 return NULL;
654
655 newinfo->size = size;
656
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700657 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800658 if (size <= PAGE_SIZE)
659 newinfo->entries[cpu] = kmalloc_node(size,
660 GFP_KERNEL,
661 cpu_to_node(cpu));
662 else
663 newinfo->entries[cpu] = vmalloc_node(size,
664 cpu_to_node(cpu));
665
666 if (newinfo->entries[cpu] == NULL) {
667 xt_free_table_info(newinfo);
668 return NULL;
669 }
670 }
671
672 return newinfo;
673}
674EXPORT_SYMBOL(xt_alloc_table_info);
675
676void xt_free_table_info(struct xt_table_info *info)
677{
678 int cpu;
679
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700680 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800681 if (info->size <= PAGE_SIZE)
682 kfree(info->entries[cpu]);
683 else
684 vfree(info->entries[cpu]);
685 }
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200686
687 if (info->jumpstack != NULL) {
688 if (sizeof(void *) * info->stacksize > PAGE_SIZE) {
689 for_each_possible_cpu(cpu)
690 vfree(info->jumpstack[cpu]);
691 } else {
692 for_each_possible_cpu(cpu)
693 kfree(info->jumpstack[cpu]);
694 }
695 }
696
697 if (sizeof(void **) * nr_cpu_ids > PAGE_SIZE)
698 vfree(info->jumpstack);
699 else
700 kfree(info->jumpstack);
701 if (sizeof(unsigned int) * nr_cpu_ids > PAGE_SIZE)
702 vfree(info->stackptr);
703 else
704 kfree(info->stackptr);
705
Harald Welte2e4e6a12006-01-12 13:30:04 -0800706 kfree(info);
707}
708EXPORT_SYMBOL(xt_free_table_info);
709
710/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200711struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
712 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800713{
714 struct xt_table *t;
715
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800716 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800717 return ERR_PTR(-EINTR);
718
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800719 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800720 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
721 return t;
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800722 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800723 return NULL;
724}
725EXPORT_SYMBOL_GPL(xt_find_table_lock);
726
727void xt_table_unlock(struct xt_table *table)
728{
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800729 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800730}
731EXPORT_SYMBOL_GPL(xt_table_unlock);
732
Dmitry Mishin27229712006-04-01 02:25:19 -0800733#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200734void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800735{
736 mutex_lock(&xt[af].compat_mutex);
737}
738EXPORT_SYMBOL_GPL(xt_compat_lock);
739
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200740void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800741{
742 mutex_unlock(&xt[af].compat_mutex);
743}
744EXPORT_SYMBOL_GPL(xt_compat_unlock);
745#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800746
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700747DEFINE_PER_CPU(struct xt_info_lock, xt_info_locks);
748EXPORT_PER_CPU_SYMBOL_GPL(xt_info_locks);
749
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200750static int xt_jumpstack_alloc(struct xt_table_info *i)
751{
752 unsigned int size;
753 int cpu;
754
755 size = sizeof(unsigned int) * nr_cpu_ids;
756 if (size > PAGE_SIZE)
757 i->stackptr = vmalloc(size);
758 else
759 i->stackptr = kmalloc(size, GFP_KERNEL);
760 if (i->stackptr == NULL)
761 return -ENOMEM;
762 memset(i->stackptr, 0, size);
763
764 size = sizeof(void **) * nr_cpu_ids;
765 if (size > PAGE_SIZE)
766 i->jumpstack = vmalloc(size);
767 else
768 i->jumpstack = kmalloc(size, GFP_KERNEL);
769 if (i->jumpstack == NULL)
770 return -ENOMEM;
771 memset(i->jumpstack, 0, size);
772
773 i->stacksize *= xt_jumpstack_multiplier;
774 size = sizeof(void *) * i->stacksize;
775 for_each_possible_cpu(cpu) {
776 if (size > PAGE_SIZE)
777 i->jumpstack[cpu] = vmalloc_node(size,
778 cpu_to_node(cpu));
779 else
780 i->jumpstack[cpu] = kmalloc_node(size,
781 GFP_KERNEL, cpu_to_node(cpu));
782 if (i->jumpstack[cpu] == NULL)
783 /*
784 * Freeing will be done later on by the callers. The
785 * chain is: xt_replace_table -> __do_replace ->
786 * do_replace -> xt_free_table_info.
787 */
788 return -ENOMEM;
789 }
790
791 return 0;
792}
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700793
Harald Welte2e4e6a12006-01-12 13:30:04 -0800794struct xt_table_info *
795xt_replace_table(struct xt_table *table,
796 unsigned int num_counters,
797 struct xt_table_info *newinfo,
798 int *error)
799{
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700800 struct xt_table_info *private;
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200801 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800802
803 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700804 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800805 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700806
Harald Welte2e4e6a12006-01-12 13:30:04 -0800807 /* Check inside lock: is the old number correct? */
808 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100809 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800810 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700811 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800812 *error = -EAGAIN;
813 return NULL;
814 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800815
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200816 ret = xt_jumpstack_alloc(newinfo);
817 if (ret < 0) {
818 *error = ret;
819 return NULL;
820 }
821
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700822 table->private = newinfo;
823 newinfo->initial_entries = private->initial_entries;
824
825 /*
826 * Even though table entries have now been swapped, other CPU's
827 * may still be using the old entries. This is okay, because
828 * resynchronization happens because of the locking done
829 * during the get_counters() routine.
830 */
831 local_bh_enable();
832
833 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800834}
835EXPORT_SYMBOL_GPL(xt_replace_table);
836
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200837struct xt_table *xt_register_table(struct net *net,
838 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -0800839 struct xt_table_info *bootstrap,
840 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800841{
842 int ret;
843 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200844 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800845
Jan Engelhardtf3c5c1b2010-04-19 16:05:10 +0200846 ret = xt_jumpstack_alloc(newinfo);
847 if (ret < 0)
848 return ERR_PTR(ret);
849
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800850 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200851 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800852 if (!table) {
853 ret = -ENOMEM;
854 goto out;
855 }
856
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800857 ret = mutex_lock_interruptible(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800858 if (ret != 0)
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800859 goto out_free;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800860
861 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800862 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700863 if (strcmp(t->name, table->name) == 0) {
864 ret = -EEXIST;
865 goto unlock;
866 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800867 }
868
869 /* Simplifies replace_table code. */
870 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +0100871
Harald Welte2e4e6a12006-01-12 13:30:04 -0800872 if (!xt_replace_table(table, 0, newinfo, &ret))
873 goto unlock;
874
875 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100876 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800877
878 /* save number of initial entries */
879 private->initial_entries = private->number;
880
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800881 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800882 mutex_unlock(&xt[table->af].mutex);
883 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800884
Harald Welte2e4e6a12006-01-12 13:30:04 -0800885 unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800886 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800887out_free:
888 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800889out:
890 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800891}
892EXPORT_SYMBOL_GPL(xt_register_table);
893
894void *xt_unregister_table(struct xt_table *table)
895{
896 struct xt_table_info *private;
897
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800898 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800899 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700900 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800901 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800902 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800903
904 return private;
905}
906EXPORT_SYMBOL_GPL(xt_unregister_table);
907
908#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800909struct xt_names_priv {
910 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200911 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800912};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800913static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800914{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800915 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900916 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200917 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800918
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800919 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800920 return seq_list_start(&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_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800924{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800925 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900926 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200927 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800928
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800929 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800930}
931
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800932static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800933{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800934 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200935 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800936
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800937 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800938}
939
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800940static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800941{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800942 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800943
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800944 if (strlen(table->name))
945 return seq_printf(seq, "%s\n", table->name);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800946 else
947 return 0;
948}
949
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800950static const struct seq_operations xt_table_seq_ops = {
951 .start = xt_table_seq_start,
952 .next = xt_table_seq_next,
953 .stop = xt_table_seq_stop,
954 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800955};
956
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800957static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800958{
959 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800960 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800961
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800962 ret = seq_open_net(inode, file, &xt_table_seq_ops,
963 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800964 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800965 priv = ((struct seq_file *)file->private_data)->private;
966 priv->af = (unsigned long)PDE(inode)->data;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800967 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800968 return ret;
969}
970
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800971static const struct file_operations xt_table_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800972 .owner = THIS_MODULE,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800973 .open = xt_table_open,
974 .read = seq_read,
975 .llseek = seq_lseek,
Pavel Emelyanov0e93bb92008-04-29 03:15:35 -0700976 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800977};
978
Jan Engelhardteb132202009-02-18 16:42:19 +0100979/*
980 * Traverse state for ip{,6}_{tables,matches} for helping crossing
981 * the multi-AF mutexes.
982 */
983struct nf_mttg_trav {
984 struct list_head *head, *curr;
985 uint8_t class, nfproto;
986};
987
988enum {
989 MTTG_TRAV_INIT,
990 MTTG_TRAV_NFP_UNSPEC,
991 MTTG_TRAV_NFP_SPEC,
992 MTTG_TRAV_DONE,
993};
994
995static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
996 bool is_target)
997{
998 static const uint8_t next_class[] = {
999 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
1000 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
1001 };
1002 struct nf_mttg_trav *trav = seq->private;
1003
1004 switch (trav->class) {
1005 case MTTG_TRAV_INIT:
1006 trav->class = MTTG_TRAV_NFP_UNSPEC;
1007 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
1008 trav->head = trav->curr = is_target ?
1009 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
1010 break;
1011 case MTTG_TRAV_NFP_UNSPEC:
1012 trav->curr = trav->curr->next;
1013 if (trav->curr != trav->head)
1014 break;
1015 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1016 mutex_lock(&xt[trav->nfproto].mutex);
1017 trav->head = trav->curr = is_target ?
1018 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
1019 trav->class = next_class[trav->class];
1020 break;
1021 case MTTG_TRAV_NFP_SPEC:
1022 trav->curr = trav->curr->next;
1023 if (trav->curr != trav->head)
1024 break;
1025 /* fallthru, _stop will unlock */
1026 default:
1027 return NULL;
1028 }
1029
1030 if (ppos != NULL)
1031 ++*ppos;
1032 return trav;
1033}
1034
1035static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
1036 bool is_target)
1037{
1038 struct nf_mttg_trav *trav = seq->private;
1039 unsigned int j;
1040
1041 trav->class = MTTG_TRAV_INIT;
1042 for (j = 0; j < *pos; ++j)
1043 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
1044 return NULL;
1045 return trav;
1046}
1047
1048static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
1049{
1050 struct nf_mttg_trav *trav = seq->private;
1051
1052 switch (trav->class) {
1053 case MTTG_TRAV_NFP_UNSPEC:
1054 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
1055 break;
1056 case MTTG_TRAV_NFP_SPEC:
1057 mutex_unlock(&xt[trav->nfproto].mutex);
1058 break;
1059 }
1060}
1061
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001062static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
1063{
Jan Engelhardteb132202009-02-18 16:42:19 +01001064 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001065}
1066
Jan Engelhardteb132202009-02-18 16:42:19 +01001067static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001068{
Jan Engelhardteb132202009-02-18 16:42:19 +01001069 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001070}
1071
1072static int xt_match_seq_show(struct seq_file *seq, void *v)
1073{
Jan Engelhardteb132202009-02-18 16:42:19 +01001074 const struct nf_mttg_trav *trav = seq->private;
1075 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001076
Jan Engelhardteb132202009-02-18 16:42:19 +01001077 switch (trav->class) {
1078 case MTTG_TRAV_NFP_UNSPEC:
1079 case MTTG_TRAV_NFP_SPEC:
1080 if (trav->curr == trav->head)
1081 return 0;
1082 match = list_entry(trav->curr, struct xt_match, list);
1083 return (*match->name == '\0') ? 0 :
1084 seq_printf(seq, "%s\n", match->name);
1085 }
1086 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001087}
1088
1089static const struct seq_operations xt_match_seq_ops = {
1090 .start = xt_match_seq_start,
1091 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001092 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001093 .show = xt_match_seq_show,
1094};
1095
1096static int xt_match_open(struct inode *inode, struct file *file)
1097{
Jan Engelhardteb132202009-02-18 16:42:19 +01001098 struct seq_file *seq;
1099 struct nf_mttg_trav *trav;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001100 int ret;
1101
Jan Engelhardteb132202009-02-18 16:42:19 +01001102 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1103 if (trav == NULL)
1104 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001105
Jan Engelhardteb132202009-02-18 16:42:19 +01001106 ret = seq_open(file, &xt_match_seq_ops);
1107 if (ret < 0) {
1108 kfree(trav);
1109 return ret;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001110 }
Jan Engelhardteb132202009-02-18 16:42:19 +01001111
1112 seq = file->private_data;
1113 seq->private = trav;
1114 trav->nfproto = (unsigned long)PDE(inode)->data;
1115 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001116}
1117
1118static const struct file_operations xt_match_ops = {
1119 .owner = THIS_MODULE,
1120 .open = xt_match_open,
1121 .read = seq_read,
1122 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001123 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001124};
1125
1126static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1127{
Jan Engelhardteb132202009-02-18 16:42:19 +01001128 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001129}
1130
Jan Engelhardteb132202009-02-18 16:42:19 +01001131static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001132{
Jan Engelhardteb132202009-02-18 16:42:19 +01001133 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001134}
1135
1136static int xt_target_seq_show(struct seq_file *seq, void *v)
1137{
Jan Engelhardteb132202009-02-18 16:42:19 +01001138 const struct nf_mttg_trav *trav = seq->private;
1139 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001140
Jan Engelhardteb132202009-02-18 16:42:19 +01001141 switch (trav->class) {
1142 case MTTG_TRAV_NFP_UNSPEC:
1143 case MTTG_TRAV_NFP_SPEC:
1144 if (trav->curr == trav->head)
1145 return 0;
1146 target = list_entry(trav->curr, struct xt_target, list);
1147 return (*target->name == '\0') ? 0 :
1148 seq_printf(seq, "%s\n", target->name);
1149 }
1150 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001151}
1152
1153static const struct seq_operations xt_target_seq_ops = {
1154 .start = xt_target_seq_start,
1155 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001156 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001157 .show = xt_target_seq_show,
1158};
1159
1160static int xt_target_open(struct inode *inode, struct file *file)
1161{
Jan Engelhardteb132202009-02-18 16:42:19 +01001162 struct seq_file *seq;
1163 struct nf_mttg_trav *trav;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001164 int ret;
1165
Jan Engelhardteb132202009-02-18 16:42:19 +01001166 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1167 if (trav == NULL)
1168 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001169
Jan Engelhardteb132202009-02-18 16:42:19 +01001170 ret = seq_open(file, &xt_target_seq_ops);
1171 if (ret < 0) {
1172 kfree(trav);
1173 return ret;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001174 }
Jan Engelhardteb132202009-02-18 16:42:19 +01001175
1176 seq = file->private_data;
1177 seq->private = trav;
1178 trav->nfproto = (unsigned long)PDE(inode)->data;
1179 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001180}
1181
1182static const struct file_operations xt_target_ops = {
1183 .owner = THIS_MODULE,
1184 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001185 .read = seq_read,
1186 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001187 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001188};
1189
1190#define FORMAT_TABLES "_tables_names"
1191#define FORMAT_MATCHES "_tables_matches"
1192#define FORMAT_TARGETS "_tables_targets"
1193
1194#endif /* CONFIG_PROC_FS */
1195
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001196/**
1197 * xt_hook_link - set up hooks for a new table
1198 * @table: table with metadata needed to set up hooks
1199 * @fn: Hook function
1200 *
1201 * This function will take care of creating and registering the necessary
1202 * Netfilter hooks for XT tables.
1203 */
1204struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1205{
1206 unsigned int hook_mask = table->valid_hooks;
1207 uint8_t i, num_hooks = hweight32(hook_mask);
1208 uint8_t hooknum;
1209 struct nf_hook_ops *ops;
1210 int ret;
1211
1212 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1213 if (ops == NULL)
1214 return ERR_PTR(-ENOMEM);
1215
1216 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1217 hook_mask >>= 1, ++hooknum) {
1218 if (!(hook_mask & 1))
1219 continue;
1220 ops[i].hook = fn;
1221 ops[i].owner = table->me;
1222 ops[i].pf = table->af;
1223 ops[i].hooknum = hooknum;
1224 ops[i].priority = table->priority;
1225 ++i;
1226 }
1227
1228 ret = nf_register_hooks(ops, num_hooks);
1229 if (ret < 0) {
1230 kfree(ops);
1231 return ERR_PTR(ret);
1232 }
1233
1234 return ops;
1235}
1236EXPORT_SYMBOL_GPL(xt_hook_link);
1237
1238/**
1239 * xt_hook_unlink - remove hooks for a table
1240 * @ops: nf_hook_ops array as returned by nf_hook_link
1241 * @hook_mask: the very same mask that was passed to nf_hook_link
1242 */
1243void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1244{
1245 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1246 kfree(ops);
1247}
1248EXPORT_SYMBOL_GPL(xt_hook_unlink);
1249
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001250int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001251{
1252#ifdef CONFIG_PROC_FS
1253 char buf[XT_FUNCTION_MAXNAMELEN];
1254 struct proc_dir_entry *proc;
1255#endif
1256
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001257 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001258 return -EINVAL;
1259
1260
1261#ifdef CONFIG_PROC_FS
Tobias Klauserce18afe2007-03-14 16:36:16 -07001262 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001263 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001264 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1265 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001266 if (!proc)
1267 goto out;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001268
Tobias Klauserce18afe2007-03-14 16:36:16 -07001269 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001270 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001271 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1272 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001273 if (!proc)
1274 goto out_remove_tables;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001275
Tobias Klauserce18afe2007-03-14 16:36:16 -07001276 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001277 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001278 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1279 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001280 if (!proc)
1281 goto out_remove_matches;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001282#endif
1283
1284 return 0;
1285
1286#ifdef CONFIG_PROC_FS
1287out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001288 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001289 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001290 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001291
1292out_remove_tables:
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_TABLES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001295 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001296out:
1297 return -1;
1298#endif
1299}
1300EXPORT_SYMBOL_GPL(xt_proto_init);
1301
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001302void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001303{
1304#ifdef CONFIG_PROC_FS
1305 char buf[XT_FUNCTION_MAXNAMELEN];
1306
Tobias Klauserce18afe2007-03-14 16:36:16 -07001307 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001308 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001309 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001310
Tobias Klauserce18afe2007-03-14 16:36:16 -07001311 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001312 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001313 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001314
Tobias Klauserce18afe2007-03-14 16:36:16 -07001315 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001316 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001317 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001318#endif /*CONFIG_PROC_FS*/
1319}
1320EXPORT_SYMBOL_GPL(xt_proto_fini);
1321
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001322static int __net_init xt_net_init(struct net *net)
1323{
1324 int i;
1325
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001326 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001327 INIT_LIST_HEAD(&net->xt.tables[i]);
1328 return 0;
1329}
1330
1331static struct pernet_operations xt_net_ops = {
1332 .init = xt_net_init,
1333};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001334
1335static int __init xt_init(void)
1336{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001337 unsigned int i;
1338 int rv;
1339
1340 for_each_possible_cpu(i) {
1341 struct xt_info_lock *lock = &per_cpu(xt_info_locks, i);
1342 spin_lock_init(&lock->lock);
1343 lock->readers = 0;
1344 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001345
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001346 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001347 if (!xt)
1348 return -ENOMEM;
1349
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001350 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001351 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001352#ifdef CONFIG_COMPAT
1353 mutex_init(&xt[i].compat_mutex);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001354 xt[i].compat_offsets = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001355#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001356 INIT_LIST_HEAD(&xt[i].target);
1357 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001358 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001359 rv = register_pernet_subsys(&xt_net_ops);
1360 if (rv < 0)
1361 kfree(xt);
1362 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001363}
1364
1365static void __exit xt_fini(void)
1366{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001367 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001368 kfree(xt);
1369}
1370
1371module_init(xt_init);
1372module_exit(xt_fini);
1373