blob: bf2806afd92053bdad34f2cd9d98c89951b6e57e [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
Harald Welte2e4e6a12006-01-12 13:30:04 -080065/* Registration hooks for targets. */
66int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080067xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080068{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020069 u_int8_t af = target->family;
70 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -080071
Ingo Molnar9e19bb62006-03-25 01:41:10 -080072 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080073 if (ret != 0)
74 return ret;
75 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080076 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080077 return ret;
78}
79EXPORT_SYMBOL(xt_register_target);
80
81void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080082xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080083{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020084 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080085
Ingo Molnar9e19bb62006-03-25 01:41:10 -080086 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070087 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080088 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080089}
90EXPORT_SYMBOL(xt_unregister_target);
91
92int
Patrick McHardy52d9c422006-08-22 00:33:45 -070093xt_register_targets(struct xt_target *target, unsigned int n)
94{
95 unsigned int i;
96 int err = 0;
97
98 for (i = 0; i < n; i++) {
99 err = xt_register_target(&target[i]);
100 if (err)
101 goto err;
102 }
103 return err;
104
105err:
106 if (i > 0)
107 xt_unregister_targets(target, i);
108 return err;
109}
110EXPORT_SYMBOL(xt_register_targets);
111
112void
113xt_unregister_targets(struct xt_target *target, unsigned int n)
114{
115 unsigned int i;
116
117 for (i = 0; i < n; i++)
118 xt_unregister_target(&target[i]);
119}
120EXPORT_SYMBOL(xt_unregister_targets);
121
122int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800123xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800124{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200125 u_int8_t af = match->family;
126 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800127
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800128 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800129 if (ret != 0)
130 return ret;
131
132 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800133 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800134
135 return ret;
136}
137EXPORT_SYMBOL(xt_register_match);
138
139void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800140xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800141{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200142 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800143
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800144 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700145 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800146 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800147}
148EXPORT_SYMBOL(xt_unregister_match);
149
Patrick McHardy52d9c422006-08-22 00:33:45 -0700150int
151xt_register_matches(struct xt_match *match, unsigned int n)
152{
153 unsigned int i;
154 int err = 0;
155
156 for (i = 0; i < n; i++) {
157 err = xt_register_match(&match[i]);
158 if (err)
159 goto err;
160 }
161 return err;
162
163err:
164 if (i > 0)
165 xt_unregister_matches(match, i);
166 return err;
167}
168EXPORT_SYMBOL(xt_register_matches);
169
170void
171xt_unregister_matches(struct xt_match *match, unsigned int n)
172{
173 unsigned int i;
174
175 for (i = 0; i < n; i++)
176 xt_unregister_match(&match[i]);
177}
178EXPORT_SYMBOL(xt_unregister_matches);
179
Harald Welte2e4e6a12006-01-12 13:30:04 -0800180
181/*
182 * These are weird, but module loading must not be done with mutex
183 * held (since they will register), and we have to have a single
184 * function to use try_then_request_module().
185 */
186
187/* Find match, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200188struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800189{
190 struct xt_match *m;
191 int err = 0;
192
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800193 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800194 return ERR_PTR(-EINTR);
195
196 list_for_each_entry(m, &xt[af].match, list) {
197 if (strcmp(m->name, name) == 0) {
198 if (m->revision == revision) {
199 if (try_module_get(m->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800200 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800201 return m;
202 }
203 } else
204 err = -EPROTOTYPE; /* Found something. */
205 }
206 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800207 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200208
209 if (af != NFPROTO_UNSPEC)
210 /* Try searching again in the family-independent list */
211 return xt_find_match(NFPROTO_UNSPEC, name, revision);
212
Harald Welte2e4e6a12006-01-12 13:30:04 -0800213 return ERR_PTR(err);
214}
215EXPORT_SYMBOL(xt_find_match);
216
217/* Find target, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200218struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800219{
220 struct xt_target *t;
221 int err = 0;
222
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800223 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800224 return ERR_PTR(-EINTR);
225
226 list_for_each_entry(t, &xt[af].target, list) {
227 if (strcmp(t->name, name) == 0) {
228 if (t->revision == revision) {
229 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800230 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800231 return t;
232 }
233 } else
234 err = -EPROTOTYPE; /* Found something. */
235 }
236 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800237 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200238
239 if (af != NFPROTO_UNSPEC)
240 /* Try searching again in the family-independent list */
241 return xt_find_target(NFPROTO_UNSPEC, name, revision);
242
Harald Welte2e4e6a12006-01-12 13:30:04 -0800243 return ERR_PTR(err);
244}
245EXPORT_SYMBOL(xt_find_target);
246
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200247struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800248{
249 struct xt_target *target;
250
251 target = try_then_request_module(xt_find_target(af, name, revision),
Patrick McHardy37f9f732006-03-20 17:59:06 -0800252 "%st_%s", xt_prefix[af], name);
Jan Engelhardtd2a7b6b2009-07-10 18:55:11 +0200253 return (target != NULL) ? target : ERR_PTR(-ENOENT);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800254}
255EXPORT_SYMBOL_GPL(xt_request_find_target);
256
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200257static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800258{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200259 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800260 int have_rev = 0;
261
262 list_for_each_entry(m, &xt[af].match, list) {
263 if (strcmp(m->name, name) == 0) {
264 if (m->revision > *bestp)
265 *bestp = m->revision;
266 if (m->revision == revision)
267 have_rev = 1;
268 }
269 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000270
271 if (af != NFPROTO_UNSPEC && !have_rev)
272 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
273
Harald Welte2e4e6a12006-01-12 13:30:04 -0800274 return have_rev;
275}
276
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200277static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800278{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200279 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800280 int have_rev = 0;
281
282 list_for_each_entry(t, &xt[af].target, list) {
283 if (strcmp(t->name, name) == 0) {
284 if (t->revision > *bestp)
285 *bestp = t->revision;
286 if (t->revision == revision)
287 have_rev = 1;
288 }
289 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000290
291 if (af != NFPROTO_UNSPEC && !have_rev)
292 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
293
Harald Welte2e4e6a12006-01-12 13:30:04 -0800294 return have_rev;
295}
296
297/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200298int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800299 int *err)
300{
301 int have_rev, best = -1;
302
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800303 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800304 *err = -EINTR;
305 return 1;
306 }
307 if (target == 1)
308 have_rev = target_revfn(af, name, revision, &best);
309 else
310 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800311 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800312
313 /* Nothing at all? Return 0 to try loading module. */
314 if (best == -1) {
315 *err = -ENOENT;
316 return 0;
317 }
318
319 *err = best;
320 if (!have_rev)
321 *err = -EPROTONOSUPPORT;
322 return 1;
323}
324EXPORT_SYMBOL_GPL(xt_find_revision);
325
Jan Engelhardt45185362009-04-05 10:43:09 +0200326static char *textify_hooks(char *buf, size_t size, unsigned int mask)
327{
328 static const char *const names[] = {
329 "PREROUTING", "INPUT", "FORWARD",
330 "OUTPUT", "POSTROUTING", "BROUTING",
331 };
332 unsigned int i;
333 char *p = buf;
334 bool np = false;
335 int res;
336
337 *p = '\0';
338 for (i = 0; i < ARRAY_SIZE(names); ++i) {
339 if (!(mask & (1 << i)))
340 continue;
341 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
342 if (res > 0) {
343 size -= res;
344 p += res;
345 }
346 np = true;
347 }
348
349 return buf;
350}
351
Jan Engelhardt916a9172008-10-08 11:35:20 +0200352int xt_check_match(struct xt_mtchk_param *par,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200353 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800354{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200355 if (XT_ALIGN(par->match->matchsize) != size &&
356 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200357 /*
358 * ebt_among is exempt from centralized matchsize checking
359 * because it uses a dynamic-size data set.
360 */
Jan Engelhardtb4024052009-06-25 18:32:12 +0200361 pr_err("%s_tables: %s.%u match: invalid size "
362 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200363 xt_prefix[par->family], par->match->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200364 par->match->revision,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200365 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800366 return -EINVAL;
367 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200368 if (par->match->table != NULL &&
369 strcmp(par->match->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200370 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200371 xt_prefix[par->family], par->match->name,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200372 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800373 return -EINVAL;
374 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200375 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200376 char used[64], allow[64];
377
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200378 pr_err("%s_tables: %s match: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200379 "valid from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200380 xt_prefix[par->family], par->match->name,
Jan Engelhardt45185362009-04-05 10:43:09 +0200381 textify_hooks(used, sizeof(used), par->hook_mask),
382 textify_hooks(allow, sizeof(allow), par->match->hooks));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800383 return -EINVAL;
384 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200385 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200386 pr_err("%s_tables: %s match: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200387 xt_prefix[par->family], par->match->name,
388 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800389 return -EINVAL;
390 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200391 if (par->match->checkentry != NULL && !par->match->checkentry(par))
Jan Engelhardt367c6792008-10-08 11:35:17 +0200392 return -EINVAL;
Patrick McHardy37f9f732006-03-20 17:59:06 -0800393 return 0;
394}
395EXPORT_SYMBOL_GPL(xt_check_match);
396
Dmitry Mishin27229712006-04-01 02:25:19 -0800397#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200398int xt_compat_add_offset(u_int8_t af, unsigned int offset, short delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800399{
400 struct compat_delta *tmp;
401
402 tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
403 if (!tmp)
404 return -ENOMEM;
405
406 tmp->offset = offset;
407 tmp->delta = delta;
408
409 if (xt[af].compat_offsets) {
410 tmp->next = xt[af].compat_offsets->next;
411 xt[af].compat_offsets->next = tmp;
412 } else {
413 xt[af].compat_offsets = tmp;
414 tmp->next = NULL;
415 }
416 return 0;
417}
418EXPORT_SYMBOL_GPL(xt_compat_add_offset);
419
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200420void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800421{
422 struct compat_delta *tmp, *next;
423
424 if (xt[af].compat_offsets) {
425 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
426 next = tmp->next;
427 kfree(tmp);
428 }
429 xt[af].compat_offsets = NULL;
430 }
431}
432EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
433
Florian Westphal3e5e5242010-02-15 18:17:10 +0100434int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800435{
436 struct compat_delta *tmp;
Florian Westphal3e5e5242010-02-15 18:17:10 +0100437 int delta;
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800438
439 for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
440 if (tmp->offset < offset)
441 delta += tmp->delta;
442 return delta;
443}
444EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
445
Jan Engelhardt5452e422008-04-14 11:15:35 +0200446int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800447{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700448 u_int16_t csize = match->compatsize ? : match->matchsize;
449 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800450}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700451EXPORT_SYMBOL_GPL(xt_compat_match_offset);
452
Patrick McHardy89566952007-12-17 21:46:40 -0800453int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800454 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700455{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200456 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700457 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
458 int pad, off = xt_compat_match_offset(match);
459 u_int16_t msize = cm->u.user.match_size;
460
461 m = *dstptr;
462 memcpy(m, cm, sizeof(*cm));
463 if (match->compat_from_user)
464 match->compat_from_user(m->data, cm->data);
465 else
466 memcpy(m->data, cm->data, msize - sizeof(*cm));
467 pad = XT_ALIGN(match->matchsize) - match->matchsize;
468 if (pad > 0)
469 memset(m->data + match->matchsize, 0, pad);
470
471 msize += off;
472 m->u.user.match_size = msize;
473
474 *size += off;
475 *dstptr += msize;
Patrick McHardy89566952007-12-17 21:46:40 -0800476 return 0;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700477}
478EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
479
Jan Engelhardt739674f2009-06-26 08:23:19 +0200480int xt_compat_match_to_user(const struct xt_entry_match *m,
481 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700482{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200483 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700484 struct compat_xt_entry_match __user *cm = *dstptr;
485 int off = xt_compat_match_offset(match);
486 u_int16_t msize = m->u.user.match_size - off;
487
488 if (copy_to_user(cm, m, sizeof(*cm)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800489 put_user(msize, &cm->u.user.match_size) ||
490 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
491 strlen(m->u.kernel.match->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800492 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700493
494 if (match->compat_to_user) {
495 if (match->compat_to_user((void __user *)cm->data, m->data))
496 return -EFAULT;
497 } else {
498 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
499 return -EFAULT;
500 }
501
502 *size -= off;
503 *dstptr += msize;
504 return 0;
505}
506EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
507#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800508
Jan Engelhardt916a9172008-10-08 11:35:20 +0200509int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200510 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800511{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200512 if (XT_ALIGN(par->target->targetsize) != size) {
Jan Engelhardtb4024052009-06-25 18:32:12 +0200513 pr_err("%s_tables: %s.%u target: invalid size "
514 "%u (kernel) != (user) %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200515 xt_prefix[par->family], par->target->name,
Jan Engelhardtb4024052009-06-25 18:32:12 +0200516 par->target->revision,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200517 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800518 return -EINVAL;
519 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200520 if (par->target->table != NULL &&
521 strcmp(par->target->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200522 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200523 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200524 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800525 return -EINVAL;
526 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200527 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200528 char used[64], allow[64];
529
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200530 pr_err("%s_tables: %s target: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200531 "usable from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200532 xt_prefix[par->family], par->target->name,
Jan Engelhardt45185362009-04-05 10:43:09 +0200533 textify_hooks(used, sizeof(used), par->hook_mask),
534 textify_hooks(allow, sizeof(allow), par->target->hooks));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800535 return -EINVAL;
536 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200537 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200538 pr_err("%s_tables: %s target: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200539 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200540 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800541 return -EINVAL;
542 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200543 if (par->target->checkentry != NULL && !par->target->checkentry(par))
Jan Engelhardt367c6792008-10-08 11:35:17 +0200544 return -EINVAL;
Patrick McHardy37f9f732006-03-20 17:59:06 -0800545 return 0;
546}
547EXPORT_SYMBOL_GPL(xt_check_target);
548
Dmitry Mishin27229712006-04-01 02:25:19 -0800549#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +0200550int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800551{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700552 u_int16_t csize = target->compatsize ? : target->targetsize;
553 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800554}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700555EXPORT_SYMBOL_GPL(xt_compat_target_offset);
556
557void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800558 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700559{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200560 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700561 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
562 int pad, off = xt_compat_target_offset(target);
563 u_int16_t tsize = ct->u.user.target_size;
564
565 t = *dstptr;
566 memcpy(t, ct, sizeof(*ct));
567 if (target->compat_from_user)
568 target->compat_from_user(t->data, ct->data);
569 else
570 memcpy(t->data, ct->data, tsize - sizeof(*ct));
571 pad = XT_ALIGN(target->targetsize) - target->targetsize;
572 if (pad > 0)
573 memset(t->data + target->targetsize, 0, pad);
574
575 tsize += off;
576 t->u.user.target_size = tsize;
577
578 *size += off;
579 *dstptr += tsize;
580}
581EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
582
Jan Engelhardt739674f2009-06-26 08:23:19 +0200583int xt_compat_target_to_user(const struct xt_entry_target *t,
584 void __user **dstptr, unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700585{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200586 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700587 struct compat_xt_entry_target __user *ct = *dstptr;
588 int off = xt_compat_target_offset(target);
589 u_int16_t tsize = t->u.user.target_size - off;
590
591 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800592 put_user(tsize, &ct->u.user.target_size) ||
593 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
594 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800595 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700596
597 if (target->compat_to_user) {
598 if (target->compat_to_user((void __user *)ct->data, t->data))
599 return -EFAULT;
600 } else {
601 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
602 return -EFAULT;
603 }
604
605 *size -= off;
606 *dstptr += tsize;
607 return 0;
608}
609EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -0800610#endif
611
Harald Welte2e4e6a12006-01-12 13:30:04 -0800612struct xt_table_info *xt_alloc_table_info(unsigned int size)
613{
614 struct xt_table_info *newinfo;
615 int cpu;
616
617 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
Jan Beulich44813742009-09-21 17:03:05 -0700618 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800619 return NULL;
620
Eric Dumazet259d4e42007-12-04 23:24:56 -0800621 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800622 if (!newinfo)
623 return NULL;
624
625 newinfo->size = size;
626
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700627 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800628 if (size <= PAGE_SIZE)
629 newinfo->entries[cpu] = kmalloc_node(size,
630 GFP_KERNEL,
631 cpu_to_node(cpu));
632 else
633 newinfo->entries[cpu] = vmalloc_node(size,
634 cpu_to_node(cpu));
635
636 if (newinfo->entries[cpu] == NULL) {
637 xt_free_table_info(newinfo);
638 return NULL;
639 }
640 }
641
642 return newinfo;
643}
644EXPORT_SYMBOL(xt_alloc_table_info);
645
646void xt_free_table_info(struct xt_table_info *info)
647{
648 int cpu;
649
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700650 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800651 if (info->size <= PAGE_SIZE)
652 kfree(info->entries[cpu]);
653 else
654 vfree(info->entries[cpu]);
655 }
656 kfree(info);
657}
658EXPORT_SYMBOL(xt_free_table_info);
659
660/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200661struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
662 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800663{
664 struct xt_table *t;
665
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800666 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800667 return ERR_PTR(-EINTR);
668
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800669 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800670 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
671 return t;
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800672 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800673 return NULL;
674}
675EXPORT_SYMBOL_GPL(xt_find_table_lock);
676
677void xt_table_unlock(struct xt_table *table)
678{
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800679 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800680}
681EXPORT_SYMBOL_GPL(xt_table_unlock);
682
Dmitry Mishin27229712006-04-01 02:25:19 -0800683#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200684void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800685{
686 mutex_lock(&xt[af].compat_mutex);
687}
688EXPORT_SYMBOL_GPL(xt_compat_lock);
689
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200690void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800691{
692 mutex_unlock(&xt[af].compat_mutex);
693}
694EXPORT_SYMBOL_GPL(xt_compat_unlock);
695#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800696
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700697DEFINE_PER_CPU(struct xt_info_lock, xt_info_locks);
698EXPORT_PER_CPU_SYMBOL_GPL(xt_info_locks);
699
700
Harald Welte2e4e6a12006-01-12 13:30:04 -0800701struct xt_table_info *
702xt_replace_table(struct xt_table *table,
703 unsigned int num_counters,
704 struct xt_table_info *newinfo,
705 int *error)
706{
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700707 struct xt_table_info *private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800708
709 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700710 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800711 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700712
Harald Welte2e4e6a12006-01-12 13:30:04 -0800713 /* Check inside lock: is the old number correct? */
714 if (num_counters != private->number) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100715 pr_debug("num_counters != table->private->number (%u/%u)\n",
Harald Welte2e4e6a12006-01-12 13:30:04 -0800716 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700717 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800718 *error = -EAGAIN;
719 return NULL;
720 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800721
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700722 table->private = newinfo;
723 newinfo->initial_entries = private->initial_entries;
724
725 /*
726 * Even though table entries have now been swapped, other CPU's
727 * may still be using the old entries. This is okay, because
728 * resynchronization happens because of the locking done
729 * during the get_counters() routine.
730 */
731 local_bh_enable();
732
733 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800734}
735EXPORT_SYMBOL_GPL(xt_replace_table);
736
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200737struct xt_table *xt_register_table(struct net *net,
738 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -0800739 struct xt_table_info *bootstrap,
740 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800741{
742 int ret;
743 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200744 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800745
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800746 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200747 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800748 if (!table) {
749 ret = -ENOMEM;
750 goto out;
751 }
752
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800753 ret = mutex_lock_interruptible(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800754 if (ret != 0)
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800755 goto out_free;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800756
757 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800758 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700759 if (strcmp(t->name, table->name) == 0) {
760 ret = -EEXIST;
761 goto unlock;
762 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800763 }
764
765 /* Simplifies replace_table code. */
766 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +0100767
Harald Welte2e4e6a12006-01-12 13:30:04 -0800768 if (!xt_replace_table(table, 0, newinfo, &ret))
769 goto unlock;
770
771 private = table->private;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100772 pr_debug("table->private->number = %u\n", private->number);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800773
774 /* save number of initial entries */
775 private->initial_entries = private->number;
776
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800777 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800778 mutex_unlock(&xt[table->af].mutex);
779 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800780
Harald Welte2e4e6a12006-01-12 13:30:04 -0800781 unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800782 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800783out_free:
784 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800785out:
786 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800787}
788EXPORT_SYMBOL_GPL(xt_register_table);
789
790void *xt_unregister_table(struct xt_table *table)
791{
792 struct xt_table_info *private;
793
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800794 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800795 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700796 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800797 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800798 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800799
800 return private;
801}
802EXPORT_SYMBOL_GPL(xt_unregister_table);
803
804#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800805struct xt_names_priv {
806 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200807 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800808};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800809static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800810{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800811 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900812 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200813 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800814
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800815 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800816 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800817}
818
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800819static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800820{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800821 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900822 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200823 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800824
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800825 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800826}
827
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800828static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800829{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800830 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200831 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800832
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800833 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800834}
835
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800836static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800837{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800838 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800839
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800840 if (strlen(table->name))
841 return seq_printf(seq, "%s\n", table->name);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800842 else
843 return 0;
844}
845
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800846static const struct seq_operations xt_table_seq_ops = {
847 .start = xt_table_seq_start,
848 .next = xt_table_seq_next,
849 .stop = xt_table_seq_stop,
850 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800851};
852
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800853static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800854{
855 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800856 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800857
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800858 ret = seq_open_net(inode, file, &xt_table_seq_ops,
859 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800860 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800861 priv = ((struct seq_file *)file->private_data)->private;
862 priv->af = (unsigned long)PDE(inode)->data;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800863 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800864 return ret;
865}
866
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800867static const struct file_operations xt_table_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800868 .owner = THIS_MODULE,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800869 .open = xt_table_open,
870 .read = seq_read,
871 .llseek = seq_lseek,
Pavel Emelyanov0e93bb92008-04-29 03:15:35 -0700872 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800873};
874
Jan Engelhardteb132202009-02-18 16:42:19 +0100875/*
876 * Traverse state for ip{,6}_{tables,matches} for helping crossing
877 * the multi-AF mutexes.
878 */
879struct nf_mttg_trav {
880 struct list_head *head, *curr;
881 uint8_t class, nfproto;
882};
883
884enum {
885 MTTG_TRAV_INIT,
886 MTTG_TRAV_NFP_UNSPEC,
887 MTTG_TRAV_NFP_SPEC,
888 MTTG_TRAV_DONE,
889};
890
891static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
892 bool is_target)
893{
894 static const uint8_t next_class[] = {
895 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
896 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
897 };
898 struct nf_mttg_trav *trav = seq->private;
899
900 switch (trav->class) {
901 case MTTG_TRAV_INIT:
902 trav->class = MTTG_TRAV_NFP_UNSPEC;
903 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
904 trav->head = trav->curr = is_target ?
905 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
906 break;
907 case MTTG_TRAV_NFP_UNSPEC:
908 trav->curr = trav->curr->next;
909 if (trav->curr != trav->head)
910 break;
911 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
912 mutex_lock(&xt[trav->nfproto].mutex);
913 trav->head = trav->curr = is_target ?
914 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
915 trav->class = next_class[trav->class];
916 break;
917 case MTTG_TRAV_NFP_SPEC:
918 trav->curr = trav->curr->next;
919 if (trav->curr != trav->head)
920 break;
921 /* fallthru, _stop will unlock */
922 default:
923 return NULL;
924 }
925
926 if (ppos != NULL)
927 ++*ppos;
928 return trav;
929}
930
931static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
932 bool is_target)
933{
934 struct nf_mttg_trav *trav = seq->private;
935 unsigned int j;
936
937 trav->class = MTTG_TRAV_INIT;
938 for (j = 0; j < *pos; ++j)
939 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
940 return NULL;
941 return trav;
942}
943
944static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
945{
946 struct nf_mttg_trav *trav = seq->private;
947
948 switch (trav->class) {
949 case MTTG_TRAV_NFP_UNSPEC:
950 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
951 break;
952 case MTTG_TRAV_NFP_SPEC:
953 mutex_unlock(&xt[trav->nfproto].mutex);
954 break;
955 }
956}
957
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800958static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
959{
Jan Engelhardteb132202009-02-18 16:42:19 +0100960 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800961}
962
Jan Engelhardteb132202009-02-18 16:42:19 +0100963static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800964{
Jan Engelhardteb132202009-02-18 16:42:19 +0100965 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800966}
967
968static int xt_match_seq_show(struct seq_file *seq, void *v)
969{
Jan Engelhardteb132202009-02-18 16:42:19 +0100970 const struct nf_mttg_trav *trav = seq->private;
971 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800972
Jan Engelhardteb132202009-02-18 16:42:19 +0100973 switch (trav->class) {
974 case MTTG_TRAV_NFP_UNSPEC:
975 case MTTG_TRAV_NFP_SPEC:
976 if (trav->curr == trav->head)
977 return 0;
978 match = list_entry(trav->curr, struct xt_match, list);
979 return (*match->name == '\0') ? 0 :
980 seq_printf(seq, "%s\n", match->name);
981 }
982 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800983}
984
985static const struct seq_operations xt_match_seq_ops = {
986 .start = xt_match_seq_start,
987 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +0100988 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800989 .show = xt_match_seq_show,
990};
991
992static int xt_match_open(struct inode *inode, struct file *file)
993{
Jan Engelhardteb132202009-02-18 16:42:19 +0100994 struct seq_file *seq;
995 struct nf_mttg_trav *trav;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800996 int ret;
997
Jan Engelhardteb132202009-02-18 16:42:19 +0100998 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
999 if (trav == NULL)
1000 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001001
Jan Engelhardteb132202009-02-18 16:42:19 +01001002 ret = seq_open(file, &xt_match_seq_ops);
1003 if (ret < 0) {
1004 kfree(trav);
1005 return ret;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001006 }
Jan Engelhardteb132202009-02-18 16:42:19 +01001007
1008 seq = file->private_data;
1009 seq->private = trav;
1010 trav->nfproto = (unsigned long)PDE(inode)->data;
1011 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001012}
1013
1014static const struct file_operations xt_match_ops = {
1015 .owner = THIS_MODULE,
1016 .open = xt_match_open,
1017 .read = seq_read,
1018 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001019 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001020};
1021
1022static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1023{
Jan Engelhardteb132202009-02-18 16:42:19 +01001024 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001025}
1026
Jan Engelhardteb132202009-02-18 16:42:19 +01001027static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001028{
Jan Engelhardteb132202009-02-18 16:42:19 +01001029 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001030}
1031
1032static int xt_target_seq_show(struct seq_file *seq, void *v)
1033{
Jan Engelhardteb132202009-02-18 16:42:19 +01001034 const struct nf_mttg_trav *trav = seq->private;
1035 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001036
Jan Engelhardteb132202009-02-18 16:42:19 +01001037 switch (trav->class) {
1038 case MTTG_TRAV_NFP_UNSPEC:
1039 case MTTG_TRAV_NFP_SPEC:
1040 if (trav->curr == trav->head)
1041 return 0;
1042 target = list_entry(trav->curr, struct xt_target, list);
1043 return (*target->name == '\0') ? 0 :
1044 seq_printf(seq, "%s\n", target->name);
1045 }
1046 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001047}
1048
1049static const struct seq_operations xt_target_seq_ops = {
1050 .start = xt_target_seq_start,
1051 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001052 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001053 .show = xt_target_seq_show,
1054};
1055
1056static int xt_target_open(struct inode *inode, struct file *file)
1057{
Jan Engelhardteb132202009-02-18 16:42:19 +01001058 struct seq_file *seq;
1059 struct nf_mttg_trav *trav;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001060 int ret;
1061
Jan Engelhardteb132202009-02-18 16:42:19 +01001062 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1063 if (trav == NULL)
1064 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001065
Jan Engelhardteb132202009-02-18 16:42:19 +01001066 ret = seq_open(file, &xt_target_seq_ops);
1067 if (ret < 0) {
1068 kfree(trav);
1069 return ret;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001070 }
Jan Engelhardteb132202009-02-18 16:42:19 +01001071
1072 seq = file->private_data;
1073 seq->private = trav;
1074 trav->nfproto = (unsigned long)PDE(inode)->data;
1075 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001076}
1077
1078static const struct file_operations xt_target_ops = {
1079 .owner = THIS_MODULE,
1080 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001081 .read = seq_read,
1082 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001083 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001084};
1085
1086#define FORMAT_TABLES "_tables_names"
1087#define FORMAT_MATCHES "_tables_matches"
1088#define FORMAT_TARGETS "_tables_targets"
1089
1090#endif /* CONFIG_PROC_FS */
1091
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001092/**
1093 * xt_hook_link - set up hooks for a new table
1094 * @table: table with metadata needed to set up hooks
1095 * @fn: Hook function
1096 *
1097 * This function will take care of creating and registering the necessary
1098 * Netfilter hooks for XT tables.
1099 */
1100struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1101{
1102 unsigned int hook_mask = table->valid_hooks;
1103 uint8_t i, num_hooks = hweight32(hook_mask);
1104 uint8_t hooknum;
1105 struct nf_hook_ops *ops;
1106 int ret;
1107
1108 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1109 if (ops == NULL)
1110 return ERR_PTR(-ENOMEM);
1111
1112 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1113 hook_mask >>= 1, ++hooknum) {
1114 if (!(hook_mask & 1))
1115 continue;
1116 ops[i].hook = fn;
1117 ops[i].owner = table->me;
1118 ops[i].pf = table->af;
1119 ops[i].hooknum = hooknum;
1120 ops[i].priority = table->priority;
1121 ++i;
1122 }
1123
1124 ret = nf_register_hooks(ops, num_hooks);
1125 if (ret < 0) {
1126 kfree(ops);
1127 return ERR_PTR(ret);
1128 }
1129
1130 return ops;
1131}
1132EXPORT_SYMBOL_GPL(xt_hook_link);
1133
1134/**
1135 * xt_hook_unlink - remove hooks for a table
1136 * @ops: nf_hook_ops array as returned by nf_hook_link
1137 * @hook_mask: the very same mask that was passed to nf_hook_link
1138 */
1139void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1140{
1141 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1142 kfree(ops);
1143}
1144EXPORT_SYMBOL_GPL(xt_hook_unlink);
1145
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001146int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001147{
1148#ifdef CONFIG_PROC_FS
1149 char buf[XT_FUNCTION_MAXNAMELEN];
1150 struct proc_dir_entry *proc;
1151#endif
1152
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001153 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001154 return -EINVAL;
1155
1156
1157#ifdef CONFIG_PROC_FS
Tobias Klauserce18afe2007-03-14 16:36:16 -07001158 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001159 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001160 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1161 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001162 if (!proc)
1163 goto out;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001164
Tobias Klauserce18afe2007-03-14 16:36:16 -07001165 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001166 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001167 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1168 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001169 if (!proc)
1170 goto out_remove_tables;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001171
Tobias Klauserce18afe2007-03-14 16:36:16 -07001172 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001173 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001174 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1175 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001176 if (!proc)
1177 goto out_remove_matches;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001178#endif
1179
1180 return 0;
1181
1182#ifdef CONFIG_PROC_FS
1183out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001184 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001185 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001186 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001187
1188out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001189 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001190 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001191 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001192out:
1193 return -1;
1194#endif
1195}
1196EXPORT_SYMBOL_GPL(xt_proto_init);
1197
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001198void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001199{
1200#ifdef CONFIG_PROC_FS
1201 char buf[XT_FUNCTION_MAXNAMELEN];
1202
Tobias Klauserce18afe2007-03-14 16:36:16 -07001203 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001204 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001205 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001206
Tobias Klauserce18afe2007-03-14 16:36:16 -07001207 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001208 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001209 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001210
Tobias Klauserce18afe2007-03-14 16:36:16 -07001211 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001212 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001213 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001214#endif /*CONFIG_PROC_FS*/
1215}
1216EXPORT_SYMBOL_GPL(xt_proto_fini);
1217
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001218static int __net_init xt_net_init(struct net *net)
1219{
1220 int i;
1221
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001222 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001223 INIT_LIST_HEAD(&net->xt.tables[i]);
1224 return 0;
1225}
1226
1227static struct pernet_operations xt_net_ops = {
1228 .init = xt_net_init,
1229};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001230
1231static int __init xt_init(void)
1232{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001233 unsigned int i;
1234 int rv;
1235
1236 for_each_possible_cpu(i) {
1237 struct xt_info_lock *lock = &per_cpu(xt_info_locks, i);
1238 spin_lock_init(&lock->lock);
1239 lock->readers = 0;
1240 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001241
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001242 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001243 if (!xt)
1244 return -ENOMEM;
1245
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001246 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001247 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001248#ifdef CONFIG_COMPAT
1249 mutex_init(&xt[i].compat_mutex);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001250 xt[i].compat_offsets = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001251#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001252 INIT_LIST_HEAD(&xt[i].target);
1253 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001254 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001255 rv = register_pernet_subsys(&xt_net_ops);
1256 if (rv < 0)
1257 kfree(xt);
1258 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001259}
1260
1261static void __exit xt_fini(void)
1262{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001263 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001264 kfree(xt);
1265}
1266
1267module_init(xt_init);
1268module_exit(xt_fini);
1269