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 /* Registration hooks for targets. */
57 xt_register_target(int af
, struct xt_target
*target
)
61 ret
= down_interruptible(&xt
[af
].mutex
);
64 list_add(&target
->list
, &xt
[af
].target
);
68 EXPORT_SYMBOL(xt_register_target
);
71 xt_unregister_target(int af
, struct xt_target
*target
)
74 LIST_DELETE(&xt
[af
].target
, target
);
77 EXPORT_SYMBOL(xt_unregister_target
);
80 xt_register_match(int af
, struct xt_match
*match
)
84 ret
= down_interruptible(&xt
[af
].mutex
);
88 list_add(&match
->list
, &xt
[af
].match
);
93 EXPORT_SYMBOL(xt_register_match
);
96 xt_unregister_match(int af
, struct xt_match
*match
)
99 LIST_DELETE(&xt
[af
].match
, match
);
102 EXPORT_SYMBOL(xt_unregister_match
);
106 * These are weird, but module loading must not be done with mutex
107 * held (since they will register), and we have to have a single
108 * function to use try_then_request_module().
111 /* Find match, grabs ref. Returns ERR_PTR() on error. */
112 struct xt_match
*xt_find_match(int af
, const char *name
, u8 revision
)
117 if (down_interruptible(&xt
[af
].mutex
) != 0)
118 return ERR_PTR(-EINTR
);
120 list_for_each_entry(m
, &xt
[af
].match
, list
) {
121 if (strcmp(m
->name
, name
) == 0) {
122 if (m
->revision
== revision
) {
123 if (try_module_get(m
->me
)) {
128 err
= -EPROTOTYPE
; /* Found something. */
134 EXPORT_SYMBOL(xt_find_match
);
136 /* Find target, grabs ref. Returns ERR_PTR() on error. */
137 struct xt_target
*xt_find_target(int af
, const char *name
, u8 revision
)
142 if (down_interruptible(&xt
[af
].mutex
) != 0)
143 return ERR_PTR(-EINTR
);
145 list_for_each_entry(t
, &xt
[af
].target
, list
) {
146 if (strcmp(t
->name
, name
) == 0) {
147 if (t
->revision
== revision
) {
148 if (try_module_get(t
->me
)) {
153 err
= -EPROTOTYPE
; /* Found something. */
159 EXPORT_SYMBOL(xt_find_target
);
161 static const char *xt_prefix
[NPROTO
] = {
162 [AF_INET
] = "ipt_%s",
163 [AF_INET6
] = "ip6t_%s",
164 [NF_ARP
] = "arpt_%s",
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 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 struct xt_table_info
*xt_alloc_table_info(unsigned int size
)
242 struct xt_table_info
*newinfo
;
245 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
246 if ((SMP_ALIGN(size
) >> PAGE_SHIFT
) + 2 > num_physpages
)
249 newinfo
= kzalloc(sizeof(struct xt_table_info
), GFP_KERNEL
);
253 newinfo
->size
= size
;
256 if (size
<= PAGE_SIZE
)
257 newinfo
->entries
[cpu
] = kmalloc_node(size
,
261 newinfo
->entries
[cpu
] = vmalloc_node(size
,
264 if (newinfo
->entries
[cpu
] == NULL
) {
265 xt_free_table_info(newinfo
);
272 EXPORT_SYMBOL(xt_alloc_table_info
);
274 void xt_free_table_info(struct xt_table_info
*info
)
279 if (info
->size
<= PAGE_SIZE
)
280 kfree(info
->entries
[cpu
]);
282 vfree(info
->entries
[cpu
]);
286 EXPORT_SYMBOL(xt_free_table_info
);
288 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
289 struct xt_table
*xt_find_table_lock(int af
, const char *name
)
293 if (down_interruptible(&xt
[af
].mutex
) != 0)
294 return ERR_PTR(-EINTR
);
296 list_for_each_entry(t
, &xt
[af
].tables
, list
)
297 if (strcmp(t
->name
, name
) == 0 && try_module_get(t
->me
))
302 EXPORT_SYMBOL_GPL(xt_find_table_lock
);
304 void xt_table_unlock(struct xt_table
*table
)
306 up(&xt
[table
->af
].mutex
);
308 EXPORT_SYMBOL_GPL(xt_table_unlock
);
311 struct xt_table_info
*
312 xt_replace_table(struct xt_table
*table
,
313 unsigned int num_counters
,
314 struct xt_table_info
*newinfo
,
317 struct xt_table_info
*oldinfo
, *private;
319 /* Do the substitution. */
320 write_lock_bh(&table
->lock
);
321 private = table
->private;
322 /* Check inside lock: is the old number correct? */
323 if (num_counters
!= private->number
) {
324 duprintf("num_counters != table->private->number (%u/%u)\n",
325 num_counters
, private->number
);
326 write_unlock_bh(&table
->lock
);
331 table
->private = newinfo
;
332 newinfo
->initial_entries
= oldinfo
->initial_entries
;
333 write_unlock_bh(&table
->lock
);
337 EXPORT_SYMBOL_GPL(xt_replace_table
);
339 int xt_register_table(struct xt_table
*table
,
340 struct xt_table_info
*bootstrap
,
341 struct xt_table_info
*newinfo
)
344 struct xt_table_info
*private;
346 ret
= down_interruptible(&xt
[table
->af
].mutex
);
350 /* Don't autoload: we'd eat our tail... */
351 if (list_named_find(&xt
[table
->af
].tables
, table
->name
)) {
356 /* Simplifies replace_table code. */
357 table
->private = bootstrap
;
358 if (!xt_replace_table(table
, 0, newinfo
, &ret
))
361 private = table
->private;
362 duprintf("table->private->number = %u\n", private->number
);
364 /* save number of initial entries */
365 private->initial_entries
= private->number
;
367 rwlock_init(&table
->lock
);
368 list_prepend(&xt
[table
->af
].tables
, table
);
372 up(&xt
[table
->af
].mutex
);
375 EXPORT_SYMBOL_GPL(xt_register_table
);
377 void *xt_unregister_table(struct xt_table
*table
)
379 struct xt_table_info
*private;
381 down(&xt
[table
->af
].mutex
);
382 private = table
->private;
383 LIST_DELETE(&xt
[table
->af
].tables
, table
);
384 up(&xt
[table
->af
].mutex
);
388 EXPORT_SYMBOL_GPL(xt_unregister_table
);
390 #ifdef CONFIG_PROC_FS
391 static char *xt_proto_prefix
[NPROTO
] = {
397 static struct list_head
*xt_get_idx(struct list_head
*list
, struct seq_file
*seq
, loff_t pos
)
399 struct list_head
*head
= list
->next
;
401 if (!head
|| list_empty(list
))
404 while (pos
&& (head
= head
->next
)) {
409 return pos
? NULL
: head
;
412 static struct list_head
*type2list(u_int16_t af
, u_int16_t type
)
414 struct list_head
*list
;
418 list
= &xt
[af
].target
;
421 list
= &xt
[af
].match
;
424 list
= &xt
[af
].tables
;
434 static void *xt_tgt_seq_start(struct seq_file
*seq
, loff_t
*pos
)
436 struct proc_dir_entry
*pde
= (struct proc_dir_entry
*) seq
->private;
437 u_int16_t af
= (unsigned long)pde
->data
& 0xffff;
438 u_int16_t type
= (unsigned long)pde
->data
>> 16;
439 struct list_head
*list
;
444 list
= type2list(af
, type
);
448 if (down_interruptible(&xt
[af
].mutex
) != 0)
451 return xt_get_idx(list
, seq
, *pos
);
454 static void *xt_tgt_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
456 struct proc_dir_entry
*pde
= seq
->private;
457 u_int16_t af
= (unsigned long)pde
->data
& 0xffff;
458 u_int16_t type
= (unsigned long)pde
->data
>> 16;
459 struct list_head
*list
;
464 list
= type2list(af
, type
);
469 return xt_get_idx(list
, seq
, *pos
);
472 static void xt_tgt_seq_stop(struct seq_file
*seq
, void *v
)
474 struct proc_dir_entry
*pde
= seq
->private;
475 u_int16_t af
= (unsigned long)pde
->data
& 0xffff;
480 static int xt_name_seq_show(struct seq_file
*seq
, void *v
)
482 char *name
= (char *)v
+ sizeof(struct list_head
);
485 return seq_printf(seq
, "%s\n", name
);
490 static struct seq_operations xt_tgt_seq_ops
= {
491 .start
= xt_tgt_seq_start
,
492 .next
= xt_tgt_seq_next
,
493 .stop
= xt_tgt_seq_stop
,
494 .show
= xt_name_seq_show
,
497 static int xt_tgt_open(struct inode
*inode
, struct file
*file
)
501 ret
= seq_open(file
, &xt_tgt_seq_ops
);
503 struct seq_file
*seq
= file
->private_data
;
504 struct proc_dir_entry
*pde
= PDE(inode
);
512 static struct file_operations xt_file_ops
= {
513 .owner
= THIS_MODULE
,
517 .release
= seq_release
,
520 #define FORMAT_TABLES "_tables_names"
521 #define FORMAT_MATCHES "_tables_matches"
522 #define FORMAT_TARGETS "_tables_targets"
524 #endif /* CONFIG_PROC_FS */
526 int xt_proto_init(int af
)
528 #ifdef CONFIG_PROC_FS
529 char buf
[XT_FUNCTION_MAXNAMELEN
];
530 struct proc_dir_entry
*proc
;
537 #ifdef CONFIG_PROC_FS
538 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
539 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
540 proc
= proc_net_fops_create(buf
, 0440, &xt_file_ops
);
543 proc
->data
= (void *) ((unsigned long) af
| (TABLE
<< 16));
546 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
547 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
548 proc
= proc_net_fops_create(buf
, 0440, &xt_file_ops
);
550 goto out_remove_tables
;
551 proc
->data
= (void *) ((unsigned long) af
| (MATCH
<< 16));
553 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
554 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
555 proc
= proc_net_fops_create(buf
, 0440, &xt_file_ops
);
557 goto out_remove_matches
;
558 proc
->data
= (void *) ((unsigned long) af
| (TARGET
<< 16));
563 #ifdef CONFIG_PROC_FS
565 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
566 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
567 proc_net_remove(buf
);
570 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
571 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
572 proc_net_remove(buf
);
577 EXPORT_SYMBOL_GPL(xt_proto_init
);
579 void xt_proto_fini(int af
)
581 #ifdef CONFIG_PROC_FS
582 char buf
[XT_FUNCTION_MAXNAMELEN
];
584 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
585 strlcat(buf
, FORMAT_TABLES
, sizeof(buf
));
586 proc_net_remove(buf
);
588 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
589 strlcat(buf
, FORMAT_TARGETS
, sizeof(buf
));
590 proc_net_remove(buf
);
592 strlcpy(buf
, xt_proto_prefix
[af
], sizeof(buf
));
593 strlcat(buf
, FORMAT_MATCHES
, sizeof(buf
));
594 proc_net_remove(buf
);
595 #endif /*CONFIG_PROC_FS*/
597 EXPORT_SYMBOL_GPL(xt_proto_fini
);
600 static int __init
xt_init(void)
604 xt
= kmalloc(sizeof(struct xt_af
) * NPROTO
, GFP_KERNEL
);
608 for (i
= 0; i
< NPROTO
; i
++) {
609 init_MUTEX(&xt
[i
].mutex
);
610 INIT_LIST_HEAD(&xt
[i
].target
);
611 INIT_LIST_HEAD(&xt
[i
].match
);
612 INIT_LIST_HEAD(&xt
[i
].tables
);
617 static void __exit
xt_fini(void)
622 module_init(xt_init
);
623 module_exit(xt_fini
);