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>
25 #include <linux/netfilter/x_tables.h>
26 #include <linux/netfilter_arp.h>
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
30 MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module");
32 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
35 struct semaphore mutex
;
36 struct list_head match
;
37 struct list_head target
;
38 struct list_head tables
;
41 static struct xt_af
*xt
;
43 #ifdef DEBUG_IP_FIREWALL_USER
44 #define duprintf(format, args...) printk(format , ## args)
46 #define duprintf(format, args...)
55 static const char *xt_prefix
[NPROTO
] = {
61 /* Registration hooks for targets. */
63 xt_register_target(int af
, struct xt_target
*target
)
67 ret
= down_interruptible(&xt
[af
].mutex
);
70 list_add(&target
->list
, &xt
[af
].target
);
74 EXPORT_SYMBOL(xt_register_target
);
77 xt_unregister_target(int af
, struct xt_target
*target
)
80 LIST_DELETE(&xt
[af
].target
, target
);
83 EXPORT_SYMBOL(xt_unregister_target
);
86 xt_register_match(int af
, struct xt_match
*match
)
90 ret
= down_interruptible(&xt
[af
].mutex
);
94 list_add(&match
->list
, &xt
[af
].match
);
99 EXPORT_SYMBOL(xt_register_match
);
102 xt_unregister_match(int af
, struct xt_match
*match
)
105 LIST_DELETE(&xt
[af
].match
, match
);
108 EXPORT_SYMBOL(xt_unregister_match
);
112 * These are weird, but module loading must not be done with mutex
113 * held (since they will register), and we have to have a single
114 * function to use try_then_request_module().
117 /* Find match, grabs ref. Returns ERR_PTR() on error. */
118 struct xt_match
*xt_find_match(int af
, const char *name
, u8 revision
)
123 if (down_interruptible(&xt
[af
].mutex
) != 0)
124 return ERR_PTR(-EINTR
);
126 list_for_each_entry(m
, &xt
[af
].match
, list
) {
127 if (strcmp(m
->name
, name
) == 0) {
128 if (m
->revision
== revision
) {
129 if (try_module_get(m
->me
)) {
134 err
= -EPROTOTYPE
; /* Found something. */
140 EXPORT_SYMBOL(xt_find_match
);
142 /* Find target, grabs ref. Returns ERR_PTR() on error. */
143 struct xt_target
*xt_find_target(int af
, const char *name
, u8 revision
)
148 if (down_interruptible(&xt
[af
].mutex
) != 0)
149 return ERR_PTR(-EINTR
);
151 list_for_each_entry(t
, &xt
[af
].target
, list
) {
152 if (strcmp(t
->name
, name
) == 0) {
153 if (t
->revision
== revision
) {
154 if (try_module_get(t
->me
)) {
159 err
= -EPROTOTYPE
; /* Found something. */
165 EXPORT_SYMBOL(xt_find_target
);
167 struct xt_target
*xt_request_find_target(int af
, const char *name
, u8 revision
)
169 struct xt_target
*target
;
171 target
= try_then_request_module(xt_find_target(af
, name
, revision
),
172 "%st_%s", xt_prefix
[af
], name
);
173 if (IS_ERR(target
) || !target
)
177 EXPORT_SYMBOL_GPL(xt_request_find_target
);
179 static int match_revfn(int af
, const char *name
, u8 revision
, int *bestp
)
184 list_for_each_entry(m
, &xt
[af
].match
, list
) {
185 if (strcmp(m
->name
, name
) == 0) {
186 if (m
->revision
> *bestp
)
187 *bestp
= m
->revision
;
188 if (m
->revision
== revision
)
195 static int target_revfn(int af
, const char *name
, u8 revision
, int *bestp
)
200 list_for_each_entry(t
, &xt
[af
].target
, list
) {
201 if (strcmp(t
->name
, name
) == 0) {
202 if (t
->revision
> *bestp
)
203 *bestp
= t
->revision
;
204 if (t
->revision
== revision
)
211 /* Returns true or false (if no such extension at all) */
212 int xt_find_revision(int af
, const char *name
, u8 revision
, int target
,
215 int have_rev
, best
= -1;
217 if (down_interruptible(&xt
[af
].mutex
) != 0) {
222 have_rev
= target_revfn(af
, name
, revision
, &best
);
224 have_rev
= match_revfn(af
, name
, revision
, &best
);
227 /* Nothing at all? Return 0 to try loading module. */
235 *err
= -EPROTONOSUPPORT
;
238 EXPORT_SYMBOL_GPL(xt_find_revision
);
240 int xt_check_match(const struct xt_match
*match
, unsigned short family
,
241 unsigned int size
, const char *table
, unsigned int hook_mask
,
242 unsigned short proto
, int inv_proto
)
244 if (XT_ALIGN(match
->matchsize
) != size
) {
245 printk("%s_tables: %s match: invalid size %Zu != %u\n",
246 xt_prefix
[family
], match
->name
,
247 XT_ALIGN(match
->matchsize
), size
);
250 if (match
->table
&& strcmp(match
->table
, table
)) {
251 printk("%s_tables: %s match: only valid in %s table, not %s\n",
252 xt_prefix
[family
], match
->name
, match
->table
, table
);
255 if (match
->hooks
&& (hook_mask
& ~match
->hooks
) != 0) {
256 printk("%s_tables: %s match: bad hook_mask %u\n",
257 xt_prefix
[family
], match
->name
, hook_mask
);
260 if (match
->proto
&& (match
->proto
!= proto
|| inv_proto
)) {
261 printk("%s_tables: %s match: only valid for protocol %u\n",
262 xt_prefix
[family
], match
->name
, match
->proto
);
267 EXPORT_SYMBOL_GPL(xt_check_match
);
269 int xt_check_target(const struct xt_target
*target
, unsigned short family
,
270 unsigned int size
, const char *table
, unsigned int hook_mask
,
271 unsigned short proto
, int inv_proto
)
273 if (XT_ALIGN(target
->targetsize
) != size
) {
274 printk("%s_tables: %s target: invalid size %Zu != %u\n",
275 xt_prefix
[family
], target
->name
,
276 XT_ALIGN(target
->targetsize
), size
);
279 if (target
->table
&& strcmp(target
->table
, table
)) {
280 printk("%s_tables: %s target: only valid in %s table, not %s\n",
281 xt_prefix
[family
], target
->name
, target
->table
, table
);
284 if (target
->hooks
&& (hook_mask
& ~target
->hooks
) != 0) {
285 printk("%s_tables: %s target: bad hook_mask %u\n",
286 xt_prefix
[family
], target
->name
, hook_mask
);
289 if (target
->proto
&& (target
->proto
!= proto
|| inv_proto
)) {
290 printk("%s_tables: %s target: only valid for protocol %u\n",
291 xt_prefix
[family
], target
->name
, target
->proto
);
296 EXPORT_SYMBOL_GPL(xt_check_target
);
298 struct xt_table_info
*xt_alloc_table_info(unsigned int size
)
300 struct xt_table_info
*newinfo
;
303 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
304 if ((SMP_ALIGN(size
) >> PAGE_SHIFT
) + 2 > num_physpages
)
307 newinfo
= kzalloc(sizeof(struct xt_table_info
), GFP_KERNEL
);
311 newinfo
->size
= size
;
314 if (size
<= PAGE_SIZE
)
315 newinfo
->entries
[cpu
] = kmalloc_node(size
,
319 newinfo
->entries
[cpu
] = vmalloc_node(size
,
322 if (newinfo
->entries
[cpu
] == NULL
) {
323 xt_free_table_info(newinfo
);
330 EXPORT_SYMBOL(xt_alloc_table_info
);
332 void xt_free_table_info(struct xt_table_info
*info
)
337 if (info
->size
<= PAGE_SIZE
)
338 kfree(info
->entries
[cpu
]);
340 vfree(info
->entries
[cpu
]);
344 EXPORT_SYMBOL(xt_free_table_info
);
346 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
347 struct xt_table
*xt_find_table_lock(int af
, const char *name
)
351 if (down_interruptible(&xt
[af
].mutex
) != 0)
352 return ERR_PTR(-EINTR
);
354 list_for_each_entry(t
, &xt
[af
].tables
, list
)
355 if (strcmp(t
->name
, name
) == 0 && try_module_get(t
->me
))
360 EXPORT_SYMBOL_GPL(xt_find_table_lock
);
362 void xt_table_unlock(struct xt_table
*table
)
364 up(&xt
[table
->af
].mutex
);
366 EXPORT_SYMBOL_GPL(xt_table_unlock
);
369 struct xt_table_info
*
370 xt_replace_table(struct xt_table
*table
,
371 unsigned int num_counters
,
372 struct xt_table_info
*newinfo
,
375 struct xt_table_info
*oldinfo
, *private;
377 /* Do the substitution. */
378 write_lock_bh(&table
->lock
);
379 private = table
->private;
380 /* Check inside lock: is the old number correct? */
381 if (num_counters
!= private->number
) {
382 duprintf("num_counters != table->private->number (%u/%u)\n",
383 num_counters
, private->number
);
384 write_unlock_bh(&table
->lock
);
389 table
->private = newinfo
;
390 newinfo
->initial_entries
= oldinfo
->initial_entries
;
391 write_unlock_bh(&table
->lock
);
395 EXPORT_SYMBOL_GPL(xt_replace_table
);
397 int xt_register_table(struct xt_table
*table
,
398 struct xt_table_info
*bootstrap
,
399 struct xt_table_info
*newinfo
)
402 struct xt_table_info
*private;
404 ret
= down_interruptible(&xt
[table
->af
].mutex
);
408 /* Don't autoload: we'd eat our tail... */
409 if (list_named_find(&xt
[table
->af
].tables
, table
->name
)) {
414 /* Simplifies replace_table code. */
415 table
->private = bootstrap
;
416 if (!xt_replace_table(table
, 0, newinfo
, &ret
))
419 private = table
->private;
420 duprintf("table->private->number = %u\n", private->number
);
422 /* save number of initial entries */
423 private->initial_entries
= private->number
;
425 rwlock_init(&table
->lock
);
426 list_prepend(&xt
[table
->af
].tables
, table
);
430 up(&xt
[table
->af
].mutex
);
433 EXPORT_SYMBOL_GPL(xt_register_table
);
435 void *xt_unregister_table(struct xt_table
*table
)
437 struct xt_table_info
*private;
439 down(&xt
[table
->af
].mutex
);
440 private = table
->private;
441 LIST_DELETE(&xt
[table
->af
].tables
, table
);
442 up(&xt
[table
->af
].mutex
);
446 EXPORT_SYMBOL_GPL(xt_unregister_table
);
448 #ifdef CONFIG_PROC_FS
449 static char *xt_proto_prefix
[NPROTO
] = {
455 static struct list_head
*xt_get_idx(struct list_head
*list
, struct seq_file
*seq
, loff_t pos
)
457 struct list_head
*head
= list
->next
;
459 if (!head
|| list_empty(list
))
462 while (pos
&& (head
= head
->next
)) {
467 return pos
? NULL
: head
;
470 static struct list_head
*type2list(u_int16_t af
, u_int16_t type
)
472 struct list_head
*list
;
476 list
= &xt
[af
].target
;
479 list
= &xt
[af
].match
;
482 list
= &xt
[af
].tables
;
492 static void *xt_tgt_seq_start(struct seq_file
*seq
, loff_t
*pos
)
494 struct proc_dir_entry
*pde
= (struct proc_dir_entry
*) seq
->private;
495 u_int16_t af
= (unsigned long)pde
->data
& 0xffff;
496 u_int16_t type
= (unsigned long)pde
->data
>> 16;
497 struct list_head
*list
;
502 list
= type2list(af
, type
);
506 if (down_interruptible(&xt
[af
].mutex
) != 0)
509 return xt_get_idx(list
, seq
, *pos
);
512 static void *xt_tgt_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
514 struct proc_dir_entry
*pde
= seq
->private;
515 u_int16_t af
= (unsigned long)pde
->data
& 0xffff;
516 u_int16_t type
= (unsigned long)pde
->data
>> 16;
517 struct list_head
*list
;
522 list
= type2list(af
, type
);
527 return xt_get_idx(list
, seq
, *pos
);
530 static void xt_tgt_seq_stop(struct seq_file
*seq
, void *v
)
532 struct proc_dir_entry
*pde
= seq
->private;
533 u_int16_t af
= (unsigned long)pde
->data
& 0xffff;
538 static int xt_name_seq_show(struct seq_file
*seq
, void *v
)
540 char *name
= (char *)v
+ sizeof(struct list_head
);
543 return seq_printf(seq
, "%s\n", name
);
548 static struct seq_operations xt_tgt_seq_ops
= {
549 .start
= xt_tgt_seq_start
,
550 .next
= xt_tgt_seq_next
,
551 .stop
= xt_tgt_seq_stop
,
552 .show
= xt_name_seq_show
,
555 static int xt_tgt_open(struct inode
*inode
, struct file
*file
)
559 ret
= seq_open(file
, &xt_tgt_seq_ops
);
561 struct seq_file
*seq
= file
->private_data
;
562 struct proc_dir_entry
*pde
= PDE(inode
);
570 static struct file_operations xt_file_ops
= {
571 .owner
= THIS_MODULE
,
575 .release
= seq_release
,
578 #define FORMAT_TABLES "_tables_names"
579 #define FORMAT_MATCHES "_tables_matches"
580 #define FORMAT_TARGETS "_tables_targets"
582 #endif /* CONFIG_PROC_FS */
584 int xt_proto_init(int af
)
586 #ifdef CONFIG_PROC_FS
587 char buf
[XT_FUNCTION_MAXNAMELEN
];
588 struct proc_dir_entry
*proc
;
595 #ifdef CONFIG_PROC_FS
596 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
597 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
598 proc
= proc_net_fops_create(buf
, 0440, &xt_file_ops
);
601 proc
->data
= (void *) ((unsigned long) af
| (TABLE
<< 16));
604 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
605 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
606 proc
= proc_net_fops_create(buf
, 0440, &xt_file_ops
);
608 goto out_remove_tables
;
609 proc
->data
= (void *) ((unsigned long) af
| (MATCH
<< 16));
611 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
612 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
613 proc
= proc_net_fops_create(buf
, 0440, &xt_file_ops
);
615 goto out_remove_matches
;
616 proc
->data
= (void *) ((unsigned long) af
| (TARGET
<< 16));
621 #ifdef CONFIG_PROC_FS
623 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
624 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
625 proc_net_remove(buf
);
628 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
629 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
630 proc_net_remove(buf
);
635 EXPORT_SYMBOL_GPL(xt_proto_init
);
637 void xt_proto_fini(int af
)
639 #ifdef CONFIG_PROC_FS
640 char buf
[XT_FUNCTION_MAXNAMELEN
];
642 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
643 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
644 proc_net_remove(buf
);
646 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
647 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
648 proc_net_remove(buf
);
650 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
651 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
652 proc_net_remove(buf
);
653 #endif /*CONFIG_PROC_FS*/
655 EXPORT_SYMBOL_GPL(xt_proto_fini
);
658 static int __init
xt_init(void)
662 xt
= kmalloc(sizeof(struct xt_af
) * NPROTO
, GFP_KERNEL
);
666 for (i
= 0; i
< NPROTO
; i
++) {
667 init_MUTEX(&xt
[i
].mutex
);
668 INIT_LIST_HEAD(&xt
[i
].target
);
669 INIT_LIST_HEAD(&xt
[i
].match
);
670 INIT_LIST_HEAD(&xt
[i
].tables
);
675 static void __exit
xt_fini(void)
680 module_init(xt_init
);
681 module_exit(xt_fini
);