2 * x_tables core - Backend for {ip,ip6,arp}_tables
4 * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
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>
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.
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/socket.h>
19 #include <linux/net.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/string.h>
23 #include <linux/vmalloc.h>
24 #include <linux/mutex.h>
26 #include <linux/netfilter/x_tables.h>
27 #include <linux/netfilter_arp.h>
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
32 MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module");
34 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
38 struct list_head match;
39 struct list_head target;
40 struct list_head tables;
41 struct mutex compat_mutex;
44 static struct xt_af *xt;
46 #ifdef DEBUG_IP_FIREWALL_USER
47 #define duprintf(format, args...) printk(format , ## args)
49 #define duprintf(format, args...)
58 static const char *xt_prefix[NPROTO] = {
64 /* Registration hooks for targets. */
66 xt_register_target(struct xt_target *target)
68 int ret, af = target->family;
70 ret = mutex_lock_interruptible(&xt[af].mutex);
73 list_add(&target->list, &xt[af].target);
74 mutex_unlock(&xt[af].mutex);
77 EXPORT_SYMBOL(xt_register_target);
80 xt_unregister_target(struct xt_target *target)
82 int af = target->family;
84 mutex_lock(&xt[af].mutex);
85 LIST_DELETE(&xt[af].target, target);
86 mutex_unlock(&xt[af].mutex);
88 EXPORT_SYMBOL(xt_unregister_target);
91 xt_register_match(struct xt_match *match)
93 int ret, af = match->family;
95 ret = mutex_lock_interruptible(&xt[af].mutex);
99 list_add(&match->list, &xt[af].match);
100 mutex_unlock(&xt[af].mutex);
104 EXPORT_SYMBOL(xt_register_match);
107 xt_unregister_match(struct xt_match *match)
109 int af = match->family;
111 mutex_lock(&xt[af].mutex);
112 LIST_DELETE(&xt[af].match, match);
113 mutex_unlock(&xt[af].mutex);
115 EXPORT_SYMBOL(xt_unregister_match);
119 * These are weird, but module loading must not be done with mutex
120 * held (since they will register), and we have to have a single
121 * function to use try_then_request_module().
124 /* Find match, grabs ref. Returns ERR_PTR() on error. */
125 struct xt_match *xt_find_match(int af, const char *name, u8 revision)
130 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
131 return ERR_PTR(-EINTR);
133 list_for_each_entry(m, &xt[af].match, list) {
134 if (strcmp(m->name, name) == 0) {
135 if (m->revision == revision) {
136 if (try_module_get(m->me)) {
137 mutex_unlock(&xt[af].mutex);
141 err = -EPROTOTYPE; /* Found something. */
144 mutex_unlock(&xt[af].mutex);
147 EXPORT_SYMBOL(xt_find_match);
149 /* Find target, grabs ref. Returns ERR_PTR() on error. */
150 struct xt_target *xt_find_target(int af, const char *name, u8 revision)
155 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
156 return ERR_PTR(-EINTR);
158 list_for_each_entry(t, &xt[af].target, list) {
159 if (strcmp(t->name, name) == 0) {
160 if (t->revision == revision) {
161 if (try_module_get(t->me)) {
162 mutex_unlock(&xt[af].mutex);
166 err = -EPROTOTYPE; /* Found something. */
169 mutex_unlock(&xt[af].mutex);
172 EXPORT_SYMBOL(xt_find_target);
174 struct xt_target *xt_request_find_target(int af, const char *name, u8 revision)
176 struct xt_target *target;
178 target = try_then_request_module(xt_find_target(af, name, revision),
179 "%st_%s", xt_prefix[af], name);
180 if (IS_ERR(target) || !target)
184 EXPORT_SYMBOL_GPL(xt_request_find_target);
186 static int match_revfn(int af, const char *name, u8 revision, int *bestp)
191 list_for_each_entry(m, &xt[af].match, list) {
192 if (strcmp(m->name, name) == 0) {
193 if (m->revision > *bestp)
194 *bestp = m->revision;
195 if (m->revision == revision)
202 static int target_revfn(int af, const char *name, u8 revision, int *bestp)
207 list_for_each_entry(t, &xt[af].target, list) {
208 if (strcmp(t->name, name) == 0) {
209 if (t->revision > *bestp)
210 *bestp = t->revision;
211 if (t->revision == revision)
218 /* Returns true or false (if no such extension at all) */
219 int xt_find_revision(int af, const char *name, u8 revision, int target,
222 int have_rev, best = -1;
224 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
229 have_rev = target_revfn(af, name, revision, &best);
231 have_rev = match_revfn(af, name, revision, &best);
232 mutex_unlock(&xt[af].mutex);
234 /* Nothing at all? Return 0 to try loading module. */
242 *err = -EPROTONOSUPPORT;
245 EXPORT_SYMBOL_GPL(xt_find_revision);
247 int xt_check_match(const struct xt_match *match, unsigned short family,
248 unsigned int size, const char *table, unsigned int hook_mask,
249 unsigned short proto, int inv_proto)
251 if (XT_ALIGN(match->matchsize) != size) {
252 printk("%s_tables: %s match: invalid size %Zu != %u\n",
253 xt_prefix[family], match->name,
254 XT_ALIGN(match->matchsize), size);
257 if (match->table && strcmp(match->table, table)) {
258 printk("%s_tables: %s match: only valid in %s table, not %s\n",
259 xt_prefix[family], match->name, match->table, table);
262 if (match->hooks && (hook_mask & ~match->hooks) != 0) {
263 printk("%s_tables: %s match: bad hook_mask %u\n",
264 xt_prefix[family], match->name, hook_mask);
267 if (match->proto && (match->proto != proto || inv_proto)) {
268 printk("%s_tables: %s match: only valid for protocol %u\n",
269 xt_prefix[family], match->name, match->proto);
274 EXPORT_SYMBOL_GPL(xt_check_match);
277 int xt_compat_match(void *match, void **dstptr, int *size, int convert)
280 struct compat_xt_entry_match *pcompat_m;
281 struct xt_entry_match *pm;
286 m = ((struct xt_entry_match *)match)->u.kernel.match;
287 off = XT_ALIGN(m->matchsize) - COMPAT_XT_ALIGN(m->matchsize);
290 pm = (struct xt_entry_match *)match;
291 msize = pm->u.user.match_size;
292 if (__copy_to_user(*dstptr, pm, msize)) {
297 if (put_user(msize, (u_int16_t *)*dstptr))
302 case COMPAT_FROM_USER:
303 pcompat_m = (struct compat_xt_entry_match *)match;
304 pm = (struct xt_entry_match *)*dstptr;
305 msize = pcompat_m->u.user.match_size;
306 memcpy(pm, pcompat_m, msize);
308 pm->u.user.match_size = msize;
312 case COMPAT_CALC_SIZE:
321 EXPORT_SYMBOL_GPL(xt_compat_match);
324 int xt_check_target(const struct xt_target *target, unsigned short family,
325 unsigned int size, const char *table, unsigned int hook_mask,
326 unsigned short proto, int inv_proto)
328 if (XT_ALIGN(target->targetsize) != size) {
329 printk("%s_tables: %s target: invalid size %Zu != %u\n",
330 xt_prefix[family], target->name,
331 XT_ALIGN(target->targetsize), size);
334 if (target->table && strcmp(target->table, table)) {
335 printk("%s_tables: %s target: only valid in %s table, not %s\n",
336 xt_prefix[family], target->name, target->table, table);
339 if (target->hooks && (hook_mask & ~target->hooks) != 0) {
340 printk("%s_tables: %s target: bad hook_mask %u\n",
341 xt_prefix[family], target->name, hook_mask);
344 if (target->proto && (target->proto != proto || inv_proto)) {
345 printk("%s_tables: %s target: only valid for protocol %u\n",
346 xt_prefix[family], target->name, target->proto);
351 EXPORT_SYMBOL_GPL(xt_check_target);
354 int xt_compat_target(void *target, void **dstptr, int *size, int convert)
357 struct compat_xt_entry_target *pcompat;
358 struct xt_entry_target *pt;
363 t = ((struct xt_entry_target *)target)->u.kernel.target;
364 off = XT_ALIGN(t->targetsize) - COMPAT_XT_ALIGN(t->targetsize);
367 pt = (struct xt_entry_target *)target;
368 tsize = pt->u.user.target_size;
369 if (__copy_to_user(*dstptr, pt, tsize)) {
374 if (put_user(tsize, (u_int16_t *)*dstptr))
379 case COMPAT_FROM_USER:
380 pcompat = (struct compat_xt_entry_target *)target;
381 pt = (struct xt_entry_target *)*dstptr;
382 tsize = pcompat->u.user.target_size;
383 memcpy(pt, pcompat, tsize);
385 pt->u.user.target_size = tsize;
389 case COMPAT_CALC_SIZE:
398 EXPORT_SYMBOL_GPL(xt_compat_target);
401 struct xt_table_info *xt_alloc_table_info(unsigned int size)
403 struct xt_table_info *newinfo;
406 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
407 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
410 newinfo = kzalloc(sizeof(struct xt_table_info), GFP_KERNEL);
414 newinfo->size = size;
416 for_each_possible_cpu(cpu) {
417 if (size <= PAGE_SIZE)
418 newinfo->entries[cpu] = kmalloc_node(size,
422 newinfo->entries[cpu] = vmalloc_node(size,
425 if (newinfo->entries[cpu] == NULL) {
426 xt_free_table_info(newinfo);
433 EXPORT_SYMBOL(xt_alloc_table_info);
435 void xt_free_table_info(struct xt_table_info *info)
439 for_each_possible_cpu(cpu) {
440 if (info->size <= PAGE_SIZE)
441 kfree(info->entries[cpu]);
443 vfree(info->entries[cpu]);
447 EXPORT_SYMBOL(xt_free_table_info);
449 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
450 struct xt_table *xt_find_table_lock(int af, const char *name)
454 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
455 return ERR_PTR(-EINTR);
457 list_for_each_entry(t, &xt[af].tables, list)
458 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
460 mutex_unlock(&xt[af].mutex);
463 EXPORT_SYMBOL_GPL(xt_find_table_lock);
465 void xt_table_unlock(struct xt_table *table)
467 mutex_unlock(&xt[table->af].mutex);
469 EXPORT_SYMBOL_GPL(xt_table_unlock);
472 void xt_compat_lock(int af)
474 mutex_lock(&xt[af].compat_mutex);
476 EXPORT_SYMBOL_GPL(xt_compat_lock);
478 void xt_compat_unlock(int af)
480 mutex_unlock(&xt[af].compat_mutex);
482 EXPORT_SYMBOL_GPL(xt_compat_unlock);
485 struct xt_table_info *
486 xt_replace_table(struct xt_table *table,
487 unsigned int num_counters,
488 struct xt_table_info *newinfo,
491 struct xt_table_info *oldinfo, *private;
493 /* Do the substitution. */
494 write_lock_bh(&table->lock);
495 private = table->private;
496 /* Check inside lock: is the old number correct? */
497 if (num_counters != private->number) {
498 duprintf("num_counters != table->private->number (%u/%u)\n",
499 num_counters, private->number);
500 write_unlock_bh(&table->lock);
505 table->private = newinfo;
506 newinfo->initial_entries = oldinfo->initial_entries;
507 write_unlock_bh(&table->lock);
511 EXPORT_SYMBOL_GPL(xt_replace_table);
513 int xt_register_table(struct xt_table *table,
514 struct xt_table_info *bootstrap,
515 struct xt_table_info *newinfo)
518 struct xt_table_info *private;
520 ret = mutex_lock_interruptible(&xt[table->af].mutex);
524 /* Don't autoload: we'd eat our tail... */
525 if (list_named_find(&xt[table->af].tables, table->name)) {
530 /* Simplifies replace_table code. */
531 table->private = bootstrap;
532 if (!xt_replace_table(table, 0, newinfo, &ret))
535 private = table->private;
536 duprintf("table->private->number = %u\n", private->number);
538 /* save number of initial entries */
539 private->initial_entries = private->number;
541 rwlock_init(&table->lock);
542 list_prepend(&xt[table->af].tables, table);
546 mutex_unlock(&xt[table->af].mutex);
549 EXPORT_SYMBOL_GPL(xt_register_table);
551 void *xt_unregister_table(struct xt_table *table)
553 struct xt_table_info *private;
555 mutex_lock(&xt[table->af].mutex);
556 private = table->private;
557 LIST_DELETE(&xt[table->af].tables, table);
558 mutex_unlock(&xt[table->af].mutex);
562 EXPORT_SYMBOL_GPL(xt_unregister_table);
564 #ifdef CONFIG_PROC_FS
565 static char *xt_proto_prefix[NPROTO] = {
571 static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos)
573 struct list_head *head = list->next;
575 if (!head || list_empty(list))
578 while (pos && (head = head->next)) {
583 return pos ? NULL : head;
586 static struct list_head *type2list(u_int16_t af, u_int16_t type)
588 struct list_head *list;
592 list = &xt[af].target;
595 list = &xt[af].match;
598 list = &xt[af].tables;
608 static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos)
610 struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private;
611 u_int16_t af = (unsigned long)pde->data & 0xffff;
612 u_int16_t type = (unsigned long)pde->data >> 16;
613 struct list_head *list;
618 list = type2list(af, type);
622 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
625 return xt_get_idx(list, seq, *pos);
628 static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
630 struct proc_dir_entry *pde = seq->private;
631 u_int16_t af = (unsigned long)pde->data & 0xffff;
632 u_int16_t type = (unsigned long)pde->data >> 16;
633 struct list_head *list;
638 list = type2list(af, type);
643 return xt_get_idx(list, seq, *pos);
646 static void xt_tgt_seq_stop(struct seq_file *seq, void *v)
648 struct proc_dir_entry *pde = seq->private;
649 u_int16_t af = (unsigned long)pde->data & 0xffff;
651 mutex_unlock(&xt[af].mutex);
654 static int xt_name_seq_show(struct seq_file *seq, void *v)
656 char *name = (char *)v + sizeof(struct list_head);
659 return seq_printf(seq, "%s\n", name);
664 static struct seq_operations xt_tgt_seq_ops = {
665 .start = xt_tgt_seq_start,
666 .next = xt_tgt_seq_next,
667 .stop = xt_tgt_seq_stop,
668 .show = xt_name_seq_show,
671 static int xt_tgt_open(struct inode *inode, struct file *file)
675 ret = seq_open(file, &xt_tgt_seq_ops);
677 struct seq_file *seq = file->private_data;
678 struct proc_dir_entry *pde = PDE(inode);
686 static struct file_operations xt_file_ops = {
687 .owner = THIS_MODULE,
691 .release = seq_release,
694 #define FORMAT_TABLES "_tables_names"
695 #define FORMAT_MATCHES "_tables_matches"
696 #define FORMAT_TARGETS "_tables_targets"
698 #endif /* CONFIG_PROC_FS */
700 int xt_proto_init(int af)
702 #ifdef CONFIG_PROC_FS
703 char buf[XT_FUNCTION_MAXNAMELEN];
704 struct proc_dir_entry *proc;
711 #ifdef CONFIG_PROC_FS
712 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
713 strlcat(buf, FORMAT_TABLES, sizeof(buf));
714 proc = proc_net_fops_create(buf, 0440, &xt_file_ops);
717 proc->data = (void *) ((unsigned long) af | (TABLE << 16));
720 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
721 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
722 proc = proc_net_fops_create(buf, 0440, &xt_file_ops);
724 goto out_remove_tables;
725 proc->data = (void *) ((unsigned long) af | (MATCH << 16));
727 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
728 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
729 proc = proc_net_fops_create(buf, 0440, &xt_file_ops);
731 goto out_remove_matches;
732 proc->data = (void *) ((unsigned long) af | (TARGET << 16));
737 #ifdef CONFIG_PROC_FS
739 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
740 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
741 proc_net_remove(buf);
744 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
745 strlcat(buf, FORMAT_TABLES, sizeof(buf));
746 proc_net_remove(buf);
751 EXPORT_SYMBOL_GPL(xt_proto_init);
753 void xt_proto_fini(int af)
755 #ifdef CONFIG_PROC_FS
756 char buf[XT_FUNCTION_MAXNAMELEN];
758 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
759 strlcat(buf, FORMAT_TABLES, sizeof(buf));
760 proc_net_remove(buf);
762 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
763 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
764 proc_net_remove(buf);
766 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
767 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
768 proc_net_remove(buf);
769 #endif /*CONFIG_PROC_FS*/
771 EXPORT_SYMBOL_GPL(xt_proto_fini);
774 static int __init xt_init(void)
778 xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
782 for (i = 0; i < NPROTO; i++) {
783 mutex_init(&xt[i].mutex);
785 mutex_init(&xt[i].compat_mutex);
787 INIT_LIST_HEAD(&xt[i].target);
788 INIT_LIST_HEAD(&xt[i].match);
789 INIT_LIST_HEAD(&xt[i].tables);
794 static void __exit xt_fini(void)
799 module_init(xt_init);
800 module_exit(xt_fini);