blob: 8d4fca96a4a719a054ee3ac0cb02643dc90113ba [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>");
33MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module");
34
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;
47 struct list_head tables;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080048#ifdef CONFIG_COMPAT
Dmitry Mishin27229712006-04-01 02:25:19 -080049 struct mutex compat_mutex;
Patrick McHardyb386d9f2007-12-17 21:47:48 -080050 struct compat_delta *compat_offsets;
51#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -080052};
53
54static struct xt_af *xt;
55
56#ifdef DEBUG_IP_FIREWALL_USER
57#define duprintf(format, args...) printk(format , ## args)
58#else
59#define duprintf(format, args...)
60#endif
61
62enum {
63 TABLE,
64 TARGET,
65 MATCH,
66};
67
Patrick McHardy37f9f732006-03-20 17:59:06 -080068static const char *xt_prefix[NPROTO] = {
Tobias Klauserce18afe2007-03-14 16:36:16 -070069 [AF_INET] = "ip",
70 [AF_INET6] = "ip6",
Patrick McHardy37f9f732006-03-20 17:59:06 -080071 [NF_ARP] = "arp",
72};
73
Harald Welte2e4e6a12006-01-12 13:30:04 -080074/* Registration hooks for targets. */
75int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080076xt_register_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080077{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080078 int ret, af = target->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -080079
Ingo Molnar9e19bb62006-03-25 01:41:10 -080080 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080081 if (ret != 0)
82 return ret;
83 list_add(&target->list, &xt[af].target);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080084 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080085 return ret;
86}
87EXPORT_SYMBOL(xt_register_target);
88
89void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080090xt_unregister_target(struct xt_target *target)
Harald Welte2e4e6a12006-01-12 13:30:04 -080091{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -080092 int af = target->family;
93
Ingo Molnar9e19bb62006-03-25 01:41:10 -080094 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -070095 list_del(&target->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -080096 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -080097}
98EXPORT_SYMBOL(xt_unregister_target);
99
100int
Patrick McHardy52d9c422006-08-22 00:33:45 -0700101xt_register_targets(struct xt_target *target, unsigned int n)
102{
103 unsigned int i;
104 int err = 0;
105
106 for (i = 0; i < n; i++) {
107 err = xt_register_target(&target[i]);
108 if (err)
109 goto err;
110 }
111 return err;
112
113err:
114 if (i > 0)
115 xt_unregister_targets(target, i);
116 return err;
117}
118EXPORT_SYMBOL(xt_register_targets);
119
120void
121xt_unregister_targets(struct xt_target *target, unsigned int n)
122{
123 unsigned int i;
124
125 for (i = 0; i < n; i++)
126 xt_unregister_target(&target[i]);
127}
128EXPORT_SYMBOL(xt_unregister_targets);
129
130int
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800131xt_register_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800132{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800133 int ret, af = match->family;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800134
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800135 ret = mutex_lock_interruptible(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800136 if (ret != 0)
137 return ret;
138
139 list_add(&match->list, &xt[af].match);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800140 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800141
142 return ret;
143}
144EXPORT_SYMBOL(xt_register_match);
145
146void
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800147xt_unregister_match(struct xt_match *match)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800148{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800149 int af = match->family;
150
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800151 mutex_lock(&xt[af].mutex);
Patrick McHardydf0933d2006-09-20 11:57:53 -0700152 list_del(&match->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800153 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800154}
155EXPORT_SYMBOL(xt_unregister_match);
156
Patrick McHardy52d9c422006-08-22 00:33:45 -0700157int
158xt_register_matches(struct xt_match *match, unsigned int n)
159{
160 unsigned int i;
161 int err = 0;
162
163 for (i = 0; i < n; i++) {
164 err = xt_register_match(&match[i]);
165 if (err)
166 goto err;
167 }
168 return err;
169
170err:
171 if (i > 0)
172 xt_unregister_matches(match, i);
173 return err;
174}
175EXPORT_SYMBOL(xt_register_matches);
176
177void
178xt_unregister_matches(struct xt_match *match, unsigned int n)
179{
180 unsigned int i;
181
182 for (i = 0; i < n; i++)
183 xt_unregister_match(&match[i]);
184}
185EXPORT_SYMBOL(xt_unregister_matches);
186
Harald Welte2e4e6a12006-01-12 13:30:04 -0800187
188/*
189 * These are weird, but module loading must not be done with mutex
190 * held (since they will register), and we have to have a single
191 * function to use try_then_request_module().
192 */
193
194/* Find match, grabs ref. Returns ERR_PTR() on error. */
195struct xt_match *xt_find_match(int af, const char *name, u8 revision)
196{
197 struct xt_match *m;
198 int err = 0;
199
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800200 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800201 return ERR_PTR(-EINTR);
202
203 list_for_each_entry(m, &xt[af].match, list) {
204 if (strcmp(m->name, name) == 0) {
205 if (m->revision == revision) {
206 if (try_module_get(m->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800207 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800208 return m;
209 }
210 } else
211 err = -EPROTOTYPE; /* Found something. */
212 }
213 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800214 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800215 return ERR_PTR(err);
216}
217EXPORT_SYMBOL(xt_find_match);
218
219/* Find target, grabs ref. Returns ERR_PTR() on error. */
220struct xt_target *xt_find_target(int af, const char *name, u8 revision)
221{
222 struct xt_target *t;
223 int err = 0;
224
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800225 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800226 return ERR_PTR(-EINTR);
227
228 list_for_each_entry(t, &xt[af].target, list) {
229 if (strcmp(t->name, name) == 0) {
230 if (t->revision == revision) {
231 if (try_module_get(t->me)) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800232 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800233 return t;
234 }
235 } else
236 err = -EPROTOTYPE; /* Found something. */
237 }
238 }
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800239 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800240 return ERR_PTR(err);
241}
242EXPORT_SYMBOL(xt_find_target);
243
Harald Welte2e4e6a12006-01-12 13:30:04 -0800244struct xt_target *xt_request_find_target(int af, const char *name, u8 revision)
245{
246 struct xt_target *target;
247
248 target = try_then_request_module(xt_find_target(af, name, revision),
Patrick McHardy37f9f732006-03-20 17:59:06 -0800249 "%st_%s", xt_prefix[af], name);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800250 if (IS_ERR(target) || !target)
251 return NULL;
252 return target;
253}
254EXPORT_SYMBOL_GPL(xt_request_find_target);
255
256static int match_revfn(int af, const char *name, u8 revision, int *bestp)
257{
258 struct xt_match *m;
259 int have_rev = 0;
260
261 list_for_each_entry(m, &xt[af].match, list) {
262 if (strcmp(m->name, name) == 0) {
263 if (m->revision > *bestp)
264 *bestp = m->revision;
265 if (m->revision == revision)
266 have_rev = 1;
267 }
268 }
269 return have_rev;
270}
271
272static int target_revfn(int af, const char *name, u8 revision, int *bestp)
273{
274 struct xt_target *t;
275 int have_rev = 0;
276
277 list_for_each_entry(t, &xt[af].target, list) {
278 if (strcmp(t->name, name) == 0) {
279 if (t->revision > *bestp)
280 *bestp = t->revision;
281 if (t->revision == revision)
282 have_rev = 1;
283 }
284 }
285 return have_rev;
286}
287
288/* Returns true or false (if no such extension at all) */
289int xt_find_revision(int af, const char *name, u8 revision, int target,
290 int *err)
291{
292 int have_rev, best = -1;
293
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800294 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800295 *err = -EINTR;
296 return 1;
297 }
298 if (target == 1)
299 have_rev = target_revfn(af, name, revision, &best);
300 else
301 have_rev = match_revfn(af, name, revision, &best);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800302 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800303
304 /* Nothing at all? Return 0 to try loading module. */
305 if (best == -1) {
306 *err = -ENOENT;
307 return 0;
308 }
309
310 *err = best;
311 if (!have_rev)
312 *err = -EPROTONOSUPPORT;
313 return 1;
314}
315EXPORT_SYMBOL_GPL(xt_find_revision);
316
Patrick McHardy37f9f732006-03-20 17:59:06 -0800317int xt_check_match(const struct xt_match *match, unsigned short family,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800318 unsigned int size, const char *table, unsigned int hook_mask,
Patrick McHardy37f9f732006-03-20 17:59:06 -0800319 unsigned short proto, int inv_proto)
320{
321 if (XT_ALIGN(match->matchsize) != size) {
322 printk("%s_tables: %s match: invalid size %Zu != %u\n",
323 xt_prefix[family], match->name,
324 XT_ALIGN(match->matchsize), size);
325 return -EINVAL;
326 }
327 if (match->table && strcmp(match->table, table)) {
328 printk("%s_tables: %s match: only valid in %s table, not %s\n",
329 xt_prefix[family], match->name, match->table, table);
330 return -EINVAL;
331 }
332 if (match->hooks && (hook_mask & ~match->hooks) != 0) {
Balazs Scheidler5faf4152007-07-07 22:41:01 -0700333 printk("%s_tables: %s match: bad hook_mask %u/%u\n",
334 xt_prefix[family], match->name, hook_mask, match->hooks);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800335 return -EINVAL;
336 }
337 if (match->proto && (match->proto != proto || inv_proto)) {
338 printk("%s_tables: %s match: only valid for protocol %u\n",
339 xt_prefix[family], match->name, match->proto);
340 return -EINVAL;
341 }
342 return 0;
343}
344EXPORT_SYMBOL_GPL(xt_check_match);
345
Dmitry Mishin27229712006-04-01 02:25:19 -0800346#ifdef CONFIG_COMPAT
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800347int xt_compat_add_offset(int af, unsigned int offset, short delta)
348{
349 struct compat_delta *tmp;
350
351 tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
352 if (!tmp)
353 return -ENOMEM;
354
355 tmp->offset = offset;
356 tmp->delta = delta;
357
358 if (xt[af].compat_offsets) {
359 tmp->next = xt[af].compat_offsets->next;
360 xt[af].compat_offsets->next = tmp;
361 } else {
362 xt[af].compat_offsets = tmp;
363 tmp->next = NULL;
364 }
365 return 0;
366}
367EXPORT_SYMBOL_GPL(xt_compat_add_offset);
368
369void xt_compat_flush_offsets(int af)
370{
371 struct compat_delta *tmp, *next;
372
373 if (xt[af].compat_offsets) {
374 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
375 next = tmp->next;
376 kfree(tmp);
377 }
378 xt[af].compat_offsets = NULL;
379 }
380}
381EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
382
383short xt_compat_calc_jump(int af, unsigned int offset)
384{
385 struct compat_delta *tmp;
386 short delta;
387
388 for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
389 if (tmp->offset < offset)
390 delta += tmp->delta;
391 return delta;
392}
393EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
394
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700395int xt_compat_match_offset(struct xt_match *match)
Dmitry Mishin27229712006-04-01 02:25:19 -0800396{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700397 u_int16_t csize = match->compatsize ? : match->matchsize;
398 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800399}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700400EXPORT_SYMBOL_GPL(xt_compat_match_offset);
401
Patrick McHardy89566952007-12-17 21:46:40 -0800402int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
403 int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700404{
405 struct xt_match *match = m->u.kernel.match;
406 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
407 int pad, off = xt_compat_match_offset(match);
408 u_int16_t msize = cm->u.user.match_size;
409
410 m = *dstptr;
411 memcpy(m, cm, sizeof(*cm));
412 if (match->compat_from_user)
413 match->compat_from_user(m->data, cm->data);
414 else
415 memcpy(m->data, cm->data, msize - sizeof(*cm));
416 pad = XT_ALIGN(match->matchsize) - match->matchsize;
417 if (pad > 0)
418 memset(m->data + match->matchsize, 0, pad);
419
420 msize += off;
421 m->u.user.match_size = msize;
422
423 *size += off;
424 *dstptr += msize;
Patrick McHardy89566952007-12-17 21:46:40 -0800425 return 0;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700426}
427EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
428
429int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr,
430 int *size)
431{
432 struct xt_match *match = m->u.kernel.match;
433 struct compat_xt_entry_match __user *cm = *dstptr;
434 int off = xt_compat_match_offset(match);
435 u_int16_t msize = m->u.user.match_size - off;
436
437 if (copy_to_user(cm, m, sizeof(*cm)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800438 put_user(msize, &cm->u.user.match_size) ||
439 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
440 strlen(m->u.kernel.match->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800441 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700442
443 if (match->compat_to_user) {
444 if (match->compat_to_user((void __user *)cm->data, m->data))
445 return -EFAULT;
446 } else {
447 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
448 return -EFAULT;
449 }
450
451 *size -= off;
452 *dstptr += msize;
453 return 0;
454}
455EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
456#endif /* CONFIG_COMPAT */
Dmitry Mishin27229712006-04-01 02:25:19 -0800457
Patrick McHardy37f9f732006-03-20 17:59:06 -0800458int xt_check_target(const struct xt_target *target, unsigned short family,
459 unsigned int size, const char *table, unsigned int hook_mask,
460 unsigned short proto, int inv_proto)
461{
462 if (XT_ALIGN(target->targetsize) != size) {
463 printk("%s_tables: %s target: invalid size %Zu != %u\n",
464 xt_prefix[family], target->name,
465 XT_ALIGN(target->targetsize), size);
466 return -EINVAL;
467 }
468 if (target->table && strcmp(target->table, table)) {
469 printk("%s_tables: %s target: only valid in %s table, not %s\n",
470 xt_prefix[family], target->name, target->table, table);
471 return -EINVAL;
472 }
473 if (target->hooks && (hook_mask & ~target->hooks) != 0) {
Balazs Scheidler5faf4152007-07-07 22:41:01 -0700474 printk("%s_tables: %s target: bad hook_mask %u/%u\n",
475 xt_prefix[family], target->name, hook_mask,
476 target->hooks);
Patrick McHardy37f9f732006-03-20 17:59:06 -0800477 return -EINVAL;
478 }
479 if (target->proto && (target->proto != proto || inv_proto)) {
480 printk("%s_tables: %s target: only valid for protocol %u\n",
481 xt_prefix[family], target->name, target->proto);
482 return -EINVAL;
483 }
484 return 0;
485}
486EXPORT_SYMBOL_GPL(xt_check_target);
487
Dmitry Mishin27229712006-04-01 02:25:19 -0800488#ifdef CONFIG_COMPAT
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700489int xt_compat_target_offset(struct xt_target *target)
Dmitry Mishin27229712006-04-01 02:25:19 -0800490{
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700491 u_int16_t csize = target->compatsize ? : target->targetsize;
492 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
Dmitry Mishin27229712006-04-01 02:25:19 -0800493}
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700494EXPORT_SYMBOL_GPL(xt_compat_target_offset);
495
496void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800497 int *size)
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700498{
499 struct xt_target *target = t->u.kernel.target;
500 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
501 int pad, off = xt_compat_target_offset(target);
502 u_int16_t tsize = ct->u.user.target_size;
503
504 t = *dstptr;
505 memcpy(t, ct, sizeof(*ct));
506 if (target->compat_from_user)
507 target->compat_from_user(t->data, ct->data);
508 else
509 memcpy(t->data, ct->data, tsize - sizeof(*ct));
510 pad = XT_ALIGN(target->targetsize) - target->targetsize;
511 if (pad > 0)
512 memset(t->data + target->targetsize, 0, pad);
513
514 tsize += off;
515 t->u.user.target_size = tsize;
516
517 *size += off;
518 *dstptr += tsize;
519}
520EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
521
522int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr,
523 int *size)
524{
525 struct xt_target *target = t->u.kernel.target;
526 struct compat_xt_entry_target __user *ct = *dstptr;
527 int off = xt_compat_target_offset(target);
528 u_int16_t tsize = t->u.user.target_size - off;
529
530 if (copy_to_user(ct, t, sizeof(*ct)) ||
Patrick McHardya18aa312007-12-12 10:35:16 -0800531 put_user(tsize, &ct->u.user.target_size) ||
532 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
533 strlen(t->u.kernel.target->name) + 1))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800534 return -EFAULT;
Patrick McHardy9fa492c2006-09-20 12:05:37 -0700535
536 if (target->compat_to_user) {
537 if (target->compat_to_user((void __user *)ct->data, t->data))
538 return -EFAULT;
539 } else {
540 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
541 return -EFAULT;
542 }
543
544 *size -= off;
545 *dstptr += tsize;
546 return 0;
547}
548EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
Dmitry Mishin27229712006-04-01 02:25:19 -0800549#endif
550
Harald Welte2e4e6a12006-01-12 13:30:04 -0800551struct xt_table_info *xt_alloc_table_info(unsigned int size)
552{
553 struct xt_table_info *newinfo;
554 int cpu;
555
556 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
557 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
558 return NULL;
559
Eric Dumazet259d4e42007-12-04 23:24:56 -0800560 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800561 if (!newinfo)
562 return NULL;
563
564 newinfo->size = size;
565
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700566 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800567 if (size <= PAGE_SIZE)
568 newinfo->entries[cpu] = kmalloc_node(size,
569 GFP_KERNEL,
570 cpu_to_node(cpu));
571 else
572 newinfo->entries[cpu] = vmalloc_node(size,
573 cpu_to_node(cpu));
574
575 if (newinfo->entries[cpu] == NULL) {
576 xt_free_table_info(newinfo);
577 return NULL;
578 }
579 }
580
581 return newinfo;
582}
583EXPORT_SYMBOL(xt_alloc_table_info);
584
585void xt_free_table_info(struct xt_table_info *info)
586{
587 int cpu;
588
KAMEZAWA Hiroyuki6f912042006-04-10 22:52:50 -0700589 for_each_possible_cpu(cpu) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800590 if (info->size <= PAGE_SIZE)
591 kfree(info->entries[cpu]);
592 else
593 vfree(info->entries[cpu]);
594 }
595 kfree(info);
596}
597EXPORT_SYMBOL(xt_free_table_info);
598
599/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
600struct xt_table *xt_find_table_lock(int af, const char *name)
601{
602 struct xt_table *t;
603
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800604 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800605 return ERR_PTR(-EINTR);
606
607 list_for_each_entry(t, &xt[af].tables, list)
608 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
609 return t;
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800610 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800611 return NULL;
612}
613EXPORT_SYMBOL_GPL(xt_find_table_lock);
614
615void xt_table_unlock(struct xt_table *table)
616{
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800617 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800618}
619EXPORT_SYMBOL_GPL(xt_table_unlock);
620
Dmitry Mishin27229712006-04-01 02:25:19 -0800621#ifdef CONFIG_COMPAT
622void xt_compat_lock(int af)
623{
624 mutex_lock(&xt[af].compat_mutex);
625}
626EXPORT_SYMBOL_GPL(xt_compat_lock);
627
628void xt_compat_unlock(int af)
629{
630 mutex_unlock(&xt[af].compat_mutex);
631}
632EXPORT_SYMBOL_GPL(xt_compat_unlock);
633#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800634
635struct xt_table_info *
636xt_replace_table(struct xt_table *table,
637 unsigned int num_counters,
638 struct xt_table_info *newinfo,
639 int *error)
640{
641 struct xt_table_info *oldinfo, *private;
642
643 /* Do the substitution. */
644 write_lock_bh(&table->lock);
645 private = table->private;
646 /* Check inside lock: is the old number correct? */
647 if (num_counters != private->number) {
648 duprintf("num_counters != table->private->number (%u/%u)\n",
649 num_counters, private->number);
650 write_unlock_bh(&table->lock);
651 *error = -EAGAIN;
652 return NULL;
653 }
654 oldinfo = private;
655 table->private = newinfo;
656 newinfo->initial_entries = oldinfo->initial_entries;
657 write_unlock_bh(&table->lock);
658
659 return oldinfo;
660}
661EXPORT_SYMBOL_GPL(xt_replace_table);
662
663int xt_register_table(struct xt_table *table,
664 struct xt_table_info *bootstrap,
665 struct xt_table_info *newinfo)
666{
667 int ret;
668 struct xt_table_info *private;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700669 struct xt_table *t;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800670
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800671 ret = mutex_lock_interruptible(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800672 if (ret != 0)
673 return ret;
674
675 /* Don't autoload: we'd eat our tail... */
Patrick McHardydf0933d2006-09-20 11:57:53 -0700676 list_for_each_entry(t, &xt[table->af].tables, list) {
677 if (strcmp(t->name, table->name) == 0) {
678 ret = -EEXIST;
679 goto unlock;
680 }
Harald Welte2e4e6a12006-01-12 13:30:04 -0800681 }
682
683 /* Simplifies replace_table code. */
684 table->private = bootstrap;
Dmitry Mishin91536b72006-04-24 17:18:25 -0700685 rwlock_init(&table->lock);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800686 if (!xt_replace_table(table, 0, newinfo, &ret))
687 goto unlock;
688
689 private = table->private;
690 duprintf("table->private->number = %u\n", private->number);
691
692 /* save number of initial entries */
693 private->initial_entries = private->number;
694
Patrick McHardydf0933d2006-09-20 11:57:53 -0700695 list_add(&table->list, &xt[table->af].tables);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800696
697 ret = 0;
698 unlock:
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800699 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800700 return ret;
701}
702EXPORT_SYMBOL_GPL(xt_register_table);
703
704void *xt_unregister_table(struct xt_table *table)
705{
706 struct xt_table_info *private;
707
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800708 mutex_lock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800709 private = table->private;
Patrick McHardydf0933d2006-09-20 11:57:53 -0700710 list_del(&table->list);
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800711 mutex_unlock(&xt[table->af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800712
713 return private;
714}
715EXPORT_SYMBOL_GPL(xt_unregister_table);
716
717#ifdef CONFIG_PROC_FS
Harald Welte2e4e6a12006-01-12 13:30:04 -0800718static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos)
719{
720 struct list_head *head = list->next;
721
722 if (!head || list_empty(list))
723 return NULL;
724
725 while (pos && (head = head->next)) {
726 if (head == list)
727 return NULL;
728 pos--;
729 }
730 return pos ? NULL : head;
731}
732
733static struct list_head *type2list(u_int16_t af, u_int16_t type)
734{
735 struct list_head *list;
736
737 switch (type) {
738 case TARGET:
739 list = &xt[af].target;
740 break;
741 case MATCH:
742 list = &xt[af].match;
743 break;
744 case TABLE:
745 list = &xt[af].tables;
746 break;
747 default:
748 list = NULL;
749 break;
750 }
751
752 return list;
753}
754
755static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos)
756{
757 struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private;
758 u_int16_t af = (unsigned long)pde->data & 0xffff;
759 u_int16_t type = (unsigned long)pde->data >> 16;
760 struct list_head *list;
761
762 if (af >= NPROTO)
763 return NULL;
764
765 list = type2list(af, type);
766 if (!list)
767 return NULL;
768
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800769 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800770 return NULL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800771
Harald Welte2e4e6a12006-01-12 13:30:04 -0800772 return xt_get_idx(list, seq, *pos);
773}
774
775static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
776{
777 struct proc_dir_entry *pde = seq->private;
778 u_int16_t af = (unsigned long)pde->data & 0xffff;
779 u_int16_t type = (unsigned long)pde->data >> 16;
780 struct list_head *list;
781
782 if (af >= NPROTO)
783 return NULL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800784
Harald Welte2e4e6a12006-01-12 13:30:04 -0800785 list = type2list(af, type);
786 if (!list)
787 return NULL;
788
789 (*pos)++;
790 return xt_get_idx(list, seq, *pos);
791}
792
793static void xt_tgt_seq_stop(struct seq_file *seq, void *v)
794{
795 struct proc_dir_entry *pde = seq->private;
796 u_int16_t af = (unsigned long)pde->data & 0xffff;
797
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800798 mutex_unlock(&xt[af].mutex);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800799}
800
801static int xt_name_seq_show(struct seq_file *seq, void *v)
802{
803 char *name = (char *)v + sizeof(struct list_head);
804
805 if (strlen(name))
806 return seq_printf(seq, "%s\n", name);
807 else
808 return 0;
809}
810
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700811static const struct seq_operations xt_tgt_seq_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800812 .start = xt_tgt_seq_start,
813 .next = xt_tgt_seq_next,
814 .stop = xt_tgt_seq_stop,
815 .show = xt_name_seq_show,
816};
817
818static int xt_tgt_open(struct inode *inode, struct file *file)
819{
820 int ret;
821
822 ret = seq_open(file, &xt_tgt_seq_ops);
823 if (!ret) {
824 struct seq_file *seq = file->private_data;
825 struct proc_dir_entry *pde = PDE(inode);
826
827 seq->private = pde;
828 }
829
830 return ret;
831}
832
Arjan van de Venda7071d2007-02-12 00:55:36 -0800833static const struct file_operations xt_file_ops = {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800834 .owner = THIS_MODULE,
835 .open = xt_tgt_open,
836 .read = seq_read,
837 .llseek = seq_lseek,
838 .release = seq_release,
839};
840
841#define FORMAT_TABLES "_tables_names"
842#define FORMAT_MATCHES "_tables_matches"
843#define FORMAT_TARGETS "_tables_targets"
844
845#endif /* CONFIG_PROC_FS */
846
847int xt_proto_init(int af)
848{
849#ifdef CONFIG_PROC_FS
850 char buf[XT_FUNCTION_MAXNAMELEN];
851 struct proc_dir_entry *proc;
852#endif
853
854 if (af >= NPROTO)
855 return -EINVAL;
856
857
858#ifdef CONFIG_PROC_FS
Tobias Klauserce18afe2007-03-14 16:36:16 -0700859 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800860 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200861 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800862 if (!proc)
863 goto out;
864 proc->data = (void *) ((unsigned long) af | (TABLE << 16));
865
866
Tobias Klauserce18afe2007-03-14 16:36:16 -0700867 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800868 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200869 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800870 if (!proc)
871 goto out_remove_tables;
872 proc->data = (void *) ((unsigned long) af | (MATCH << 16));
873
Tobias Klauserce18afe2007-03-14 16:36:16 -0700874 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800875 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200876 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800877 if (!proc)
878 goto out_remove_matches;
879 proc->data = (void *) ((unsigned long) af | (TARGET << 16));
880#endif
881
882 return 0;
883
884#ifdef CONFIG_PROC_FS
885out_remove_matches:
Tobias Klauserce18afe2007-03-14 16:36:16 -0700886 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800887 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200888 proc_net_remove(&init_net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800889
890out_remove_tables:
Tobias Klauserce18afe2007-03-14 16:36:16 -0700891 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800892 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200893 proc_net_remove(&init_net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800894out:
895 return -1;
896#endif
897}
898EXPORT_SYMBOL_GPL(xt_proto_init);
899
900void xt_proto_fini(int af)
901{
902#ifdef CONFIG_PROC_FS
903 char buf[XT_FUNCTION_MAXNAMELEN];
904
Tobias Klauserce18afe2007-03-14 16:36:16 -0700905 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800906 strlcat(buf, FORMAT_TABLES, sizeof(buf));
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200907 proc_net_remove(&init_net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800908
Tobias Klauserce18afe2007-03-14 16:36:16 -0700909 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800910 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200911 proc_net_remove(&init_net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800912
Tobias Klauserce18afe2007-03-14 16:36:16 -0700913 strlcpy(buf, xt_prefix[af], sizeof(buf));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800914 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
Eric W. Biederman457c4cb2007-09-12 12:01:34 +0200915 proc_net_remove(&init_net, buf);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800916#endif /*CONFIG_PROC_FS*/
917}
918EXPORT_SYMBOL_GPL(xt_proto_fini);
919
920
921static int __init xt_init(void)
922{
923 int i;
924
925 xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
926 if (!xt)
927 return -ENOMEM;
928
929 for (i = 0; i < NPROTO; i++) {
Ingo Molnar9e19bb62006-03-25 01:41:10 -0800930 mutex_init(&xt[i].mutex);
Dmitry Mishin27229712006-04-01 02:25:19 -0800931#ifdef CONFIG_COMPAT
932 mutex_init(&xt[i].compat_mutex);
Patrick McHardyb386d9f2007-12-17 21:47:48 -0800933 xt[i].compat_offsets = NULL;
Dmitry Mishin27229712006-04-01 02:25:19 -0800934#endif
Harald Welte2e4e6a12006-01-12 13:30:04 -0800935 INIT_LIST_HEAD(&xt[i].target);
936 INIT_LIST_HEAD(&xt[i].match);
937 INIT_LIST_HEAD(&xt[i].tables);
938 }
939 return 0;
940}
941
942static void __exit xt_fini(void)
943{
944 kfree(xt);
945}
946
947module_init(xt_init);
948module_exit(xt_fini);
949