[NETFILTER]: x_tables: semi-rewrite of /proc/net/foo_tables_*
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / x_tables.c
blob89e322d3b3617480d0413d3db70c031b4a84a81e
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/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>
24 #include <linux/mm.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))
37 struct compat_delta {
38 struct compat_delta *next;
39 unsigned int offset;
40 short delta;
43 struct xt_af {
44 struct mutex mutex;
45 struct list_head match;
46 struct list_head target;
47 #ifdef CONFIG_COMPAT
48 struct mutex compat_mutex;
49 struct compat_delta *compat_offsets;
50 #endif
53 static struct xt_af *xt;
55 #ifdef DEBUG_IP_FIREWALL_USER
56 #define duprintf(format, args...) printk(format , ## args)
57 #else
58 #define duprintf(format, args...)
59 #endif
61 static const char *xt_prefix[NPROTO] = {
62 [AF_INET] = "ip",
63 [AF_INET6] = "ip6",
64 [NF_ARP] = "arp",
67 /* Registration hooks for targets. */
68 int
69 xt_register_target(struct xt_target *target)
71 int ret, af = target->family;
73 ret = mutex_lock_interruptible(&xt[af].mutex);
74 if (ret != 0)
75 return ret;
76 list_add(&target->list, &xt[af].target);
77 mutex_unlock(&xt[af].mutex);
78 return ret;
80 EXPORT_SYMBOL(xt_register_target);
82 void
83 xt_unregister_target(struct xt_target *target)
85 int af = target->family;
87 mutex_lock(&xt[af].mutex);
88 list_del(&target->list);
89 mutex_unlock(&xt[af].mutex);
91 EXPORT_SYMBOL(xt_unregister_target);
93 int
94 xt_register_targets(struct xt_target *target, unsigned int n)
96 unsigned int i;
97 int err = 0;
99 for (i = 0; i < n; i++) {
100 err = xt_register_target(&target[i]);
101 if (err)
102 goto err;
104 return err;
106 err:
107 if (i > 0)
108 xt_unregister_targets(target, i);
109 return err;
111 EXPORT_SYMBOL(xt_register_targets);
113 void
114 xt_unregister_targets(struct xt_target *target, unsigned int n)
116 unsigned int i;
118 for (i = 0; i < n; i++)
119 xt_unregister_target(&target[i]);
121 EXPORT_SYMBOL(xt_unregister_targets);
124 xt_register_match(struct xt_match *match)
126 int ret, af = match->family;
128 ret = mutex_lock_interruptible(&xt[af].mutex);
129 if (ret != 0)
130 return ret;
132 list_add(&match->list, &xt[af].match);
133 mutex_unlock(&xt[af].mutex);
135 return ret;
137 EXPORT_SYMBOL(xt_register_match);
139 void
140 xt_unregister_match(struct xt_match *match)
142 int af = match->family;
144 mutex_lock(&xt[af].mutex);
145 list_del(&match->list);
146 mutex_unlock(&xt[af].mutex);
148 EXPORT_SYMBOL(xt_unregister_match);
151 xt_register_matches(struct xt_match *match, unsigned int n)
153 unsigned int i;
154 int err = 0;
156 for (i = 0; i < n; i++) {
157 err = xt_register_match(&match[i]);
158 if (err)
159 goto err;
161 return err;
163 err:
164 if (i > 0)
165 xt_unregister_matches(match, i);
166 return err;
168 EXPORT_SYMBOL(xt_register_matches);
170 void
171 xt_unregister_matches(struct xt_match *match, unsigned int n)
173 unsigned int i;
175 for (i = 0; i < n; i++)
176 xt_unregister_match(&match[i]);
178 EXPORT_SYMBOL(xt_unregister_matches);
182 * These are weird, but module loading must not be done with mutex
183 * held (since they will register), and we have to have a single
184 * function to use try_then_request_module().
187 /* Find match, grabs ref. Returns ERR_PTR() on error. */
188 struct xt_match *xt_find_match(int af, const char *name, u8 revision)
190 struct xt_match *m;
191 int err = 0;
193 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
194 return ERR_PTR(-EINTR);
196 list_for_each_entry(m, &xt[af].match, list) {
197 if (strcmp(m->name, name) == 0) {
198 if (m->revision == revision) {
199 if (try_module_get(m->me)) {
200 mutex_unlock(&xt[af].mutex);
201 return m;
203 } else
204 err = -EPROTOTYPE; /* Found something. */
207 mutex_unlock(&xt[af].mutex);
208 return ERR_PTR(err);
210 EXPORT_SYMBOL(xt_find_match);
212 /* Find target, grabs ref. Returns ERR_PTR() on error. */
213 struct xt_target *xt_find_target(int af, const char *name, u8 revision)
215 struct xt_target *t;
216 int err = 0;
218 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
219 return ERR_PTR(-EINTR);
221 list_for_each_entry(t, &xt[af].target, list) {
222 if (strcmp(t->name, name) == 0) {
223 if (t->revision == revision) {
224 if (try_module_get(t->me)) {
225 mutex_unlock(&xt[af].mutex);
226 return t;
228 } else
229 err = -EPROTOTYPE; /* Found something. */
232 mutex_unlock(&xt[af].mutex);
233 return ERR_PTR(err);
235 EXPORT_SYMBOL(xt_find_target);
237 struct xt_target *xt_request_find_target(int af, const char *name, u8 revision)
239 struct xt_target *target;
241 target = try_then_request_module(xt_find_target(af, name, revision),
242 "%st_%s", xt_prefix[af], name);
243 if (IS_ERR(target) || !target)
244 return NULL;
245 return target;
247 EXPORT_SYMBOL_GPL(xt_request_find_target);
249 static int match_revfn(int af, const char *name, u8 revision, int *bestp)
251 struct xt_match *m;
252 int have_rev = 0;
254 list_for_each_entry(m, &xt[af].match, list) {
255 if (strcmp(m->name, name) == 0) {
256 if (m->revision > *bestp)
257 *bestp = m->revision;
258 if (m->revision == revision)
259 have_rev = 1;
262 return have_rev;
265 static int target_revfn(int af, const char *name, u8 revision, int *bestp)
267 struct xt_target *t;
268 int have_rev = 0;
270 list_for_each_entry(t, &xt[af].target, list) {
271 if (strcmp(t->name, name) == 0) {
272 if (t->revision > *bestp)
273 *bestp = t->revision;
274 if (t->revision == revision)
275 have_rev = 1;
278 return have_rev;
281 /* Returns true or false (if no such extension at all) */
282 int xt_find_revision(int af, const char *name, u8 revision, int target,
283 int *err)
285 int have_rev, best = -1;
287 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
288 *err = -EINTR;
289 return 1;
291 if (target == 1)
292 have_rev = target_revfn(af, name, revision, &best);
293 else
294 have_rev = match_revfn(af, name, revision, &best);
295 mutex_unlock(&xt[af].mutex);
297 /* Nothing at all? Return 0 to try loading module. */
298 if (best == -1) {
299 *err = -ENOENT;
300 return 0;
303 *err = best;
304 if (!have_rev)
305 *err = -EPROTONOSUPPORT;
306 return 1;
308 EXPORT_SYMBOL_GPL(xt_find_revision);
310 int xt_check_match(const struct xt_match *match, unsigned short family,
311 unsigned int size, const char *table, unsigned int hook_mask,
312 unsigned short proto, int inv_proto)
314 if (XT_ALIGN(match->matchsize) != size) {
315 printk("%s_tables: %s match: invalid size %Zu != %u\n",
316 xt_prefix[family], match->name,
317 XT_ALIGN(match->matchsize), size);
318 return -EINVAL;
320 if (match->table && strcmp(match->table, table)) {
321 printk("%s_tables: %s match: only valid in %s table, not %s\n",
322 xt_prefix[family], match->name, match->table, table);
323 return -EINVAL;
325 if (match->hooks && (hook_mask & ~match->hooks) != 0) {
326 printk("%s_tables: %s match: bad hook_mask %u/%u\n",
327 xt_prefix[family], match->name, hook_mask, match->hooks);
328 return -EINVAL;
330 if (match->proto && (match->proto != proto || inv_proto)) {
331 printk("%s_tables: %s match: only valid for protocol %u\n",
332 xt_prefix[family], match->name, match->proto);
333 return -EINVAL;
335 return 0;
337 EXPORT_SYMBOL_GPL(xt_check_match);
339 #ifdef CONFIG_COMPAT
340 int xt_compat_add_offset(int af, unsigned int offset, short delta)
342 struct compat_delta *tmp;
344 tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
345 if (!tmp)
346 return -ENOMEM;
348 tmp->offset = offset;
349 tmp->delta = delta;
351 if (xt[af].compat_offsets) {
352 tmp->next = xt[af].compat_offsets->next;
353 xt[af].compat_offsets->next = tmp;
354 } else {
355 xt[af].compat_offsets = tmp;
356 tmp->next = NULL;
358 return 0;
360 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
362 void xt_compat_flush_offsets(int af)
364 struct compat_delta *tmp, *next;
366 if (xt[af].compat_offsets) {
367 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
368 next = tmp->next;
369 kfree(tmp);
371 xt[af].compat_offsets = NULL;
374 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
376 short xt_compat_calc_jump(int af, unsigned int offset)
378 struct compat_delta *tmp;
379 short delta;
381 for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
382 if (tmp->offset < offset)
383 delta += tmp->delta;
384 return delta;
386 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
388 int xt_compat_match_offset(struct xt_match *match)
390 u_int16_t csize = match->compatsize ? : match->matchsize;
391 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
393 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
395 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
396 unsigned int *size)
398 struct xt_match *match = m->u.kernel.match;
399 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
400 int pad, off = xt_compat_match_offset(match);
401 u_int16_t msize = cm->u.user.match_size;
403 m = *dstptr;
404 memcpy(m, cm, sizeof(*cm));
405 if (match->compat_from_user)
406 match->compat_from_user(m->data, cm->data);
407 else
408 memcpy(m->data, cm->data, msize - sizeof(*cm));
409 pad = XT_ALIGN(match->matchsize) - match->matchsize;
410 if (pad > 0)
411 memset(m->data + match->matchsize, 0, pad);
413 msize += off;
414 m->u.user.match_size = msize;
416 *size += off;
417 *dstptr += msize;
418 return 0;
420 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
422 int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr,
423 unsigned int *size)
425 struct xt_match *match = m->u.kernel.match;
426 struct compat_xt_entry_match __user *cm = *dstptr;
427 int off = xt_compat_match_offset(match);
428 u_int16_t msize = m->u.user.match_size - off;
430 if (copy_to_user(cm, m, sizeof(*cm)) ||
431 put_user(msize, &cm->u.user.match_size) ||
432 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
433 strlen(m->u.kernel.match->name) + 1))
434 return -EFAULT;
436 if (match->compat_to_user) {
437 if (match->compat_to_user((void __user *)cm->data, m->data))
438 return -EFAULT;
439 } else {
440 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
441 return -EFAULT;
444 *size -= off;
445 *dstptr += msize;
446 return 0;
448 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
449 #endif /* CONFIG_COMPAT */
451 int xt_check_target(const struct xt_target *target, unsigned short family,
452 unsigned int size, const char *table, unsigned int hook_mask,
453 unsigned short proto, int inv_proto)
455 if (XT_ALIGN(target->targetsize) != size) {
456 printk("%s_tables: %s target: invalid size %Zu != %u\n",
457 xt_prefix[family], target->name,
458 XT_ALIGN(target->targetsize), size);
459 return -EINVAL;
461 if (target->table && strcmp(target->table, table)) {
462 printk("%s_tables: %s target: only valid in %s table, not %s\n",
463 xt_prefix[family], target->name, target->table, table);
464 return -EINVAL;
466 if (target->hooks && (hook_mask & ~target->hooks) != 0) {
467 printk("%s_tables: %s target: bad hook_mask %u/%u\n",
468 xt_prefix[family], target->name, hook_mask,
469 target->hooks);
470 return -EINVAL;
472 if (target->proto && (target->proto != proto || inv_proto)) {
473 printk("%s_tables: %s target: only valid for protocol %u\n",
474 xt_prefix[family], target->name, target->proto);
475 return -EINVAL;
477 return 0;
479 EXPORT_SYMBOL_GPL(xt_check_target);
481 #ifdef CONFIG_COMPAT
482 int xt_compat_target_offset(struct xt_target *target)
484 u_int16_t csize = target->compatsize ? : target->targetsize;
485 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
487 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
489 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
490 unsigned int *size)
492 struct xt_target *target = t->u.kernel.target;
493 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
494 int pad, off = xt_compat_target_offset(target);
495 u_int16_t tsize = ct->u.user.target_size;
497 t = *dstptr;
498 memcpy(t, ct, sizeof(*ct));
499 if (target->compat_from_user)
500 target->compat_from_user(t->data, ct->data);
501 else
502 memcpy(t->data, ct->data, tsize - sizeof(*ct));
503 pad = XT_ALIGN(target->targetsize) - target->targetsize;
504 if (pad > 0)
505 memset(t->data + target->targetsize, 0, pad);
507 tsize += off;
508 t->u.user.target_size = tsize;
510 *size += off;
511 *dstptr += tsize;
513 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
515 int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr,
516 unsigned int *size)
518 struct xt_target *target = t->u.kernel.target;
519 struct compat_xt_entry_target __user *ct = *dstptr;
520 int off = xt_compat_target_offset(target);
521 u_int16_t tsize = t->u.user.target_size - off;
523 if (copy_to_user(ct, t, sizeof(*ct)) ||
524 put_user(tsize, &ct->u.user.target_size) ||
525 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
526 strlen(t->u.kernel.target->name) + 1))
527 return -EFAULT;
529 if (target->compat_to_user) {
530 if (target->compat_to_user((void __user *)ct->data, t->data))
531 return -EFAULT;
532 } else {
533 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
534 return -EFAULT;
537 *size -= off;
538 *dstptr += tsize;
539 return 0;
541 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
542 #endif
544 struct xt_table_info *xt_alloc_table_info(unsigned int size)
546 struct xt_table_info *newinfo;
547 int cpu;
549 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
550 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
551 return NULL;
553 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
554 if (!newinfo)
555 return NULL;
557 newinfo->size = size;
559 for_each_possible_cpu(cpu) {
560 if (size <= PAGE_SIZE)
561 newinfo->entries[cpu] = kmalloc_node(size,
562 GFP_KERNEL,
563 cpu_to_node(cpu));
564 else
565 newinfo->entries[cpu] = vmalloc_node(size,
566 cpu_to_node(cpu));
568 if (newinfo->entries[cpu] == NULL) {
569 xt_free_table_info(newinfo);
570 return NULL;
574 return newinfo;
576 EXPORT_SYMBOL(xt_alloc_table_info);
578 void xt_free_table_info(struct xt_table_info *info)
580 int cpu;
582 for_each_possible_cpu(cpu) {
583 if (info->size <= PAGE_SIZE)
584 kfree(info->entries[cpu]);
585 else
586 vfree(info->entries[cpu]);
588 kfree(info);
590 EXPORT_SYMBOL(xt_free_table_info);
592 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
593 struct xt_table *xt_find_table_lock(struct net *net, int af, const char *name)
595 struct xt_table *t;
597 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
598 return ERR_PTR(-EINTR);
600 list_for_each_entry(t, &net->xt.tables[af], list)
601 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
602 return t;
603 mutex_unlock(&xt[af].mutex);
604 return NULL;
606 EXPORT_SYMBOL_GPL(xt_find_table_lock);
608 void xt_table_unlock(struct xt_table *table)
610 mutex_unlock(&xt[table->af].mutex);
612 EXPORT_SYMBOL_GPL(xt_table_unlock);
614 #ifdef CONFIG_COMPAT
615 void xt_compat_lock(int af)
617 mutex_lock(&xt[af].compat_mutex);
619 EXPORT_SYMBOL_GPL(xt_compat_lock);
621 void xt_compat_unlock(int af)
623 mutex_unlock(&xt[af].compat_mutex);
625 EXPORT_SYMBOL_GPL(xt_compat_unlock);
626 #endif
628 struct xt_table_info *
629 xt_replace_table(struct xt_table *table,
630 unsigned int num_counters,
631 struct xt_table_info *newinfo,
632 int *error)
634 struct xt_table_info *oldinfo, *private;
636 /* Do the substitution. */
637 write_lock_bh(&table->lock);
638 private = table->private;
639 /* Check inside lock: is the old number correct? */
640 if (num_counters != private->number) {
641 duprintf("num_counters != table->private->number (%u/%u)\n",
642 num_counters, private->number);
643 write_unlock_bh(&table->lock);
644 *error = -EAGAIN;
645 return NULL;
647 oldinfo = private;
648 table->private = newinfo;
649 newinfo->initial_entries = oldinfo->initial_entries;
650 write_unlock_bh(&table->lock);
652 return oldinfo;
654 EXPORT_SYMBOL_GPL(xt_replace_table);
656 struct xt_table *xt_register_table(struct net *net, struct xt_table *table,
657 struct xt_table_info *bootstrap,
658 struct xt_table_info *newinfo)
660 int ret;
661 struct xt_table_info *private;
662 struct xt_table *t;
664 /* Don't add one object to multiple lists. */
665 table = kmemdup(table, sizeof(struct xt_table), GFP_KERNEL);
666 if (!table) {
667 ret = -ENOMEM;
668 goto out;
671 ret = mutex_lock_interruptible(&xt[table->af].mutex);
672 if (ret != 0)
673 goto out_free;
675 /* Don't autoload: we'd eat our tail... */
676 list_for_each_entry(t, &net->xt.tables[table->af], list) {
677 if (strcmp(t->name, table->name) == 0) {
678 ret = -EEXIST;
679 goto unlock;
683 /* Simplifies replace_table code. */
684 table->private = bootstrap;
685 rwlock_init(&table->lock);
686 if (!xt_replace_table(table, 0, newinfo, &ret))
687 goto unlock;
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, &net->xt.tables[table->af]);
696 mutex_unlock(&xt[table->af].mutex);
697 return table;
699 unlock:
700 mutex_unlock(&xt[table->af].mutex);
701 out_free:
702 kfree(table);
703 out:
704 return ERR_PTR(ret);
706 EXPORT_SYMBOL_GPL(xt_register_table);
708 void *xt_unregister_table(struct xt_table *table)
710 struct xt_table_info *private;
712 mutex_lock(&xt[table->af].mutex);
713 private = table->private;
714 list_del(&table->list);
715 mutex_unlock(&xt[table->af].mutex);
716 kfree(table);
718 return private;
720 EXPORT_SYMBOL_GPL(xt_unregister_table);
722 #ifdef CONFIG_PROC_FS
723 static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
725 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
726 u_int16_t af = (unsigned long)pde->data;
728 mutex_lock(&xt[af].mutex);
729 return seq_list_start(&init_net.xt.tables[af], *pos);
732 static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
734 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
735 u_int16_t af = (unsigned long)pde->data;
737 return seq_list_next(v, &init_net.xt.tables[af], pos);
740 static void xt_table_seq_stop(struct seq_file *seq, void *v)
742 struct proc_dir_entry *pde = seq->private;
743 u_int16_t af = (unsigned long)pde->data;
745 mutex_unlock(&xt[af].mutex);
748 static int xt_table_seq_show(struct seq_file *seq, void *v)
750 struct xt_table *table = list_entry(v, struct xt_table, list);
752 if (strlen(table->name))
753 return seq_printf(seq, "%s\n", table->name);
754 else
755 return 0;
758 static const struct seq_operations xt_table_seq_ops = {
759 .start = xt_table_seq_start,
760 .next = xt_table_seq_next,
761 .stop = xt_table_seq_stop,
762 .show = xt_table_seq_show,
765 static int xt_table_open(struct inode *inode, struct file *file)
767 int ret;
769 ret = seq_open(file, &xt_table_seq_ops);
770 if (!ret) {
771 struct seq_file *seq = file->private_data;
773 seq->private = PDE(inode);
775 return ret;
778 static const struct file_operations xt_table_ops = {
779 .owner = THIS_MODULE,
780 .open = xt_table_open,
781 .read = seq_read,
782 .llseek = seq_lseek,
783 .release = seq_release,
786 static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
788 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
789 u_int16_t af = (unsigned long)pde->data;
791 mutex_lock(&xt[af].mutex);
792 return seq_list_start(&xt[af].match, *pos);
795 static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *pos)
797 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
798 u_int16_t af = (unsigned long)pde->data;
800 return seq_list_next(v, &xt[af].match, pos);
803 static void xt_match_seq_stop(struct seq_file *seq, void *v)
805 struct proc_dir_entry *pde = seq->private;
806 u_int16_t af = (unsigned long)pde->data;
808 mutex_unlock(&xt[af].mutex);
811 static int xt_match_seq_show(struct seq_file *seq, void *v)
813 struct xt_match *match = list_entry(v, struct xt_match, list);
815 if (strlen(match->name))
816 return seq_printf(seq, "%s\n", match->name);
817 else
818 return 0;
821 static const struct seq_operations xt_match_seq_ops = {
822 .start = xt_match_seq_start,
823 .next = xt_match_seq_next,
824 .stop = xt_match_seq_stop,
825 .show = xt_match_seq_show,
828 static int xt_match_open(struct inode *inode, struct file *file)
830 int ret;
832 ret = seq_open(file, &xt_match_seq_ops);
833 if (!ret) {
834 struct seq_file *seq = file->private_data;
836 seq->private = PDE(inode);
838 return ret;
841 static const struct file_operations xt_match_ops = {
842 .owner = THIS_MODULE,
843 .open = xt_match_open,
844 .read = seq_read,
845 .llseek = seq_lseek,
846 .release = seq_release,
849 static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
851 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
852 u_int16_t af = (unsigned long)pde->data;
854 mutex_lock(&xt[af].mutex);
855 return seq_list_start(&xt[af].target, *pos);
858 static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *pos)
860 struct proc_dir_entry *pde = (struct proc_dir_entry *)seq->private;
861 u_int16_t af = (unsigned long)pde->data;
863 return seq_list_next(v, &xt[af].target, pos);
866 static void xt_target_seq_stop(struct seq_file *seq, void *v)
868 struct proc_dir_entry *pde = seq->private;
869 u_int16_t af = (unsigned long)pde->data;
871 mutex_unlock(&xt[af].mutex);
874 static int xt_target_seq_show(struct seq_file *seq, void *v)
876 struct xt_target *target = list_entry(v, struct xt_target, list);
878 if (strlen(target->name))
879 return seq_printf(seq, "%s\n", target->name);
880 else
881 return 0;
884 static const struct seq_operations xt_target_seq_ops = {
885 .start = xt_target_seq_start,
886 .next = xt_target_seq_next,
887 .stop = xt_target_seq_stop,
888 .show = xt_target_seq_show,
891 static int xt_target_open(struct inode *inode, struct file *file)
893 int ret;
895 ret = seq_open(file, &xt_target_seq_ops);
896 if (!ret) {
897 struct seq_file *seq = file->private_data;
899 seq->private = PDE(inode);
901 return ret;
904 static const struct file_operations xt_target_ops = {
905 .owner = THIS_MODULE,
906 .open = xt_target_open,
907 .read = seq_read,
908 .llseek = seq_lseek,
909 .release = seq_release,
912 #define FORMAT_TABLES "_tables_names"
913 #define FORMAT_MATCHES "_tables_matches"
914 #define FORMAT_TARGETS "_tables_targets"
916 #endif /* CONFIG_PROC_FS */
918 int xt_proto_init(int af)
920 #ifdef CONFIG_PROC_FS
921 char buf[XT_FUNCTION_MAXNAMELEN];
922 struct proc_dir_entry *proc;
923 #endif
925 if (af >= NPROTO)
926 return -EINVAL;
929 #ifdef CONFIG_PROC_FS
930 strlcpy(buf, xt_prefix[af], sizeof(buf));
931 strlcat(buf, FORMAT_TABLES, sizeof(buf));
932 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_table_ops);
933 if (!proc)
934 goto out;
935 proc->data = (void *)(unsigned long)af;
938 strlcpy(buf, xt_prefix[af], sizeof(buf));
939 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
940 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_match_ops);
941 if (!proc)
942 goto out_remove_tables;
943 proc->data = (void *)(unsigned long)af;
945 strlcpy(buf, xt_prefix[af], sizeof(buf));
946 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
947 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_target_ops);
948 if (!proc)
949 goto out_remove_matches;
950 proc->data = (void *)(unsigned long)af;
951 #endif
953 return 0;
955 #ifdef CONFIG_PROC_FS
956 out_remove_matches:
957 strlcpy(buf, xt_prefix[af], sizeof(buf));
958 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
959 proc_net_remove(&init_net, buf);
961 out_remove_tables:
962 strlcpy(buf, xt_prefix[af], sizeof(buf));
963 strlcat(buf, FORMAT_TABLES, sizeof(buf));
964 proc_net_remove(&init_net, buf);
965 out:
966 return -1;
967 #endif
969 EXPORT_SYMBOL_GPL(xt_proto_init);
971 void xt_proto_fini(int af)
973 #ifdef CONFIG_PROC_FS
974 char buf[XT_FUNCTION_MAXNAMELEN];
976 strlcpy(buf, xt_prefix[af], sizeof(buf));
977 strlcat(buf, FORMAT_TABLES, sizeof(buf));
978 proc_net_remove(&init_net, buf);
980 strlcpy(buf, xt_prefix[af], sizeof(buf));
981 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
982 proc_net_remove(&init_net, buf);
984 strlcpy(buf, xt_prefix[af], sizeof(buf));
985 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
986 proc_net_remove(&init_net, buf);
987 #endif /*CONFIG_PROC_FS*/
989 EXPORT_SYMBOL_GPL(xt_proto_fini);
991 static int __net_init xt_net_init(struct net *net)
993 int i;
995 for (i = 0; i < NPROTO; i++)
996 INIT_LIST_HEAD(&net->xt.tables[i]);
997 return 0;
1000 static struct pernet_operations xt_net_ops = {
1001 .init = xt_net_init,
1004 static int __init xt_init(void)
1006 int i, rv;
1008 xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
1009 if (!xt)
1010 return -ENOMEM;
1012 for (i = 0; i < NPROTO; i++) {
1013 mutex_init(&xt[i].mutex);
1014 #ifdef CONFIG_COMPAT
1015 mutex_init(&xt[i].compat_mutex);
1016 xt[i].compat_offsets = NULL;
1017 #endif
1018 INIT_LIST_HEAD(&xt[i].target);
1019 INIT_LIST_HEAD(&xt[i].match);
1021 rv = register_pernet_subsys(&xt_net_ops);
1022 if (rv < 0)
1023 kfree(xt);
1024 return rv;
1027 static void __exit xt_fini(void)
1029 unregister_pernet_subsys(&xt_net_ops);
1030 kfree(xt);
1033 module_init(xt_init);
1034 module_exit(xt_fini);