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/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>
23 #include <linux/mutex.h>
25 #include <net/net_namespace.h>
27 #include <linux/netfilter/x_tables.h>
28 #include <linux/netfilter_arp.h>
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
33 MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module");
35 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
38 struct compat_delta *next;
45 struct list_head match;
46 struct list_head target;
47 struct list_head tables;
49 struct mutex compat_mutex;
50 struct compat_delta *compat_offsets;
54 static struct xt_af *xt;
56 #ifdef DEBUG_IP_FIREWALL_USER
57 #define duprintf(format, args...) printk(format , ## args)
59 #define duprintf(format, args...)
68 static const char *xt_prefix[NPROTO] = {
74 /* Registration hooks for targets. */
76 xt_register_target(struct xt_target *target)
78 int ret, af = target->family;
80 ret = mutex_lock_interruptible(&xt[af].mutex);
83 list_add(&target->list, &xt[af].target);
84 mutex_unlock(&xt[af].mutex);
87 EXPORT_SYMBOL(xt_register_target);
90 xt_unregister_target(struct xt_target *target)
92 int af = target->family;
94 mutex_lock(&xt[af].mutex);
95 list_del(&target->list);
96 mutex_unlock(&xt[af].mutex);
98 EXPORT_SYMBOL(xt_unregister_target);
101 xt_register_targets(struct xt_target *target, unsigned int n)
106 for (i = 0; i < n; i++) {
107 err = xt_register_target(&target[i]);
115 xt_unregister_targets(target, i);
118 EXPORT_SYMBOL(xt_register_targets);
121 xt_unregister_targets(struct xt_target *target, unsigned int n)
125 for (i = 0; i < n; i++)
126 xt_unregister_target(&target[i]);
128 EXPORT_SYMBOL(xt_unregister_targets);
131 xt_register_match(struct xt_match *match)
133 int ret, af = match->family;
135 ret = mutex_lock_interruptible(&xt[af].mutex);
139 list_add(&match->list, &xt[af].match);
140 mutex_unlock(&xt[af].mutex);
144 EXPORT_SYMBOL(xt_register_match);
147 xt_unregister_match(struct xt_match *match)
149 int af = match->family;
151 mutex_lock(&xt[af].mutex);
152 list_del(&match->list);
153 mutex_unlock(&xt[af].mutex);
155 EXPORT_SYMBOL(xt_unregister_match);
158 xt_register_matches(struct xt_match *match, unsigned int n)
163 for (i = 0; i < n; i++) {
164 err = xt_register_match(&match[i]);
172 xt_unregister_matches(match, i);
175 EXPORT_SYMBOL(xt_register_matches);
178 xt_unregister_matches(struct xt_match *match, unsigned int n)
182 for (i = 0; i < n; i++)
183 xt_unregister_match(&match[i]);
185 EXPORT_SYMBOL(xt_unregister_matches);
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().
194 /* Find match, grabs ref. Returns ERR_PTR() on error. */
195 struct xt_match *xt_find_match(int af, const char *name, u8 revision)
200 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
201 return ERR_PTR(-EINTR);
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)) {
207 mutex_unlock(&xt[af].mutex);
211 err = -EPROTOTYPE; /* Found something. */
214 mutex_unlock(&xt[af].mutex);
217 EXPORT_SYMBOL(xt_find_match);
219 /* Find target, grabs ref. Returns ERR_PTR() on error. */
220 struct xt_target *xt_find_target(int af, const char *name, u8 revision)
225 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
226 return ERR_PTR(-EINTR);
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)) {
232 mutex_unlock(&xt[af].mutex);
236 err = -EPROTOTYPE; /* Found something. */
239 mutex_unlock(&xt[af].mutex);
242 EXPORT_SYMBOL(xt_find_target);
244 struct xt_target *xt_request_find_target(int af, const char *name, u8 revision)
246 struct xt_target *target;
248 target = try_then_request_module(xt_find_target(af, name, revision),
249 "%st_%s", xt_prefix[af], name);
250 if (IS_ERR(target) || !target)
254 EXPORT_SYMBOL_GPL(xt_request_find_target);
256 static int match_revfn(int af, const char *name, u8 revision, int *bestp)
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)
272 static int target_revfn(int af, const char *name, u8 revision, int *bestp)
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)
288 /* Returns true or false (if no such extension at all) */
289 int xt_find_revision(int af, const char *name, u8 revision, int target,
292 int have_rev, best = -1;
294 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
299 have_rev = target_revfn(af, name, revision, &best);
301 have_rev = match_revfn(af, name, revision, &best);
302 mutex_unlock(&xt[af].mutex);
304 /* Nothing at all? Return 0 to try loading module. */
312 *err = -EPROTONOSUPPORT;
315 EXPORT_SYMBOL_GPL(xt_find_revision);
317 int xt_check_match(const struct xt_match *match, unsigned short family,
318 unsigned int size, const char *table, unsigned int hook_mask,
319 unsigned short proto, int inv_proto)
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);
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);
332 if (match->hooks && (hook_mask & ~match->hooks) != 0) {
333 printk("%s_tables: %s match: bad hook_mask %u/%u\n",
334 xt_prefix[family], match->name, hook_mask, match->hooks);
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);
344 EXPORT_SYMBOL_GPL(xt_check_match);
347 int xt_compat_add_offset(int af, unsigned int offset, short delta)
349 struct compat_delta *tmp;
351 tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
355 tmp->offset = offset;
358 if (xt[af].compat_offsets) {
359 tmp->next = xt[af].compat_offsets->next;
360 xt[af].compat_offsets->next = tmp;
362 xt[af].compat_offsets = tmp;
367 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
369 void xt_compat_flush_offsets(int af)
371 struct compat_delta *tmp, *next;
373 if (xt[af].compat_offsets) {
374 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
378 xt[af].compat_offsets = NULL;
381 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
383 short xt_compat_calc_jump(int af, unsigned int offset)
385 struct compat_delta *tmp;
388 for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
389 if (tmp->offset < offset)
393 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
395 int xt_compat_match_offset(struct xt_match *match)
397 u_int16_t csize = match->compatsize ? : match->matchsize;
398 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
400 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
402 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
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;
411 memcpy(m, cm, sizeof(*cm));
412 if (match->compat_from_user)
413 match->compat_from_user(m->data, cm->data);
415 memcpy(m->data, cm->data, msize - sizeof(*cm));
416 pad = XT_ALIGN(match->matchsize) - match->matchsize;
418 memset(m->data + match->matchsize, 0, pad);
421 m->u.user.match_size = msize;
427 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
429 int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr,
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;
437 if (copy_to_user(cm, m, sizeof(*cm)) ||
438 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))
443 if (match->compat_to_user) {
444 if (match->compat_to_user((void __user *)cm->data, m->data))
447 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
455 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
456 #endif /* CONFIG_COMPAT */
458 int 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)
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);
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);
473 if (target->hooks && (hook_mask & ~target->hooks) != 0) {
474 printk("%s_tables: %s target: bad hook_mask %u/%u\n",
475 xt_prefix[family], target->name, hook_mask,
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);
486 EXPORT_SYMBOL_GPL(xt_check_target);
489 int xt_compat_target_offset(struct xt_target *target)
491 u_int16_t csize = target->compatsize ? : target->targetsize;
492 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
494 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
496 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
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;
505 memcpy(t, ct, sizeof(*ct));
506 if (target->compat_from_user)
507 target->compat_from_user(t->data, ct->data);
509 memcpy(t->data, ct->data, tsize - sizeof(*ct));
510 pad = XT_ALIGN(target->targetsize) - target->targetsize;
512 memset(t->data + target->targetsize, 0, pad);
515 t->u.user.target_size = tsize;
520 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
522 int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr,
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;
530 if (copy_to_user(ct, t, sizeof(*ct)) ||
531 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))
536 if (target->compat_to_user) {
537 if (target->compat_to_user((void __user *)ct->data, t->data))
540 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
548 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
551 struct xt_table_info *xt_alloc_table_info(unsigned int size)
553 struct xt_table_info *newinfo;
556 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
557 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
560 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
564 newinfo->size = size;
566 for_each_possible_cpu(cpu) {
567 if (size <= PAGE_SIZE)
568 newinfo->entries[cpu] = kmalloc_node(size,
572 newinfo->entries[cpu] = vmalloc_node(size,
575 if (newinfo->entries[cpu] == NULL) {
576 xt_free_table_info(newinfo);
583 EXPORT_SYMBOL(xt_alloc_table_info);
585 void xt_free_table_info(struct xt_table_info *info)
589 for_each_possible_cpu(cpu) {
590 if (info->size <= PAGE_SIZE)
591 kfree(info->entries[cpu]);
593 vfree(info->entries[cpu]);
597 EXPORT_SYMBOL(xt_free_table_info);
599 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
600 struct xt_table *xt_find_table_lock(int af, const char *name)
604 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
605 return ERR_PTR(-EINTR);
607 list_for_each_entry(t, &xt[af].tables, list)
608 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
610 mutex_unlock(&xt[af].mutex);
613 EXPORT_SYMBOL_GPL(xt_find_table_lock);
615 void xt_table_unlock(struct xt_table *table)
617 mutex_unlock(&xt[table->af].mutex);
619 EXPORT_SYMBOL_GPL(xt_table_unlock);
622 void xt_compat_lock(int af)
624 mutex_lock(&xt[af].compat_mutex);
626 EXPORT_SYMBOL_GPL(xt_compat_lock);
628 void xt_compat_unlock(int af)
630 mutex_unlock(&xt[af].compat_mutex);
632 EXPORT_SYMBOL_GPL(xt_compat_unlock);
635 struct xt_table_info *
636 xt_replace_table(struct xt_table *table,
637 unsigned int num_counters,
638 struct xt_table_info *newinfo,
641 struct xt_table_info *oldinfo, *private;
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);
655 table->private = newinfo;
656 newinfo->initial_entries = oldinfo->initial_entries;
657 write_unlock_bh(&table->lock);
661 EXPORT_SYMBOL_GPL(xt_replace_table);
663 struct xt_table *xt_register_table(struct xt_table *table,
664 struct xt_table_info *bootstrap,
665 struct xt_table_info *newinfo)
668 struct xt_table_info *private;
671 ret = mutex_lock_interruptible(&xt[table->af].mutex);
675 /* Don't autoload: we'd eat our tail... */
676 list_for_each_entry(t, &xt[table->af].tables, list) {
677 if (strcmp(t->name, table->name) == 0) {
683 /* Simplifies replace_table code. */
684 table->private = bootstrap;
685 rwlock_init(&table->lock);
686 if (!xt_replace_table(table, 0, newinfo, &ret))
689 private = table->private;
690 duprintf("table->private->number = %u\n", private->number);
692 /* save number of initial entries */
693 private->initial_entries = private->number;
695 list_add(&table->list, &xt[table->af].tables);
696 mutex_unlock(&xt[table->af].mutex);
700 mutex_unlock(&xt[table->af].mutex);
704 EXPORT_SYMBOL_GPL(xt_register_table);
706 void *xt_unregister_table(struct xt_table *table)
708 struct xt_table_info *private;
710 mutex_lock(&xt[table->af].mutex);
711 private = table->private;
712 list_del(&table->list);
713 mutex_unlock(&xt[table->af].mutex);
717 EXPORT_SYMBOL_GPL(xt_unregister_table);
719 #ifdef CONFIG_PROC_FS
720 static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos)
722 struct list_head *head = list->next;
724 if (!head || list_empty(list))
727 while (pos && (head = head->next)) {
732 return pos ? NULL : head;
735 static struct list_head *type2list(u_int16_t af, u_int16_t type)
737 struct list_head *list;
741 list = &xt[af].target;
744 list = &xt[af].match;
747 list = &xt[af].tables;
757 static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos)
759 struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private;
760 u_int16_t af = (unsigned long)pde->data & 0xffff;
761 u_int16_t type = (unsigned long)pde->data >> 16;
762 struct list_head *list;
767 list = type2list(af, type);
771 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
774 return xt_get_idx(list, seq, *pos);
777 static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
779 struct proc_dir_entry *pde = seq->private;
780 u_int16_t af = (unsigned long)pde->data & 0xffff;
781 u_int16_t type = (unsigned long)pde->data >> 16;
782 struct list_head *list;
787 list = type2list(af, type);
792 return xt_get_idx(list, seq, *pos);
795 static void xt_tgt_seq_stop(struct seq_file *seq, void *v)
797 struct proc_dir_entry *pde = seq->private;
798 u_int16_t af = (unsigned long)pde->data & 0xffff;
800 mutex_unlock(&xt[af].mutex);
803 static int xt_name_seq_show(struct seq_file *seq, void *v)
805 char *name = (char *)v + sizeof(struct list_head);
808 return seq_printf(seq, "%s\n", name);
813 static const struct seq_operations xt_tgt_seq_ops = {
814 .start = xt_tgt_seq_start,
815 .next = xt_tgt_seq_next,
816 .stop = xt_tgt_seq_stop,
817 .show = xt_name_seq_show,
820 static int xt_tgt_open(struct inode *inode, struct file *file)
824 ret = seq_open(file, &xt_tgt_seq_ops);
826 struct seq_file *seq = file->private_data;
827 struct proc_dir_entry *pde = PDE(inode);
835 static const struct file_operations xt_file_ops = {
836 .owner = THIS_MODULE,
840 .release = seq_release,
843 #define FORMAT_TABLES "_tables_names"
844 #define FORMAT_MATCHES "_tables_matches"
845 #define FORMAT_TARGETS "_tables_targets"
847 #endif /* CONFIG_PROC_FS */
849 int xt_proto_init(int af)
851 #ifdef CONFIG_PROC_FS
852 char buf[XT_FUNCTION_MAXNAMELEN];
853 struct proc_dir_entry *proc;
860 #ifdef CONFIG_PROC_FS
861 strlcpy(buf, xt_prefix[af], sizeof(buf));
862 strlcat(buf, FORMAT_TABLES, sizeof(buf));
863 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
866 proc->data = (void *) ((unsigned long) af | (TABLE << 16));
869 strlcpy(buf, xt_prefix[af], sizeof(buf));
870 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
871 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
873 goto out_remove_tables;
874 proc->data = (void *) ((unsigned long) af | (MATCH << 16));
876 strlcpy(buf, xt_prefix[af], sizeof(buf));
877 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
878 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
880 goto out_remove_matches;
881 proc->data = (void *) ((unsigned long) af | (TARGET << 16));
886 #ifdef CONFIG_PROC_FS
888 strlcpy(buf, xt_prefix[af], sizeof(buf));
889 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
890 proc_net_remove(&init_net, buf);
893 strlcpy(buf, xt_prefix[af], sizeof(buf));
894 strlcat(buf, FORMAT_TABLES, sizeof(buf));
895 proc_net_remove(&init_net, buf);
900 EXPORT_SYMBOL_GPL(xt_proto_init);
902 void xt_proto_fini(int af)
904 #ifdef CONFIG_PROC_FS
905 char buf[XT_FUNCTION_MAXNAMELEN];
907 strlcpy(buf, xt_prefix[af], sizeof(buf));
908 strlcat(buf, FORMAT_TABLES, sizeof(buf));
909 proc_net_remove(&init_net, buf);
911 strlcpy(buf, xt_prefix[af], sizeof(buf));
912 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
913 proc_net_remove(&init_net, buf);
915 strlcpy(buf, xt_prefix[af], sizeof(buf));
916 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
917 proc_net_remove(&init_net, buf);
918 #endif /*CONFIG_PROC_FS*/
920 EXPORT_SYMBOL_GPL(xt_proto_fini);
923 static int __init xt_init(void)
927 xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
931 for (i = 0; i < NPROTO; i++) {
932 mutex_init(&xt[i].mutex);
934 mutex_init(&xt[i].compat_mutex);
935 xt[i].compat_offsets = NULL;
937 INIT_LIST_HEAD(&xt[i].target);
938 INIT_LIST_HEAD(&xt[i].match);
939 INIT_LIST_HEAD(&xt[i].tables);
944 static void __exit xt_fini(void)
949 module_init(xt_init);
950 module_exit(xt_fini);