[ETHER]: Bring back MAC_FMT
[linux-2.6/libata-dev.git] / net / netfilter / x_tables.c
blob8d4fca96a4a719a054ee3ac0cb02643dc90113ba
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 struct list_head tables;
48 #ifdef CONFIG_COMPAT
49 struct mutex compat_mutex;
50 struct compat_delta *compat_offsets;
51 #endif
54 static struct xt_af *xt;
56 #ifdef DEBUG_IP_FIREWALL_USER
57 #define duprintf(format, args...) printk(format , ## args)
58 #else
59 #define duprintf(format, args...)
60 #endif
62 enum {
63 TABLE,
64 TARGET,
65 MATCH,
68 static const char *xt_prefix[NPROTO] = {
69 [AF_INET] = "ip",
70 [AF_INET6] = "ip6",
71 [NF_ARP] = "arp",
74 /* Registration hooks for targets. */
75 int
76 xt_register_target(struct xt_target *target)
78 int ret, af = target->family;
80 ret = mutex_lock_interruptible(&xt[af].mutex);
81 if (ret != 0)
82 return ret;
83 list_add(&target->list, &xt[af].target);
84 mutex_unlock(&xt[af].mutex);
85 return ret;
87 EXPORT_SYMBOL(xt_register_target);
89 void
90 xt_unregister_target(struct xt_target *target)
92 int af = target->family;
94 mutex_lock(&xt[af].mutex);
95 list_del(&target->list);
96 mutex_unlock(&xt[af].mutex);
98 EXPORT_SYMBOL(xt_unregister_target);
101 xt_register_targets(struct xt_target *target, unsigned int n)
103 unsigned int i;
104 int err = 0;
106 for (i = 0; i < n; i++) {
107 err = xt_register_target(&target[i]);
108 if (err)
109 goto err;
111 return err;
113 err:
114 if (i > 0)
115 xt_unregister_targets(target, i);
116 return err;
118 EXPORT_SYMBOL(xt_register_targets);
120 void
121 xt_unregister_targets(struct xt_target *target, unsigned int n)
123 unsigned int i;
125 for (i = 0; i < n; i++)
126 xt_unregister_target(&target[i]);
128 EXPORT_SYMBOL(xt_unregister_targets);
131 xt_register_match(struct xt_match *match)
133 int ret, af = match->family;
135 ret = mutex_lock_interruptible(&xt[af].mutex);
136 if (ret != 0)
137 return ret;
139 list_add(&match->list, &xt[af].match);
140 mutex_unlock(&xt[af].mutex);
142 return ret;
144 EXPORT_SYMBOL(xt_register_match);
146 void
147 xt_unregister_match(struct xt_match *match)
149 int af = match->family;
151 mutex_lock(&xt[af].mutex);
152 list_del(&match->list);
153 mutex_unlock(&xt[af].mutex);
155 EXPORT_SYMBOL(xt_unregister_match);
158 xt_register_matches(struct xt_match *match, unsigned int n)
160 unsigned int i;
161 int err = 0;
163 for (i = 0; i < n; i++) {
164 err = xt_register_match(&match[i]);
165 if (err)
166 goto err;
168 return err;
170 err:
171 if (i > 0)
172 xt_unregister_matches(match, i);
173 return err;
175 EXPORT_SYMBOL(xt_register_matches);
177 void
178 xt_unregister_matches(struct xt_match *match, unsigned int n)
180 unsigned int i;
182 for (i = 0; i < n; i++)
183 xt_unregister_match(&match[i]);
185 EXPORT_SYMBOL(xt_unregister_matches);
189 * These are weird, but module loading must not be done with mutex
190 * held (since they will register), and we have to have a single
191 * function to use try_then_request_module().
194 /* Find match, grabs ref. Returns ERR_PTR() on error. */
195 struct xt_match *xt_find_match(int af, const char *name, u8 revision)
197 struct xt_match *m;
198 int err = 0;
200 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
201 return ERR_PTR(-EINTR);
203 list_for_each_entry(m, &xt[af].match, list) {
204 if (strcmp(m->name, name) == 0) {
205 if (m->revision == revision) {
206 if (try_module_get(m->me)) {
207 mutex_unlock(&xt[af].mutex);
208 return m;
210 } else
211 err = -EPROTOTYPE; /* Found something. */
214 mutex_unlock(&xt[af].mutex);
215 return ERR_PTR(err);
217 EXPORT_SYMBOL(xt_find_match);
219 /* Find target, grabs ref. Returns ERR_PTR() on error. */
220 struct xt_target *xt_find_target(int af, const char *name, u8 revision)
222 struct xt_target *t;
223 int err = 0;
225 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
226 return ERR_PTR(-EINTR);
228 list_for_each_entry(t, &xt[af].target, list) {
229 if (strcmp(t->name, name) == 0) {
230 if (t->revision == revision) {
231 if (try_module_get(t->me)) {
232 mutex_unlock(&xt[af].mutex);
233 return t;
235 } else
236 err = -EPROTOTYPE; /* Found something. */
239 mutex_unlock(&xt[af].mutex);
240 return ERR_PTR(err);
242 EXPORT_SYMBOL(xt_find_target);
244 struct xt_target *xt_request_find_target(int af, const char *name, u8 revision)
246 struct xt_target *target;
248 target = try_then_request_module(xt_find_target(af, name, revision),
249 "%st_%s", xt_prefix[af], name);
250 if (IS_ERR(target) || !target)
251 return NULL;
252 return target;
254 EXPORT_SYMBOL_GPL(xt_request_find_target);
256 static int match_revfn(int af, const char *name, u8 revision, int *bestp)
258 struct xt_match *m;
259 int have_rev = 0;
261 list_for_each_entry(m, &xt[af].match, list) {
262 if (strcmp(m->name, name) == 0) {
263 if (m->revision > *bestp)
264 *bestp = m->revision;
265 if (m->revision == revision)
266 have_rev = 1;
269 return have_rev;
272 static int target_revfn(int af, const char *name, u8 revision, int *bestp)
274 struct xt_target *t;
275 int have_rev = 0;
277 list_for_each_entry(t, &xt[af].target, list) {
278 if (strcmp(t->name, name) == 0) {
279 if (t->revision > *bestp)
280 *bestp = t->revision;
281 if (t->revision == revision)
282 have_rev = 1;
285 return have_rev;
288 /* Returns true or false (if no such extension at all) */
289 int xt_find_revision(int af, const char *name, u8 revision, int target,
290 int *err)
292 int have_rev, best = -1;
294 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
295 *err = -EINTR;
296 return 1;
298 if (target == 1)
299 have_rev = target_revfn(af, name, revision, &best);
300 else
301 have_rev = match_revfn(af, name, revision, &best);
302 mutex_unlock(&xt[af].mutex);
304 /* Nothing at all? Return 0 to try loading module. */
305 if (best == -1) {
306 *err = -ENOENT;
307 return 0;
310 *err = best;
311 if (!have_rev)
312 *err = -EPROTONOSUPPORT;
313 return 1;
315 EXPORT_SYMBOL_GPL(xt_find_revision);
317 int xt_check_match(const struct xt_match *match, unsigned short family,
318 unsigned int size, const char *table, unsigned int hook_mask,
319 unsigned short proto, int inv_proto)
321 if (XT_ALIGN(match->matchsize) != size) {
322 printk("%s_tables: %s match: invalid size %Zu != %u\n",
323 xt_prefix[family], match->name,
324 XT_ALIGN(match->matchsize), size);
325 return -EINVAL;
327 if (match->table && strcmp(match->table, table)) {
328 printk("%s_tables: %s match: only valid in %s table, not %s\n",
329 xt_prefix[family], match->name, match->table, table);
330 return -EINVAL;
332 if (match->hooks && (hook_mask & ~match->hooks) != 0) {
333 printk("%s_tables: %s match: bad hook_mask %u/%u\n",
334 xt_prefix[family], match->name, hook_mask, match->hooks);
335 return -EINVAL;
337 if (match->proto && (match->proto != proto || inv_proto)) {
338 printk("%s_tables: %s match: only valid for protocol %u\n",
339 xt_prefix[family], match->name, match->proto);
340 return -EINVAL;
342 return 0;
344 EXPORT_SYMBOL_GPL(xt_check_match);
346 #ifdef CONFIG_COMPAT
347 int xt_compat_add_offset(int af, unsigned int offset, short delta)
349 struct compat_delta *tmp;
351 tmp = kmalloc(sizeof(struct compat_delta), GFP_KERNEL);
352 if (!tmp)
353 return -ENOMEM;
355 tmp->offset = offset;
356 tmp->delta = delta;
358 if (xt[af].compat_offsets) {
359 tmp->next = xt[af].compat_offsets->next;
360 xt[af].compat_offsets->next = tmp;
361 } else {
362 xt[af].compat_offsets = tmp;
363 tmp->next = NULL;
365 return 0;
367 EXPORT_SYMBOL_GPL(xt_compat_add_offset);
369 void xt_compat_flush_offsets(int af)
371 struct compat_delta *tmp, *next;
373 if (xt[af].compat_offsets) {
374 for (tmp = xt[af].compat_offsets; tmp; tmp = next) {
375 next = tmp->next;
376 kfree(tmp);
378 xt[af].compat_offsets = NULL;
381 EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
383 short xt_compat_calc_jump(int af, unsigned int offset)
385 struct compat_delta *tmp;
386 short delta;
388 for (tmp = xt[af].compat_offsets, delta = 0; tmp; tmp = tmp->next)
389 if (tmp->offset < offset)
390 delta += tmp->delta;
391 return delta;
393 EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
395 int xt_compat_match_offset(struct xt_match *match)
397 u_int16_t csize = match->compatsize ? : match->matchsize;
398 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
400 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
402 int xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
403 int *size)
405 struct xt_match *match = m->u.kernel.match;
406 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
407 int pad, off = xt_compat_match_offset(match);
408 u_int16_t msize = cm->u.user.match_size;
410 m = *dstptr;
411 memcpy(m, cm, sizeof(*cm));
412 if (match->compat_from_user)
413 match->compat_from_user(m->data, cm->data);
414 else
415 memcpy(m->data, cm->data, msize - sizeof(*cm));
416 pad = XT_ALIGN(match->matchsize) - match->matchsize;
417 if (pad > 0)
418 memset(m->data + match->matchsize, 0, pad);
420 msize += off;
421 m->u.user.match_size = msize;
423 *size += off;
424 *dstptr += msize;
425 return 0;
427 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
429 int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr,
430 int *size)
432 struct xt_match *match = m->u.kernel.match;
433 struct compat_xt_entry_match __user *cm = *dstptr;
434 int off = xt_compat_match_offset(match);
435 u_int16_t msize = m->u.user.match_size - off;
437 if (copy_to_user(cm, m, sizeof(*cm)) ||
438 put_user(msize, &cm->u.user.match_size) ||
439 copy_to_user(cm->u.user.name, m->u.kernel.match->name,
440 strlen(m->u.kernel.match->name) + 1))
441 return -EFAULT;
443 if (match->compat_to_user) {
444 if (match->compat_to_user((void __user *)cm->data, m->data))
445 return -EFAULT;
446 } else {
447 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
448 return -EFAULT;
451 *size -= off;
452 *dstptr += msize;
453 return 0;
455 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
456 #endif /* CONFIG_COMPAT */
458 int xt_check_target(const struct xt_target *target, unsigned short family,
459 unsigned int size, const char *table, unsigned int hook_mask,
460 unsigned short proto, int inv_proto)
462 if (XT_ALIGN(target->targetsize) != size) {
463 printk("%s_tables: %s target: invalid size %Zu != %u\n",
464 xt_prefix[family], target->name,
465 XT_ALIGN(target->targetsize), size);
466 return -EINVAL;
468 if (target->table && strcmp(target->table, table)) {
469 printk("%s_tables: %s target: only valid in %s table, not %s\n",
470 xt_prefix[family], target->name, target->table, table);
471 return -EINVAL;
473 if (target->hooks && (hook_mask & ~target->hooks) != 0) {
474 printk("%s_tables: %s target: bad hook_mask %u/%u\n",
475 xt_prefix[family], target->name, hook_mask,
476 target->hooks);
477 return -EINVAL;
479 if (target->proto && (target->proto != proto || inv_proto)) {
480 printk("%s_tables: %s target: only valid for protocol %u\n",
481 xt_prefix[family], target->name, target->proto);
482 return -EINVAL;
484 return 0;
486 EXPORT_SYMBOL_GPL(xt_check_target);
488 #ifdef CONFIG_COMPAT
489 int xt_compat_target_offset(struct xt_target *target)
491 u_int16_t csize = target->compatsize ? : target->targetsize;
492 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
494 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
496 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
497 int *size)
499 struct xt_target *target = t->u.kernel.target;
500 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
501 int pad, off = xt_compat_target_offset(target);
502 u_int16_t tsize = ct->u.user.target_size;
504 t = *dstptr;
505 memcpy(t, ct, sizeof(*ct));
506 if (target->compat_from_user)
507 target->compat_from_user(t->data, ct->data);
508 else
509 memcpy(t->data, ct->data, tsize - sizeof(*ct));
510 pad = XT_ALIGN(target->targetsize) - target->targetsize;
511 if (pad > 0)
512 memset(t->data + target->targetsize, 0, pad);
514 tsize += off;
515 t->u.user.target_size = tsize;
517 *size += off;
518 *dstptr += tsize;
520 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
522 int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr,
523 int *size)
525 struct xt_target *target = t->u.kernel.target;
526 struct compat_xt_entry_target __user *ct = *dstptr;
527 int off = xt_compat_target_offset(target);
528 u_int16_t tsize = t->u.user.target_size - off;
530 if (copy_to_user(ct, t, sizeof(*ct)) ||
531 put_user(tsize, &ct->u.user.target_size) ||
532 copy_to_user(ct->u.user.name, t->u.kernel.target->name,
533 strlen(t->u.kernel.target->name) + 1))
534 return -EFAULT;
536 if (target->compat_to_user) {
537 if (target->compat_to_user((void __user *)ct->data, t->data))
538 return -EFAULT;
539 } else {
540 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
541 return -EFAULT;
544 *size -= off;
545 *dstptr += tsize;
546 return 0;
548 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
549 #endif
551 struct xt_table_info *xt_alloc_table_info(unsigned int size)
553 struct xt_table_info *newinfo;
554 int cpu;
556 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
557 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
558 return NULL;
560 newinfo = kzalloc(XT_TABLE_INFO_SZ, GFP_KERNEL);
561 if (!newinfo)
562 return NULL;
564 newinfo->size = size;
566 for_each_possible_cpu(cpu) {
567 if (size <= PAGE_SIZE)
568 newinfo->entries[cpu] = kmalloc_node(size,
569 GFP_KERNEL,
570 cpu_to_node(cpu));
571 else
572 newinfo->entries[cpu] = vmalloc_node(size,
573 cpu_to_node(cpu));
575 if (newinfo->entries[cpu] == NULL) {
576 xt_free_table_info(newinfo);
577 return NULL;
581 return newinfo;
583 EXPORT_SYMBOL(xt_alloc_table_info);
585 void xt_free_table_info(struct xt_table_info *info)
587 int cpu;
589 for_each_possible_cpu(cpu) {
590 if (info->size <= PAGE_SIZE)
591 kfree(info->entries[cpu]);
592 else
593 vfree(info->entries[cpu]);
595 kfree(info);
597 EXPORT_SYMBOL(xt_free_table_info);
599 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
600 struct xt_table *xt_find_table_lock(int af, const char *name)
602 struct xt_table *t;
604 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
605 return ERR_PTR(-EINTR);
607 list_for_each_entry(t, &xt[af].tables, list)
608 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
609 return t;
610 mutex_unlock(&xt[af].mutex);
611 return NULL;
613 EXPORT_SYMBOL_GPL(xt_find_table_lock);
615 void xt_table_unlock(struct xt_table *table)
617 mutex_unlock(&xt[table->af].mutex);
619 EXPORT_SYMBOL_GPL(xt_table_unlock);
621 #ifdef CONFIG_COMPAT
622 void xt_compat_lock(int af)
624 mutex_lock(&xt[af].compat_mutex);
626 EXPORT_SYMBOL_GPL(xt_compat_lock);
628 void xt_compat_unlock(int af)
630 mutex_unlock(&xt[af].compat_mutex);
632 EXPORT_SYMBOL_GPL(xt_compat_unlock);
633 #endif
635 struct xt_table_info *
636 xt_replace_table(struct xt_table *table,
637 unsigned int num_counters,
638 struct xt_table_info *newinfo,
639 int *error)
641 struct xt_table_info *oldinfo, *private;
643 /* Do the substitution. */
644 write_lock_bh(&table->lock);
645 private = table->private;
646 /* Check inside lock: is the old number correct? */
647 if (num_counters != private->number) {
648 duprintf("num_counters != table->private->number (%u/%u)\n",
649 num_counters, private->number);
650 write_unlock_bh(&table->lock);
651 *error = -EAGAIN;
652 return NULL;
654 oldinfo = private;
655 table->private = newinfo;
656 newinfo->initial_entries = oldinfo->initial_entries;
657 write_unlock_bh(&table->lock);
659 return oldinfo;
661 EXPORT_SYMBOL_GPL(xt_replace_table);
663 int xt_register_table(struct xt_table *table,
664 struct xt_table_info *bootstrap,
665 struct xt_table_info *newinfo)
667 int ret;
668 struct xt_table_info *private;
669 struct xt_table *t;
671 ret = mutex_lock_interruptible(&xt[table->af].mutex);
672 if (ret != 0)
673 return ret;
675 /* Don't autoload: we'd eat our tail... */
676 list_for_each_entry(t, &xt[table->af].tables, 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, &xt[table->af].tables);
697 ret = 0;
698 unlock:
699 mutex_unlock(&xt[table->af].mutex);
700 return ret;
702 EXPORT_SYMBOL_GPL(xt_register_table);
704 void *xt_unregister_table(struct xt_table *table)
706 struct xt_table_info *private;
708 mutex_lock(&xt[table->af].mutex);
709 private = table->private;
710 list_del(&table->list);
711 mutex_unlock(&xt[table->af].mutex);
713 return private;
715 EXPORT_SYMBOL_GPL(xt_unregister_table);
717 #ifdef CONFIG_PROC_FS
718 static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos)
720 struct list_head *head = list->next;
722 if (!head || list_empty(list))
723 return NULL;
725 while (pos && (head = head->next)) {
726 if (head == list)
727 return NULL;
728 pos--;
730 return pos ? NULL : head;
733 static struct list_head *type2list(u_int16_t af, u_int16_t type)
735 struct list_head *list;
737 switch (type) {
738 case TARGET:
739 list = &xt[af].target;
740 break;
741 case MATCH:
742 list = &xt[af].match;
743 break;
744 case TABLE:
745 list = &xt[af].tables;
746 break;
747 default:
748 list = NULL;
749 break;
752 return list;
755 static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos)
757 struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private;
758 u_int16_t af = (unsigned long)pde->data & 0xffff;
759 u_int16_t type = (unsigned long)pde->data >> 16;
760 struct list_head *list;
762 if (af >= NPROTO)
763 return NULL;
765 list = type2list(af, type);
766 if (!list)
767 return NULL;
769 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
770 return NULL;
772 return xt_get_idx(list, seq, *pos);
775 static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
777 struct proc_dir_entry *pde = seq->private;
778 u_int16_t af = (unsigned long)pde->data & 0xffff;
779 u_int16_t type = (unsigned long)pde->data >> 16;
780 struct list_head *list;
782 if (af >= NPROTO)
783 return NULL;
785 list = type2list(af, type);
786 if (!list)
787 return NULL;
789 (*pos)++;
790 return xt_get_idx(list, seq, *pos);
793 static void xt_tgt_seq_stop(struct seq_file *seq, void *v)
795 struct proc_dir_entry *pde = seq->private;
796 u_int16_t af = (unsigned long)pde->data & 0xffff;
798 mutex_unlock(&xt[af].mutex);
801 static int xt_name_seq_show(struct seq_file *seq, void *v)
803 char *name = (char *)v + sizeof(struct list_head);
805 if (strlen(name))
806 return seq_printf(seq, "%s\n", name);
807 else
808 return 0;
811 static const struct seq_operations xt_tgt_seq_ops = {
812 .start = xt_tgt_seq_start,
813 .next = xt_tgt_seq_next,
814 .stop = xt_tgt_seq_stop,
815 .show = xt_name_seq_show,
818 static int xt_tgt_open(struct inode *inode, struct file *file)
820 int ret;
822 ret = seq_open(file, &xt_tgt_seq_ops);
823 if (!ret) {
824 struct seq_file *seq = file->private_data;
825 struct proc_dir_entry *pde = PDE(inode);
827 seq->private = pde;
830 return ret;
833 static const struct file_operations xt_file_ops = {
834 .owner = THIS_MODULE,
835 .open = xt_tgt_open,
836 .read = seq_read,
837 .llseek = seq_lseek,
838 .release = seq_release,
841 #define FORMAT_TABLES "_tables_names"
842 #define FORMAT_MATCHES "_tables_matches"
843 #define FORMAT_TARGETS "_tables_targets"
845 #endif /* CONFIG_PROC_FS */
847 int xt_proto_init(int af)
849 #ifdef CONFIG_PROC_FS
850 char buf[XT_FUNCTION_MAXNAMELEN];
851 struct proc_dir_entry *proc;
852 #endif
854 if (af >= NPROTO)
855 return -EINVAL;
858 #ifdef CONFIG_PROC_FS
859 strlcpy(buf, xt_prefix[af], sizeof(buf));
860 strlcat(buf, FORMAT_TABLES, sizeof(buf));
861 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
862 if (!proc)
863 goto out;
864 proc->data = (void *) ((unsigned long) af | (TABLE << 16));
867 strlcpy(buf, xt_prefix[af], sizeof(buf));
868 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
869 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
870 if (!proc)
871 goto out_remove_tables;
872 proc->data = (void *) ((unsigned long) af | (MATCH << 16));
874 strlcpy(buf, xt_prefix[af], sizeof(buf));
875 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
876 proc = proc_net_fops_create(&init_net, buf, 0440, &xt_file_ops);
877 if (!proc)
878 goto out_remove_matches;
879 proc->data = (void *) ((unsigned long) af | (TARGET << 16));
880 #endif
882 return 0;
884 #ifdef CONFIG_PROC_FS
885 out_remove_matches:
886 strlcpy(buf, xt_prefix[af], sizeof(buf));
887 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
888 proc_net_remove(&init_net, buf);
890 out_remove_tables:
891 strlcpy(buf, xt_prefix[af], sizeof(buf));
892 strlcat(buf, FORMAT_TABLES, sizeof(buf));
893 proc_net_remove(&init_net, buf);
894 out:
895 return -1;
896 #endif
898 EXPORT_SYMBOL_GPL(xt_proto_init);
900 void xt_proto_fini(int af)
902 #ifdef CONFIG_PROC_FS
903 char buf[XT_FUNCTION_MAXNAMELEN];
905 strlcpy(buf, xt_prefix[af], sizeof(buf));
906 strlcat(buf, FORMAT_TABLES, sizeof(buf));
907 proc_net_remove(&init_net, buf);
909 strlcpy(buf, xt_prefix[af], sizeof(buf));
910 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
911 proc_net_remove(&init_net, buf);
913 strlcpy(buf, xt_prefix[af], sizeof(buf));
914 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
915 proc_net_remove(&init_net, buf);
916 #endif /*CONFIG_PROC_FS*/
918 EXPORT_SYMBOL_GPL(xt_proto_fini);
921 static int __init xt_init(void)
923 int i;
925 xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
926 if (!xt)
927 return -ENOMEM;
929 for (i = 0; i < NPROTO; i++) {
930 mutex_init(&xt[i].mutex);
931 #ifdef CONFIG_COMPAT
932 mutex_init(&xt[i].compat_mutex);
933 xt[i].compat_offsets = NULL;
934 #endif
935 INIT_LIST_HEAD(&xt[i].target);
936 INIT_LIST_HEAD(&xt[i].match);
937 INIT_LIST_HEAD(&xt[i].tables);
939 return 0;
942 static void __exit xt_fini(void)
944 kfree(xt);
947 module_init(xt_init);
948 module_exit(xt_fini);