[PATCH] synclink_gt fix size of register value storage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / x_tables.c
blobd7817afc6b9654cd12499135d5e1b9c2617b5440
1 /*
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))
34 struct xt_af {
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)
45 #else
46 #define duprintf(format, args...)
47 #endif
49 enum {
50 TABLE,
51 TARGET,
52 MATCH,
55 /* Registration hooks for targets. */
56 int
57 xt_register_target(int af, struct xt_target *target)
59 int ret;
61 ret = down_interruptible(&xt[af].mutex);
62 if (ret != 0)
63 return ret;
64 list_add(&target->list, &xt[af].target);
65 up(&xt[af].mutex);
66 return ret;
68 EXPORT_SYMBOL(xt_register_target);
70 void
71 xt_unregister_target(int af, struct xt_target *target)
73 down(&xt[af].mutex);
74 LIST_DELETE(&xt[af].target, target);
75 up(&xt[af].mutex);
77 EXPORT_SYMBOL(xt_unregister_target);
79 int
80 xt_register_match(int af, struct xt_match *match)
82 int ret;
84 ret = down_interruptible(&xt[af].mutex);
85 if (ret != 0)
86 return ret;
88 list_add(&match->list, &xt[af].match);
89 up(&xt[af].mutex);
91 return ret;
93 EXPORT_SYMBOL(xt_register_match);
95 void
96 xt_unregister_match(int af, struct xt_match *match)
98 down(&xt[af].mutex);
99 LIST_DELETE(&xt[af].match, match);
100 up(&xt[af].mutex);
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)
114 struct xt_match *m;
115 int err = 0;
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)) {
124 up(&xt[af].mutex);
125 return m;
127 } else
128 err = -EPROTOTYPE; /* Found something. */
131 up(&xt[af].mutex);
132 return ERR_PTR(err);
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)
139 struct xt_target *t;
140 int err = 0;
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)) {
149 up(&xt[af].mutex);
150 return t;
152 } else
153 err = -EPROTOTYPE; /* Found something. */
156 up(&xt[af].mutex);
157 return ERR_PTR(err);
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)
174 return NULL;
175 return target;
177 EXPORT_SYMBOL_GPL(xt_request_find_target);
179 static int match_revfn(int af, const char *name, u8 revision, int *bestp)
181 struct xt_match *m;
182 int have_rev = 0;
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)
189 have_rev = 1;
192 return have_rev;
195 static int target_revfn(int af, const char *name, u8 revision, int *bestp)
197 struct xt_target *t;
198 int have_rev = 0;
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)
205 have_rev = 1;
208 return have_rev;
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,
213 int *err)
215 int have_rev, best = -1;
217 if (down_interruptible(&xt[af].mutex) != 0) {
218 *err = -EINTR;
219 return 1;
221 if (target == 1)
222 have_rev = target_revfn(af, name, revision, &best);
223 else
224 have_rev = match_revfn(af, name, revision, &best);
225 up(&xt[af].mutex);
227 /* Nothing at all? Return 0 to try loading module. */
228 if (best == -1) {
229 *err = -ENOENT;
230 return 0;
233 *err = best;
234 if (!have_rev)
235 *err = -EPROTONOSUPPORT;
236 return 1;
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;
243 int cpu;
245 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
246 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
247 return NULL;
249 newinfo = kzalloc(sizeof(struct xt_table_info), GFP_KERNEL);
250 if (!newinfo)
251 return NULL;
253 newinfo->size = size;
255 for_each_cpu(cpu) {
256 if (size <= PAGE_SIZE)
257 newinfo->entries[cpu] = kmalloc_node(size,
258 GFP_KERNEL,
259 cpu_to_node(cpu));
260 else
261 newinfo->entries[cpu] = vmalloc_node(size,
262 cpu_to_node(cpu));
264 if (newinfo->entries[cpu] == NULL) {
265 xt_free_table_info(newinfo);
266 return NULL;
270 return newinfo;
272 EXPORT_SYMBOL(xt_alloc_table_info);
274 void xt_free_table_info(struct xt_table_info *info)
276 int cpu;
278 for_each_cpu(cpu) {
279 if (info->size <= PAGE_SIZE)
280 kfree(info->entries[cpu]);
281 else
282 vfree(info->entries[cpu]);
284 kfree(info);
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)
291 struct xt_table *t;
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))
298 return t;
299 up(&xt[af].mutex);
300 return NULL;
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,
315 int *error)
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);
327 *error = -EAGAIN;
328 return NULL;
330 oldinfo = private;
331 table->private = newinfo;
332 newinfo->initial_entries = oldinfo->initial_entries;
333 write_unlock_bh(&table->lock);
335 return oldinfo;
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)
343 int ret;
344 struct xt_table_info *private;
346 ret = down_interruptible(&xt[table->af].mutex);
347 if (ret != 0)
348 return ret;
350 /* Don't autoload: we'd eat our tail... */
351 if (list_named_find(&xt[table->af].tables, table->name)) {
352 ret = -EEXIST;
353 goto unlock;
356 /* Simplifies replace_table code. */
357 table->private = bootstrap;
358 if (!xt_replace_table(table, 0, newinfo, &ret))
359 goto unlock;
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);
370 ret = 0;
371 unlock:
372 up(&xt[table->af].mutex);
373 return ret;
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);
386 return private;
388 EXPORT_SYMBOL_GPL(xt_unregister_table);
390 #ifdef CONFIG_PROC_FS
391 static char *xt_proto_prefix[NPROTO] = {
392 [AF_INET] = "ip",
393 [AF_INET6] = "ip6",
394 [NF_ARP] = "arp",
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))
402 return NULL;
404 while (pos && (head = head->next)) {
405 if (head == list)
406 return NULL;
407 pos--;
409 return pos ? NULL : head;
412 static struct list_head *type2list(u_int16_t af, u_int16_t type)
414 struct list_head *list;
416 switch (type) {
417 case TARGET:
418 list = &xt[af].target;
419 break;
420 case MATCH:
421 list = &xt[af].match;
422 break;
423 case TABLE:
424 list = &xt[af].tables;
425 break;
426 default:
427 list = NULL;
428 break;
431 return list;
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;
441 if (af >= NPROTO)
442 return NULL;
444 list = type2list(af, type);
445 if (!list)
446 return NULL;
448 if (down_interruptible(&xt[af].mutex) != 0)
449 return NULL;
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;
461 if (af >= NPROTO)
462 return NULL;
464 list = type2list(af, type);
465 if (!list)
466 return NULL;
468 (*pos)++;
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;
477 up(&xt[af].mutex);
480 static int xt_name_seq_show(struct seq_file *seq, void *v)
482 char *name = (char *)v + sizeof(struct list_head);
484 if (strlen(name))
485 return seq_printf(seq, "%s\n", name);
486 else
487 return 0;
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)
499 int ret;
501 ret = seq_open(file, &xt_tgt_seq_ops);
502 if (!ret) {
503 struct seq_file *seq = file->private_data;
504 struct proc_dir_entry *pde = PDE(inode);
506 seq->private = pde;
509 return ret;
512 static struct file_operations xt_file_ops = {
513 .owner = THIS_MODULE,
514 .open = xt_tgt_open,
515 .read = seq_read,
516 .llseek = seq_lseek,
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;
531 #endif
533 if (af >= NPROTO)
534 return -EINVAL;
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);
541 if (!proc)
542 goto out;
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);
549 if (!proc)
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);
556 if (!proc)
557 goto out_remove_matches;
558 proc->data = (void *) ((unsigned long) af | (TARGET << 16));
559 #endif
561 return 0;
563 #ifdef CONFIG_PROC_FS
564 out_remove_matches:
565 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
566 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
567 proc_net_remove(buf);
569 out_remove_tables:
570 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
571 strlcat(buf, FORMAT_TABLES, sizeof(buf));
572 proc_net_remove(buf);
573 out:
574 return -1;
575 #endif
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)
602 int i;
604 xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
605 if (!xt)
606 return -ENOMEM;
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);
614 return 0;
617 static void __exit xt_fini(void)
619 kfree(xt);
622 module_init(xt_init);
623 module_exit(xt_fini);