[CONNECTOR]: Use netlink_has_listeners() to avoind unnecessary allocations.
[linux-2.6/verdex.git] / net / netfilter / x_tables.c
blob750b928297660b141db59c5f45ec8d4be0321f77
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 static const char *xt_prefix[NPROTO] = {
56 [AF_INET] = "ip",
57 [AF_INET6] = "ip6",
58 [NF_ARP] = "arp",
61 /* Registration hooks for targets. */
62 int
63 xt_register_target(int af, struct xt_target *target)
65 int ret;
67 ret = down_interruptible(&xt[af].mutex);
68 if (ret != 0)
69 return ret;
70 list_add(&target->list, &xt[af].target);
71 up(&xt[af].mutex);
72 return ret;
74 EXPORT_SYMBOL(xt_register_target);
76 void
77 xt_unregister_target(int af, struct xt_target *target)
79 down(&xt[af].mutex);
80 LIST_DELETE(&xt[af].target, target);
81 up(&xt[af].mutex);
83 EXPORT_SYMBOL(xt_unregister_target);
85 int
86 xt_register_match(int af, struct xt_match *match)
88 int ret;
90 ret = down_interruptible(&xt[af].mutex);
91 if (ret != 0)
92 return ret;
94 list_add(&match->list, &xt[af].match);
95 up(&xt[af].mutex);
97 return ret;
99 EXPORT_SYMBOL(xt_register_match);
101 void
102 xt_unregister_match(int af, struct xt_match *match)
104 down(&xt[af].mutex);
105 LIST_DELETE(&xt[af].match, match);
106 up(&xt[af].mutex);
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)
120 struct xt_match *m;
121 int err = 0;
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)) {
130 up(&xt[af].mutex);
131 return m;
133 } else
134 err = -EPROTOTYPE; /* Found something. */
137 up(&xt[af].mutex);
138 return ERR_PTR(err);
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)
145 struct xt_target *t;
146 int err = 0;
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)) {
155 up(&xt[af].mutex);
156 return t;
158 } else
159 err = -EPROTOTYPE; /* Found something. */
162 up(&xt[af].mutex);
163 return ERR_PTR(err);
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)
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 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);
248 return -EINVAL;
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);
253 return -EINVAL;
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);
258 return -EINVAL;
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);
263 return -EINVAL;
265 return 0;
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);
277 return -EINVAL;
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);
282 return -EINVAL;
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);
287 return -EINVAL;
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);
292 return -EINVAL;
294 return 0;
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;
301 int cpu;
303 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
304 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
305 return NULL;
307 newinfo = kzalloc(sizeof(struct xt_table_info), GFP_KERNEL);
308 if (!newinfo)
309 return NULL;
311 newinfo->size = size;
313 for_each_cpu(cpu) {
314 if (size <= PAGE_SIZE)
315 newinfo->entries[cpu] = kmalloc_node(size,
316 GFP_KERNEL,
317 cpu_to_node(cpu));
318 else
319 newinfo->entries[cpu] = vmalloc_node(size,
320 cpu_to_node(cpu));
322 if (newinfo->entries[cpu] == NULL) {
323 xt_free_table_info(newinfo);
324 return NULL;
328 return newinfo;
330 EXPORT_SYMBOL(xt_alloc_table_info);
332 void xt_free_table_info(struct xt_table_info *info)
334 int cpu;
336 for_each_cpu(cpu) {
337 if (info->size <= PAGE_SIZE)
338 kfree(info->entries[cpu]);
339 else
340 vfree(info->entries[cpu]);
342 kfree(info);
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)
349 struct xt_table *t;
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))
356 return t;
357 up(&xt[af].mutex);
358 return NULL;
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,
373 int *error)
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);
385 *error = -EAGAIN;
386 return NULL;
388 oldinfo = private;
389 table->private = newinfo;
390 newinfo->initial_entries = oldinfo->initial_entries;
391 write_unlock_bh(&table->lock);
393 return oldinfo;
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)
401 int ret;
402 struct xt_table_info *private;
404 ret = down_interruptible(&xt[table->af].mutex);
405 if (ret != 0)
406 return ret;
408 /* Don't autoload: we'd eat our tail... */
409 if (list_named_find(&xt[table->af].tables, table->name)) {
410 ret = -EEXIST;
411 goto unlock;
414 /* Simplifies replace_table code. */
415 table->private = bootstrap;
416 if (!xt_replace_table(table, 0, newinfo, &ret))
417 goto unlock;
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);
428 ret = 0;
429 unlock:
430 up(&xt[table->af].mutex);
431 return ret;
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);
444 return private;
446 EXPORT_SYMBOL_GPL(xt_unregister_table);
448 #ifdef CONFIG_PROC_FS
449 static char *xt_proto_prefix[NPROTO] = {
450 [AF_INET] = "ip",
451 [AF_INET6] = "ip6",
452 [NF_ARP] = "arp",
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))
460 return NULL;
462 while (pos && (head = head->next)) {
463 if (head == list)
464 return NULL;
465 pos--;
467 return pos ? NULL : head;
470 static struct list_head *type2list(u_int16_t af, u_int16_t type)
472 struct list_head *list;
474 switch (type) {
475 case TARGET:
476 list = &xt[af].target;
477 break;
478 case MATCH:
479 list = &xt[af].match;
480 break;
481 case TABLE:
482 list = &xt[af].tables;
483 break;
484 default:
485 list = NULL;
486 break;
489 return list;
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;
499 if (af >= NPROTO)
500 return NULL;
502 list = type2list(af, type);
503 if (!list)
504 return NULL;
506 if (down_interruptible(&xt[af].mutex) != 0)
507 return NULL;
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;
519 if (af >= NPROTO)
520 return NULL;
522 list = type2list(af, type);
523 if (!list)
524 return NULL;
526 (*pos)++;
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;
535 up(&xt[af].mutex);
538 static int xt_name_seq_show(struct seq_file *seq, void *v)
540 char *name = (char *)v + sizeof(struct list_head);
542 if (strlen(name))
543 return seq_printf(seq, "%s\n", name);
544 else
545 return 0;
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)
557 int ret;
559 ret = seq_open(file, &xt_tgt_seq_ops);
560 if (!ret) {
561 struct seq_file *seq = file->private_data;
562 struct proc_dir_entry *pde = PDE(inode);
564 seq->private = pde;
567 return ret;
570 static struct file_operations xt_file_ops = {
571 .owner = THIS_MODULE,
572 .open = xt_tgt_open,
573 .read = seq_read,
574 .llseek = seq_lseek,
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;
589 #endif
591 if (af >= NPROTO)
592 return -EINVAL;
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);
599 if (!proc)
600 goto out;
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);
607 if (!proc)
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);
614 if (!proc)
615 goto out_remove_matches;
616 proc->data = (void *) ((unsigned long) af | (TARGET << 16));
617 #endif
619 return 0;
621 #ifdef CONFIG_PROC_FS
622 out_remove_matches:
623 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
624 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
625 proc_net_remove(buf);
627 out_remove_tables:
628 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
629 strlcat(buf, FORMAT_TABLES, sizeof(buf));
630 proc_net_remove(buf);
631 out:
632 return -1;
633 #endif
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)
660 int i;
662 xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
663 if (!xt)
664 return -ENOMEM;
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);
672 return 0;
675 static void __exit xt_fini(void)
677 kfree(xt);
680 module_init(xt_init);
681 module_exit(xt_fini);