blob: b51cb0d7234a80bd15c8147cfc47823d928fcd8f [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 */
15
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>
29
Ingo Molnar9e19bb62006-03-25 01:41:10 -080030
Harald Welte2e4e6a12006-01-12 13:30:04 -080031MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt043ef462008-10-08 11:35:15 +020033MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080034
35#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
36
Patrick McHardyb386d9f2007-12-17 21:47:48 -080037struct compat_delta {
38 struct compat_delta *next;
39 unsigned int offset;
40 short delta;
41};
42
Harald Welte2e4e6a12006-01-12 13:30:04 -080043struct xt_af {
Ingo Molnar9e19bb62006-03-25 01:41:10 -080044 struct mutex mutex;
Harald Welte2e4e6a12006-01-12 13:30:04 -080045 struct list_head match;
46 struct list_head target;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080047#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080048 struct mutex compat_mutex;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080049 struct compat_delta *compat_offsets;
50#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080051};
52
53static struct xt_af *xt;
54
55#ifdef DEBUG_IP_FIREWALL_USER
56#define duprintf(format, args...) printk(format , ## args)
57#else
58#define duprintf(format, args...)
59#endif
60
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020061static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
62 [NFPROTO_UNSPEC] = "x",
63 [NFPROTO_IPV4] = "ip",
64 [NFPROTO_ARP] = "arp",
65 [NFPROTO_BRIDGE] = "eb",
66 [NFPROTO_IPV6] = "ip6",
Patrick McHardy37f9f732006-03-20 17:59:06 -080067};
68
Harald Welte2e4e6a12006-01-12 13:30:04 -080069/* Registration hooks for targets. */
70int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080071xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080072{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020073 u_int8_t af = target->family;
74 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -080075
Ingo Molnar9e19bb62006-03-25 01:41:10 -080076 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080077 if (ret != 0)
78 return ret;
79 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080080 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080081 return ret;
82}
83EXPORT_SYMBOL(xt_register_target);
84
85void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080086xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080087{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020088 u_int8_t af = target->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080089
Ingo Molnar9e19bb62006-03-25 01:41:10 -080090 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070091 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080092 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080093}
94EXPORT_SYMBOL(xt_unregister_target);
95
96int
Patrick McHardy52d9c422006-08-22 00:33:45 -070097xt_register_targets(struct xt_target *target, unsigned int n)
98{
99 unsigned int i;
100 int err = 0;
101
102 for (i = 0; i < n; i++) {
103 err = xt_register_target(&target[i]);
104 if (err)
105 goto err;
106 }
107 return err;
108
109err:
110 if (i > 0)
111 xt_unregister_targets(target, i);
112 return err;
113}
114EXPORT_SYMBOL(xt_register_targets);
115
116void
117xt_unregister_targets(struct xt_target *target, unsigned int n)
118{
119 unsigned int i;
120
121 for (i = 0; i < n; i++)
122 xt_unregister_target(&target[i]);
123}
124EXPORT_SYMBOL(xt_unregister_targets);
125
126int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800127xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800128{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200129 u_int8_t af = match->family;
130 int ret;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800131
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800132 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800133 if (ret != 0)
134 return ret;
135
136 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800137 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800138
139 return ret;
140}
141EXPORT_SYMBOL(xt_register_match);
142
143void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800144xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800145{
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200146 u_int8_t af = match->family;
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800147
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800148 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700149 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800150 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800151}
152EXPORT_SYMBOL(xt_unregister_match);
153
Patrick McHardy52d9c422006-08-22 00:33:45 -0700154int
155xt_register_matches(struct xt_match *match, unsigned int n)
156{
157 unsigned int i;
158 int err = 0;
159
160 for (i = 0; i < n; i++) {
161 err = xt_register_match(&match[i]);
162 if (err)
163 goto err;
164 }
165 return err;
166
167err:
168 if (i > 0)
169 xt_unregister_matches(match, i);
170 return err;
171}
172EXPORT_SYMBOL(xt_register_matches);
173
174void
175xt_unregister_matches(struct xt_match *match, unsigned int n)
176{
177 unsigned int i;
178
179 for (i = 0; i < n; i++)
180 xt_unregister_match(&match[i]);
181}
182EXPORT_SYMBOL(xt_unregister_matches);
183
Harald Welte2e4e6a12006-01-12 13:30:04 -0800184
185/*
186 * These are weird, but module loading must not be done with mutex
187 * held (since they will register), and we have to have a single
188 * function to use try_then_request_module().
189 */
190
191/* Find match, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200192struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800193{
194 struct xt_match *m;
195 int err = 0;
196
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800197 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800198 return ERR_PTR(-EINTR);
199
200 list_for_each_entry(m, &xt[af].match, list) {
201 if (strcmp(m->name, name) == 0) {
202 if (m->revision == revision) {
203 if (try_module_get(m->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800204 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800205 return m;
206 }
207 } else
208 err = -EPROTOTYPE; /* Found something. */
209 }
210 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800211 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200212
213 if (af != NFPROTO_UNSPEC)
214 /* Try searching again in the family-independent list */
215 return xt_find_match(NFPROTO_UNSPEC, name, revision);
216
Harald Welte2e4e6a12006-01-12 13:30:04 -0800217 return ERR_PTR(err);
218}
219EXPORT_SYMBOL(xt_find_match);
220
221/* Find target, grabs ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200222struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800223{
224 struct xt_target *t;
225 int err = 0;
226
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800227 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800228 return ERR_PTR(-EINTR);
229
230 list_for_each_entry(t, &xt[af].target, list) {
231 if (strcmp(t->name, name) == 0) {
232 if (t->revision == revision) {
233 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800234 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800235 return t;
236 }
237 } else
238 err = -EPROTOTYPE; /* Found something. */
239 }
240 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800241 mutex_unlock(&xt[af].mutex);
Jan Engelhardt55b69e92008-10-08 11:35:01 +0200242
243 if (af != NFPROTO_UNSPEC)
244 /* Try searching again in the family-independent list */
245 return xt_find_target(NFPROTO_UNSPEC, name, revision);
246
Harald Welte2e4e6a12006-01-12 13:30:04 -0800247 return ERR_PTR(err);
248}
249EXPORT_SYMBOL(xt_find_target);
250
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200251struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800252{
253 struct xt_target *target;
254
255 target = try_then_request_module(xt_find_target(af, name, revision),
Patrick McHardy37f9f732006-03-20 17:59:06 -0800256 "%st_%s", xt_prefix[af], name);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800257 if (IS_ERR(target) || !target)
258 return NULL;
259 return target;
260}
261EXPORT_SYMBOL_GPL(xt_request_find_target);
262
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200263static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800264{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200265 const struct xt_match *m;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800266 int have_rev = 0;
267
268 list_for_each_entry(m, &xt[af].match, list) {
269 if (strcmp(m->name, name) == 0) {
270 if (m->revision > *bestp)
271 *bestp = m->revision;
272 if (m->revision == revision)
273 have_rev = 1;
274 }
275 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000276
277 if (af != NFPROTO_UNSPEC && !have_rev)
278 return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
279
Harald Welte2e4e6a12006-01-12 13:30:04 -0800280 return have_rev;
281}
282
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200283static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800284{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200285 const struct xt_target *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800286 int have_rev = 0;
287
288 list_for_each_entry(t, &xt[af].target, list) {
289 if (strcmp(t->name, name) == 0) {
290 if (t->revision > *bestp)
291 *bestp = t->revision;
292 if (t->revision == revision)
293 have_rev = 1;
294 }
295 }
Patrick McHardy656caff2009-01-12 00:06:04 +0000296
297 if (af != NFPROTO_UNSPEC && !have_rev)
298 return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
299
Harald Welte2e4e6a12006-01-12 13:30:04 -0800300 return have_rev;
301}
302
303/* Returns true or false (if no such extension at all) */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200304int xt_find_revision(u8 af, const char *name, u8 revision, int target,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800305 int *err)
306{
307 int have_rev, best = -1;
308
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800309 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800310 *err = -EINTR;
311 return 1;
312 }
313 if (target == 1)
314 have_rev = target_revfn(af, name, revision, &best);
315 else
316 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800317 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800318
319 /* Nothing at all? Return 0 to try loading module. */
320 if (best == -1) {
321 *err = -ENOENT;
322 return 0;
323 }
324
325 *err = best;
326 if (!have_rev)
327 *err = -EPROTONOSUPPORT;
328 return 1;
329}
330EXPORT_SYMBOL_GPL(xt_find_revision);
331
Jan Engelhardt45185362009-04-05 10:43:09 +0200332static char *textify_hooks(char *buf, size_t size, unsigned int mask)
333{
334 static const char *const names[] = {
335 "PREROUTING", "INPUT", "FORWARD",
336 "OUTPUT", "POSTROUTING", "BROUTING",
337 };
338 unsigned int i;
339 char *p = buf;
340 bool np = false;
341 int res;
342
343 *p = '\0';
344 for (i = 0; i < ARRAY_SIZE(names); ++i) {
345 if (!(mask & (1 << i)))
346 continue;
347 res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
348 if (res > 0) {
349 size -= res;
350 p += res;
351 }
352 np = true;
353 }
354
355 return buf;
356}
357
Jan Engelhardt916a9172008-10-08 11:35:20 +0200358int xt_check_match(struct xt_mtchk_param *par,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200359 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800360{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200361 if (XT_ALIGN(par->match->matchsize) != size &&
362 par->match->matchsize != -1) {
Jan Engelhardt043ef462008-10-08 11:35:15 +0200363 /*
364 * ebt_among is exempt from centralized matchsize checking
365 * because it uses a dynamic-size data set.
366 */
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200367 pr_err("%s_tables: %s match: invalid size %Zu != %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200368 xt_prefix[par->family], par->match->name,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200369 XT_ALIGN(par->match->matchsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800370 return -EINVAL;
371 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200372 if (par->match->table != NULL &&
373 strcmp(par->match->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200374 pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200375 xt_prefix[par->family], par->match->name,
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200376 par->match->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800377 return -EINVAL;
378 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200379 if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200380 char used[64], allow[64];
381
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200382 pr_err("%s_tables: %s match: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200383 "valid from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200384 xt_prefix[par->family], par->match->name,
Jan Engelhardt45185362009-04-05 10:43:09 +0200385 textify_hooks(used, sizeof(used), par->hook_mask),
386 textify_hooks(allow, sizeof(allow), par->match->hooks));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800387 return -EINVAL;
388 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200389 if (par->match->proto && (par->match->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200390 pr_err("%s_tables: %s match: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200391 xt_prefix[par->family], par->match->name,
392 par->match->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800393 return -EINVAL;
394 }
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200395 if (par->match->checkentry != NULL && !par->match->checkentry(par))
Jan Engelhardt367c6792008-10-08 11:35:17 +0200396 return -EINVAL;
Patrick McHardy37f9f732006-03-20 17:59:06 -0800397 return 0;
398}
399EXPORT_SYMBOL_GPL(xt_check_match);
400
Dmitry Mishin27229712006-04-01 02:25:19 -0800401#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200402int xt_compat_add_offset(u_int8_t af, unsigned int offset, short delta)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800403{
404 struct compat_delta *tmp;
405
406 tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
407 if (!tmp)
408 return -ENOMEM;
409
410 tmp->offset = offset;
411 tmp->delta = delta;
412
413 if (xt[af].compat_offsets) {
414 tmp->next = xt[af].compat_offsets->next;
415 xt[af].compat_offsets->next = tmp;
416 } else {
417 xt[af].compat_offsets = tmp;
418 tmp->next = NULL;
419 }
420 return 0;
421}
422EXPORT_SYMBOL_GPL(xt_compat_add_offset);
423
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200424void xt_compat_flush_offsets(u_int8_t af)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800425{
426 struct compat_delta *tmp, *next;
427
428 if (xt[af].compat_offsets) {
429 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
430 next = tmp->next;
431 kfree(tmp);
432 }
433 xt[af].compat_offsets = NULL;
434 }
435}
436EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
437
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200438short xt_compat_calc_jump(u_int8_t af, unsigned int offset)
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800439{
440 struct compat_delta *tmp;
441 short delta;
442
443 for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
444 if (tmp->offset < offset)
445 delta += tmp->delta;
446 return delta;
447}
448EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
449
Jan Engelhardt5452e422008-04-14 11:15:35 +0200450int xt_compat_match_offset(const struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800451{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700452 u_int16_t csize = match->compatsize ? : match->matchsize;
453 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800454}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700455EXPORT_SYMBOL_GPL(xt_compat_match_offset);
456
Patrick McHardy89566952007-12-17 21:46:40 -0800457int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800458 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700459{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200460 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700461 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
462 int pad, off = xt_compat_match_offset(match);
463 u_int16_t msize = cm->u.user.match_size;
464
465 m = *dstptr;
466 memcpy(m, cm, sizeof(*cm));
467 if (match->compat_from_user)
468 match->compat_from_user(m->data, cm->data);
469 else
470 memcpy(m->data, cm->data, msize - sizeof(*cm));
471 pad = XT_ALIGN(match->matchsize) - match->matchsize;
472 if (pad > 0)
473 memset(m->data + match->matchsize, 0, pad);
474
475 msize += off;
476 m->u.user.match_size = msize;
477
478 *size += off;
479 *dstptr += msize;
Patrick McHardy89566952007-12-17 21:46:40 -0800480 return 0;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700481}
482EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
483
484int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800485 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700486{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200487 const struct xt_match *match = m->u.kernel.match;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700488 struct compat_xt_entry_match __user *cm = *dstptr;
489 int off = xt_compat_match_offset(match);
490 u_int16_t msize = m->u.user.match_size - off;
491
492 if (copy_to_user(cm, m, sizeof(*cm)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800493 put_user(msize, &cm->u.user.match_size) ||
494 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
495 strlen(m->u.kernel.match->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800496 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700497
498 if (match->compat_to_user) {
499 if (match->compat_to_user((void __user *)cm->data, m->data))
500 return -EFAULT;
501 } else {
502 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
503 return -EFAULT;
504 }
505
506 *size -= off;
507 *dstptr += msize;
508 return 0;
509}
510EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
511#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800512
Jan Engelhardt916a9172008-10-08 11:35:20 +0200513int xt_check_target(struct xt_tgchk_param *par,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200514 unsigned int size, u_int8_t proto, bool inv_proto)
Patrick McHardy37f9f732006-03-20 17:59:06 -0800515{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200516 if (XT_ALIGN(par->target->targetsize) != size) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200517 pr_err("%s_tables: %s target: invalid size %Zu != %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200518 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200519 XT_ALIGN(par->target->targetsize), size);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800520 return -EINVAL;
521 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200522 if (par->target->table != NULL &&
523 strcmp(par->target->table, par->table) != 0) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200524 pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200525 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200526 par->target->table, par->table);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800527 return -EINVAL;
528 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200529 if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
Jan Engelhardt45185362009-04-05 10:43:09 +0200530 char used[64], allow[64];
531
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200532 pr_err("%s_tables: %s target: used from hooks %s, but only "
Jan Engelhardt45185362009-04-05 10:43:09 +0200533 "usable from %s\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200534 xt_prefix[par->family], par->target->name,
Jan Engelhardt45185362009-04-05 10:43:09 +0200535 textify_hooks(used, sizeof(used), par->hook_mask),
536 textify_hooks(allow, sizeof(allow), par->target->hooks));
Patrick McHardy37f9f732006-03-20 17:59:06 -0800537 return -EINVAL;
538 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200539 if (par->target->proto && (par->target->proto != proto || inv_proto)) {
Joe Perches3dd5d7e2009-06-13 12:32:39 +0200540 pr_err("%s_tables: %s target: only valid for protocol %u\n",
Jan Engelhardt916a9172008-10-08 11:35:20 +0200541 xt_prefix[par->family], par->target->name,
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200542 par->target->proto);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800543 return -EINVAL;
544 }
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200545 if (par->target->checkentry != NULL && !par->target->checkentry(par))
Jan Engelhardt367c6792008-10-08 11:35:17 +0200546 return -EINVAL;
Patrick McHardy37f9f732006-03-20 17:59:06 -0800547 return 0;
548}
549EXPORT_SYMBOL_GPL(xt_check_target);
550
Dmitry Mishin27229712006-04-01 02:25:19 -0800551#ifdef CONFIG_COMPAT
Jan Engelhardt5452e422008-04-14 11:15:35 +0200552int xt_compat_target_offset(const struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800553{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700554 u_int16_t csize = target->compatsize ? : target->targetsize;
555 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800556}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700557EXPORT_SYMBOL_GPL(xt_compat_target_offset);
558
559void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800560 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700561{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200562 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700563 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
564 int pad, off = xt_compat_target_offset(target);
565 u_int16_t tsize = ct->u.user.target_size;
566
567 t = *dstptr;
568 memcpy(t, ct, sizeof(*ct));
569 if (target->compat_from_user)
570 target->compat_from_user(t->data, ct->data);
571 else
572 memcpy(t->data, ct->data, tsize - sizeof(*ct));
573 pad = XT_ALIGN(target->targetsize) - target->targetsize;
574 if (pad > 0)
575 memset(t->data + target->targetsize, 0, pad);
576
577 tsize += off;
578 t->u.user.target_size = tsize;
579
580 *size += off;
581 *dstptr += tsize;
582}
583EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
584
585int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr,
Patrick McHardyb0a63632008-01-31 04:10:18 -0800586 unsigned int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700587{
Jan Engelhardt5452e422008-04-14 11:15:35 +0200588 const struct xt_target *target = t->u.kernel.target;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700589 struct compat_xt_entry_target __user *ct = *dstptr;
590 int off = xt_compat_target_offset(target);
591 u_int16_t tsize = t->u.user.target_size - off;
592
593 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800594 put_user(tsize, &ct->u.user.target_size) ||
595 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
596 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800597 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700598
599 if (target->compat_to_user) {
600 if (target->compat_to_user((void __user *)ct->data, t->data))
601 return -EFAULT;
602 } else {
603 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
604 return -EFAULT;
605 }
606
607 *size -= off;
608 *dstptr += tsize;
609 return 0;
610}
611EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -0800612#endif
613
Harald Welte2e4e6a12006-01-12 13:30:04 -0800614struct xt_table_info *xt_alloc_table_info(unsigned int size)
615{
616 struct xt_table_info *newinfo;
617 int cpu;
618
619 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
Jan Beulich44813742009-09-21 17:03:05 -0700620 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > totalram_pages)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800621 return NULL;
622
Eric Dumazet259d4e42007-12-04 23:24:56 -0800623 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800624 if (!newinfo)
625 return NULL;
626
627 newinfo->size = size;
628
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700629 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800630 if (size <= PAGE_SIZE)
631 newinfo->entries[cpu] = kmalloc_node(size,
632 GFP_KERNEL,
633 cpu_to_node(cpu));
634 else
635 newinfo->entries[cpu] = vmalloc_node(size,
636 cpu_to_node(cpu));
637
638 if (newinfo->entries[cpu] == NULL) {
639 xt_free_table_info(newinfo);
640 return NULL;
641 }
642 }
643
644 return newinfo;
645}
646EXPORT_SYMBOL(xt_alloc_table_info);
647
648void xt_free_table_info(struct xt_table_info *info)
649{
650 int cpu;
651
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700652 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800653 if (info->size <= PAGE_SIZE)
654 kfree(info->entries[cpu]);
655 else
656 vfree(info->entries[cpu]);
657 }
658 kfree(info);
659}
660EXPORT_SYMBOL(xt_free_table_info);
661
662/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200663struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
664 const char *name)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800665{
666 struct xt_table *t;
667
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800668 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800669 return ERR_PTR(-EINTR);
670
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800671 list_for_each_entry(t, &net->xt.tables[af], list)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800672 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
673 return t;
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800674 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800675 return NULL;
676}
677EXPORT_SYMBOL_GPL(xt_find_table_lock);
678
679void xt_table_unlock(struct xt_table *table)
680{
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800681 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800682}
683EXPORT_SYMBOL_GPL(xt_table_unlock);
684
Dmitry Mishin27229712006-04-01 02:25:19 -0800685#ifdef CONFIG_COMPAT
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200686void xt_compat_lock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800687{
688 mutex_lock(&xt[af].compat_mutex);
689}
690EXPORT_SYMBOL_GPL(xt_compat_lock);
691
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200692void xt_compat_unlock(u_int8_t af)
Dmitry Mishin27229712006-04-01 02:25:19 -0800693{
694 mutex_unlock(&xt[af].compat_mutex);
695}
696EXPORT_SYMBOL_GPL(xt_compat_unlock);
697#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800698
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700699DEFINE_PER_CPU(struct xt_info_lock, xt_info_locks);
700EXPORT_PER_CPU_SYMBOL_GPL(xt_info_locks);
701
702
Harald Welte2e4e6a12006-01-12 13:30:04 -0800703struct xt_table_info *
704xt_replace_table(struct xt_table *table,
705 unsigned int num_counters,
706 struct xt_table_info *newinfo,
707 int *error)
708{
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700709 struct xt_table_info *private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800710
711 /* Do the substitution. */
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700712 local_bh_disable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800713 private = table->private;
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700714
Harald Welte2e4e6a12006-01-12 13:30:04 -0800715 /* Check inside lock: is the old number correct? */
716 if (num_counters != private->number) {
717 duprintf("num_counters != table->private->number (%u/%u)\n",
718 num_counters, private->number);
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700719 local_bh_enable();
Harald Welte2e4e6a12006-01-12 13:30:04 -0800720 *error = -EAGAIN;
721 return NULL;
722 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800723
Stephen Hemminger942e4a22009-04-28 22:36:33 -0700724 table->private = newinfo;
725 newinfo->initial_entries = private->initial_entries;
726
727 /*
728 * Even though table entries have now been swapped, other CPU's
729 * may still be using the old entries. This is okay, because
730 * resynchronization happens because of the locking done
731 * during the get_counters() routine.
732 */
733 local_bh_enable();
734
735 return private;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800736}
737EXPORT_SYMBOL_GPL(xt_replace_table);
738
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200739struct xt_table *xt_register_table(struct net *net,
740 const struct xt_table *input_table,
Alexey Dobriyana98da112008-01-31 04:01:49 -0800741 struct xt_table_info *bootstrap,
742 struct xt_table_info *newinfo)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800743{
744 int ret;
745 struct xt_table_info *private;
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200746 struct xt_table *t, *table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800747
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800748 /* Don't add one object to multiple lists. */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +0200749 table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800750 if (!table) {
751 ret = -ENOMEM;
752 goto out;
753 }
754
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800755 ret = mutex_lock_interruptible(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800756 if (ret != 0)
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800757 goto out_free;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800758
759 /* Don't autoload: we'd eat our tail... */
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800760 list_for_each_entry(t, &net->xt.tables[table->af], list) {
Patrick McHardydf0933d2006-09-20 11:57:53 -0700761 if (strcmp(t->name, table->name) == 0) {
762 ret = -EEXIST;
763 goto unlock;
764 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800765 }
766
767 /* Simplifies replace_table code. */
768 table->private = bootstrap;
Stephen Hemminger78454472009-02-20 10:35:32 +0100769
Harald Welte2e4e6a12006-01-12 13:30:04 -0800770 if (!xt_replace_table(table, 0, newinfo, &ret))
771 goto unlock;
772
773 private = table->private;
774 duprintf("table->private->number = %u\n", private->number);
775
776 /* save number of initial entries */
777 private->initial_entries = private->number;
778
Alexey Dobriyan8d870052008-01-31 04:02:13 -0800779 list_add(&table->list, &net->xt.tables[table->af]);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800780 mutex_unlock(&xt[table->af].mutex);
781 return table;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800782
Harald Welte2e4e6a12006-01-12 13:30:04 -0800783 unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800784 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800785out_free:
786 kfree(table);
Alexey Dobriyana98da112008-01-31 04:01:49 -0800787out:
788 return ERR_PTR(ret);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800789}
790EXPORT_SYMBOL_GPL(xt_register_table);
791
792void *xt_unregister_table(struct xt_table *table)
793{
794 struct xt_table_info *private;
795
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800796 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800797 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700798 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800799 mutex_unlock(&xt[table->af].mutex);
Alexey Dobriyan44d34e72008-01-31 04:02:44 -0800800 kfree(table);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800801
802 return private;
803}
804EXPORT_SYMBOL_GPL(xt_unregister_table);
805
806#ifdef CONFIG_PROC_FS
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800807struct xt_names_priv {
808 struct seq_net_private p;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200809 u_int8_t af;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800810};
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800811static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800812{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800813 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900814 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200815 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800816
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800817 mutex_lock(&xt[af].mutex);
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800818 return seq_list_start(&net->xt.tables[af], *pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800819}
820
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800821static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800822{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800823 struct xt_names_priv *priv = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +0900824 struct net *net = seq_file_net(seq);
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200825 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800826
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800827 return seq_list_next(v, &net->xt.tables[af], pos);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800828}
829
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800830static void xt_table_seq_stop(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800831{
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800832 struct xt_names_priv *priv = seq->private;
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200833 u_int8_t af = priv->af;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800834
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800835 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800836}
837
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800838static int xt_table_seq_show(struct seq_file *seq, void *v)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800839{
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800840 struct xt_table *table = list_entry(v, struct xt_table, list);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800841
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800842 if (strlen(table->name))
843 return seq_printf(seq, "%s\n", table->name);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800844 else
845 return 0;
846}
847
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800848static const struct seq_operations xt_table_seq_ops = {
849 .start = xt_table_seq_start,
850 .next = xt_table_seq_next,
851 .stop = xt_table_seq_stop,
852 .show = xt_table_seq_show,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800853};
854
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800855static int xt_table_open(struct inode *inode, struct file *file)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800856{
857 int ret;
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800858 struct xt_names_priv *priv;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800859
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800860 ret = seq_open_net(inode, file, &xt_table_seq_ops,
861 sizeof(struct xt_names_priv));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800862 if (!ret) {
Alexey Dobriyan715cf352008-01-31 04:49:16 -0800863 priv = ((struct seq_file *)file->private_data)->private;
864 priv->af = (unsigned long)PDE(inode)->data;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800865 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800866 return ret;
867}
868
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800869static const struct file_operations xt_table_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800870 .owner = THIS_MODULE,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800871 .open = xt_table_open,
872 .read = seq_read,
873 .llseek = seq_lseek,
Pavel Emelyanov0e93bb92008-04-29 03:15:35 -0700874 .release = seq_release_net,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800875};
876
Jan Engelhardteb132202009-02-18 16:42:19 +0100877/*
878 * Traverse state for ip{,6}_{tables,matches} for helping crossing
879 * the multi-AF mutexes.
880 */
881struct nf_mttg_trav {
882 struct list_head *head, *curr;
883 uint8_t class, nfproto;
884};
885
886enum {
887 MTTG_TRAV_INIT,
888 MTTG_TRAV_NFP_UNSPEC,
889 MTTG_TRAV_NFP_SPEC,
890 MTTG_TRAV_DONE,
891};
892
893static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
894 bool is_target)
895{
896 static const uint8_t next_class[] = {
897 [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
898 [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
899 };
900 struct nf_mttg_trav *trav = seq->private;
901
902 switch (trav->class) {
903 case MTTG_TRAV_INIT:
904 trav->class = MTTG_TRAV_NFP_UNSPEC;
905 mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
906 trav->head = trav->curr = is_target ?
907 &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
908 break;
909 case MTTG_TRAV_NFP_UNSPEC:
910 trav->curr = trav->curr->next;
911 if (trav->curr != trav->head)
912 break;
913 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
914 mutex_lock(&xt[trav->nfproto].mutex);
915 trav->head = trav->curr = is_target ?
916 &xt[trav->nfproto].target : &xt[trav->nfproto].match;
917 trav->class = next_class[trav->class];
918 break;
919 case MTTG_TRAV_NFP_SPEC:
920 trav->curr = trav->curr->next;
921 if (trav->curr != trav->head)
922 break;
923 /* fallthru, _stop will unlock */
924 default:
925 return NULL;
926 }
927
928 if (ppos != NULL)
929 ++*ppos;
930 return trav;
931}
932
933static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
934 bool is_target)
935{
936 struct nf_mttg_trav *trav = seq->private;
937 unsigned int j;
938
939 trav->class = MTTG_TRAV_INIT;
940 for (j = 0; j < *pos; ++j)
941 if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
942 return NULL;
943 return trav;
944}
945
946static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
947{
948 struct nf_mttg_trav *trav = seq->private;
949
950 switch (trav->class) {
951 case MTTG_TRAV_NFP_UNSPEC:
952 mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
953 break;
954 case MTTG_TRAV_NFP_SPEC:
955 mutex_unlock(&xt[trav->nfproto].mutex);
956 break;
957 }
958}
959
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800960static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
961{
Jan Engelhardteb132202009-02-18 16:42:19 +0100962 return xt_mttg_seq_start(seq, pos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800963}
964
Jan Engelhardteb132202009-02-18 16:42:19 +0100965static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800966{
Jan Engelhardteb132202009-02-18 16:42:19 +0100967 return xt_mttg_seq_next(seq, v, ppos, false);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800968}
969
970static int xt_match_seq_show(struct seq_file *seq, void *v)
971{
Jan Engelhardteb132202009-02-18 16:42:19 +0100972 const struct nf_mttg_trav *trav = seq->private;
973 const struct xt_match *match;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800974
Jan Engelhardteb132202009-02-18 16:42:19 +0100975 switch (trav->class) {
976 case MTTG_TRAV_NFP_UNSPEC:
977 case MTTG_TRAV_NFP_SPEC:
978 if (trav->curr == trav->head)
979 return 0;
980 match = list_entry(trav->curr, struct xt_match, list);
981 return (*match->name == '\0') ? 0 :
982 seq_printf(seq, "%s\n", match->name);
983 }
984 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800985}
986
987static const struct seq_operations xt_match_seq_ops = {
988 .start = xt_match_seq_start,
989 .next = xt_match_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +0100990 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800991 .show = xt_match_seq_show,
992};
993
994static int xt_match_open(struct inode *inode, struct file *file)
995{
Jan Engelhardteb132202009-02-18 16:42:19 +0100996 struct seq_file *seq;
997 struct nf_mttg_trav *trav;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -0800998 int ret;
999
Jan Engelhardteb132202009-02-18 16:42:19 +01001000 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1001 if (trav == NULL)
1002 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001003
Jan Engelhardteb132202009-02-18 16:42:19 +01001004 ret = seq_open(file, &xt_match_seq_ops);
1005 if (ret < 0) {
1006 kfree(trav);
1007 return ret;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001008 }
Jan Engelhardteb132202009-02-18 16:42:19 +01001009
1010 seq = file->private_data;
1011 seq->private = trav;
1012 trav->nfproto = (unsigned long)PDE(inode)->data;
1013 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001014}
1015
1016static const struct file_operations xt_match_ops = {
1017 .owner = THIS_MODULE,
1018 .open = xt_match_open,
1019 .read = seq_read,
1020 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001021 .release = seq_release_private,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001022};
1023
1024static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
1025{
Jan Engelhardteb132202009-02-18 16:42:19 +01001026 return xt_mttg_seq_start(seq, pos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001027}
1028
Jan Engelhardteb132202009-02-18 16:42:19 +01001029static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001030{
Jan Engelhardteb132202009-02-18 16:42:19 +01001031 return xt_mttg_seq_next(seq, v, ppos, true);
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001032}
1033
1034static int xt_target_seq_show(struct seq_file *seq, void *v)
1035{
Jan Engelhardteb132202009-02-18 16:42:19 +01001036 const struct nf_mttg_trav *trav = seq->private;
1037 const struct xt_target *target;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001038
Jan Engelhardteb132202009-02-18 16:42:19 +01001039 switch (trav->class) {
1040 case MTTG_TRAV_NFP_UNSPEC:
1041 case MTTG_TRAV_NFP_SPEC:
1042 if (trav->curr == trav->head)
1043 return 0;
1044 target = list_entry(trav->curr, struct xt_target, list);
1045 return (*target->name == '\0') ? 0 :
1046 seq_printf(seq, "%s\n", target->name);
1047 }
1048 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001049}
1050
1051static const struct seq_operations xt_target_seq_ops = {
1052 .start = xt_target_seq_start,
1053 .next = xt_target_seq_next,
Jan Engelhardteb132202009-02-18 16:42:19 +01001054 .stop = xt_mttg_seq_stop,
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001055 .show = xt_target_seq_show,
1056};
1057
1058static int xt_target_open(struct inode *inode, struct file *file)
1059{
Jan Engelhardteb132202009-02-18 16:42:19 +01001060 struct seq_file *seq;
1061 struct nf_mttg_trav *trav;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001062 int ret;
1063
Jan Engelhardteb132202009-02-18 16:42:19 +01001064 trav = kmalloc(sizeof(*trav), GFP_KERNEL);
1065 if (trav == NULL)
1066 return -ENOMEM;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001067
Jan Engelhardteb132202009-02-18 16:42:19 +01001068 ret = seq_open(file, &xt_target_seq_ops);
1069 if (ret < 0) {
1070 kfree(trav);
1071 return ret;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001072 }
Jan Engelhardteb132202009-02-18 16:42:19 +01001073
1074 seq = file->private_data;
1075 seq->private = trav;
1076 trav->nfproto = (unsigned long)PDE(inode)->data;
1077 return 0;
Alexey Dobriyan025d93d2008-01-31 04:48:54 -08001078}
1079
1080static const struct file_operations xt_target_ops = {
1081 .owner = THIS_MODULE,
1082 .open = xt_target_open,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001083 .read = seq_read,
1084 .llseek = seq_lseek,
Jan Engelhardteb132202009-02-18 16:42:19 +01001085 .release = seq_release_private,
Harald Welte2e4e6a12006-01-12 13:30:04 -08001086};
1087
1088#define FORMAT_TABLES "_tables_names"
1089#define FORMAT_MATCHES "_tables_matches"
1090#define FORMAT_TARGETS "_tables_targets"
1091
1092#endif /* CONFIG_PROC_FS */
1093
Jan Engelhardt2b95efe2009-06-17 13:57:48 +02001094/**
1095 * xt_hook_link - set up hooks for a new table
1096 * @table: table with metadata needed to set up hooks
1097 * @fn: Hook function
1098 *
1099 * This function will take care of creating and registering the necessary
1100 * Netfilter hooks for XT tables.
1101 */
1102struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1103{
1104 unsigned int hook_mask = table->valid_hooks;
1105 uint8_t i, num_hooks = hweight32(hook_mask);
1106 uint8_t hooknum;
1107 struct nf_hook_ops *ops;
1108 int ret;
1109
1110 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
1111 if (ops == NULL)
1112 return ERR_PTR(-ENOMEM);
1113
1114 for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
1115 hook_mask >>= 1, ++hooknum) {
1116 if (!(hook_mask & 1))
1117 continue;
1118 ops[i].hook = fn;
1119 ops[i].owner = table->me;
1120 ops[i].pf = table->af;
1121 ops[i].hooknum = hooknum;
1122 ops[i].priority = table->priority;
1123 ++i;
1124 }
1125
1126 ret = nf_register_hooks(ops, num_hooks);
1127 if (ret < 0) {
1128 kfree(ops);
1129 return ERR_PTR(ret);
1130 }
1131
1132 return ops;
1133}
1134EXPORT_SYMBOL_GPL(xt_hook_link);
1135
1136/**
1137 * xt_hook_unlink - remove hooks for a table
1138 * @ops: nf_hook_ops array as returned by nf_hook_link
1139 * @hook_mask: the very same mask that was passed to nf_hook_link
1140 */
1141void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1142{
1143 nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1144 kfree(ops);
1145}
1146EXPORT_SYMBOL_GPL(xt_hook_unlink);
1147
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001148int xt_proto_init(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001149{
1150#ifdef CONFIG_PROC_FS
1151 char buf[XT_FUNCTION_MAXNAMELEN];
1152 struct proc_dir_entry *proc;
1153#endif
1154
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001155 if (af >= ARRAY_SIZE(xt_prefix))
Harald Welte2e4e6a12006-01-12 13:30:04 -08001156 return -EINVAL;
1157
1158
1159#ifdef CONFIG_PROC_FS
Tobias Klauserce18afe2007-03-14 16:36:16 -07001160 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001161 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001162 proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
1163 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001164 if (!proc)
1165 goto out;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001166
Tobias Klauserce18afe2007-03-14 16:36:16 -07001167 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001168 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001169 proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
1170 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001171 if (!proc)
1172 goto out_remove_tables;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001173
Tobias Klauserce18afe2007-03-14 16:36:16 -07001174 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001175 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Denis V. Lunev8b169242008-05-02 04:11:52 -07001176 proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
1177 (void *)(unsigned long)af);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001178 if (!proc)
1179 goto out_remove_matches;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001180#endif
1181
1182 return 0;
1183
1184#ifdef CONFIG_PROC_FS
1185out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001186 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001187 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001188 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001189
1190out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -07001191 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001192 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001193 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001194out:
1195 return -1;
1196#endif
1197}
1198EXPORT_SYMBOL_GPL(xt_proto_init);
1199
Jan Engelhardt76108ce2008-10-08 11:35:00 +02001200void xt_proto_fini(struct net *net, u_int8_t af)
Harald Welte2e4e6a12006-01-12 13:30:04 -08001201{
1202#ifdef CONFIG_PROC_FS
1203 char buf[XT_FUNCTION_MAXNAMELEN];
1204
Tobias Klauserce18afe2007-03-14 16:36:16 -07001205 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001206 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001207 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001208
Tobias Klauserce18afe2007-03-14 16:36:16 -07001209 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001210 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001211 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001212
Tobias Klauserce18afe2007-03-14 16:36:16 -07001213 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -08001214 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Alexey Dobriyan3cb609d2008-01-31 04:49:35 -08001215 proc_net_remove(net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001216#endif /*CONFIG_PROC_FS*/
1217}
1218EXPORT_SYMBOL_GPL(xt_proto_fini);
1219
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001220static int __net_init xt_net_init(struct net *net)
1221{
1222 int i;
1223
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001224 for (i = 0; i < NFPROTO_NUMPROTO; i++)
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001225 INIT_LIST_HEAD(&net->xt.tables[i]);
1226 return 0;
1227}
1228
1229static struct pernet_operations xt_net_ops = {
1230 .init = xt_net_init,
1231};
Harald Welte2e4e6a12006-01-12 13:30:04 -08001232
1233static int __init xt_init(void)
1234{
Stephen Hemminger942e4a22009-04-28 22:36:33 -07001235 unsigned int i;
1236 int rv;
1237
1238 for_each_possible_cpu(i) {
1239 struct xt_info_lock *lock = &per_cpu(xt_info_locks, i);
1240 spin_lock_init(&lock->lock);
1241 lock->readers = 0;
1242 }
Harald Welte2e4e6a12006-01-12 13:30:04 -08001243
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001244 xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001245 if (!xt)
1246 return -ENOMEM;
1247
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +02001248 for (i = 0; i < NFPROTO_NUMPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -08001249 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -08001250#ifdef CONFIG_COMPAT
1251 mutex_init(&xt[i].compat_mutex);
Patrick McHardyb386d9f2007-12-17 21:47:48 -08001252 xt[i].compat_offsets = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -08001253#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -08001254 INIT_LIST_HEAD(&xt[i].target);
1255 INIT_LIST_HEAD(&xt[i].match);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001256 }
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001257 rv = register_pernet_subsys(&xt_net_ops);
1258 if (rv < 0)
1259 kfree(xt);
1260 return rv;
Harald Welte2e4e6a12006-01-12 13:30:04 -08001261}
1262
1263static void __exit xt_fini(void)
1264{
Alexey Dobriyan8d870052008-01-31 04:02:13 -08001265 unregister_pernet_subsys(&xt_net_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -08001266 kfree(xt);
1267}
1268
1269module_init(xt_init);
1270module_exit(xt_fini);
1271