[NETFILTER]: Kconfig: improve conntrack selection
[hh.org.git] / net / netfilter / x_tables.c
blob58522fc65d333ca163fe25e4925cfe4496ecdbc6
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>
25 #include <linux/netfilter/x_tables.h>
26 #include <linux/netfilter_arp.h>
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
31 MODULE_DESCRIPTION("[ip,ip6,arp]_tables backend module");
33 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
35 struct xt_af {
36 struct mutex mutex;
37 struct list_head match;
38 struct list_head target;
39 struct list_head tables;
40 struct mutex compat_mutex;
43 static struct xt_af *xt;
45 #ifdef DEBUG_IP_FIREWALL_USER
46 #define duprintf(format, args...) printk(format , ## args)
47 #else
48 #define duprintf(format, args...)
49 #endif
51 enum {
52 TABLE,
53 TARGET,
54 MATCH,
57 static const char *xt_prefix[NPROTO] = {
58 [AF_INET] = "ip",
59 [AF_INET6] = "ip6",
60 [NF_ARP] = "arp",
63 /* Registration hooks for targets. */
64 int
65 xt_register_target(struct xt_target *target)
67 int ret, af = target->family;
69 ret = mutex_lock_interruptible(&xt[af].mutex);
70 if (ret != 0)
71 return ret;
72 list_add(&target->list, &xt[af].target);
73 mutex_unlock(&xt[af].mutex);
74 return ret;
76 EXPORT_SYMBOL(xt_register_target);
78 void
79 xt_unregister_target(struct xt_target *target)
81 int af = target->family;
83 mutex_lock(&xt[af].mutex);
84 list_del(&target->list);
85 mutex_unlock(&xt[af].mutex);
87 EXPORT_SYMBOL(xt_unregister_target);
89 int
90 xt_register_targets(struct xt_target *target, unsigned int n)
92 unsigned int i;
93 int err = 0;
95 for (i = 0; i < n; i++) {
96 err = xt_register_target(&target[i]);
97 if (err)
98 goto err;
100 return err;
102 err:
103 if (i > 0)
104 xt_unregister_targets(target, i);
105 return err;
107 EXPORT_SYMBOL(xt_register_targets);
109 void
110 xt_unregister_targets(struct xt_target *target, unsigned int n)
112 unsigned int i;
114 for (i = 0; i < n; i++)
115 xt_unregister_target(&target[i]);
117 EXPORT_SYMBOL(xt_unregister_targets);
120 xt_register_match(struct xt_match *match)
122 int ret, af = match->family;
124 ret = mutex_lock_interruptible(&xt[af].mutex);
125 if (ret != 0)
126 return ret;
128 list_add(&match->list, &xt[af].match);
129 mutex_unlock(&xt[af].mutex);
131 return ret;
133 EXPORT_SYMBOL(xt_register_match);
135 void
136 xt_unregister_match(struct xt_match *match)
138 int af = match->family;
140 mutex_lock(&xt[af].mutex);
141 list_del(&match->list);
142 mutex_unlock(&xt[af].mutex);
144 EXPORT_SYMBOL(xt_unregister_match);
147 xt_register_matches(struct xt_match *match, unsigned int n)
149 unsigned int i;
150 int err = 0;
152 for (i = 0; i < n; i++) {
153 err = xt_register_match(&match[i]);
154 if (err)
155 goto err;
157 return err;
159 err:
160 if (i > 0)
161 xt_unregister_matches(match, i);
162 return err;
164 EXPORT_SYMBOL(xt_register_matches);
166 void
167 xt_unregister_matches(struct xt_match *match, unsigned int n)
169 unsigned int i;
171 for (i = 0; i < n; i++)
172 xt_unregister_match(&match[i]);
174 EXPORT_SYMBOL(xt_unregister_matches);
178 * These are weird, but module loading must not be done with mutex
179 * held (since they will register), and we have to have a single
180 * function to use try_then_request_module().
183 /* Find match, grabs ref. Returns ERR_PTR() on error. */
184 struct xt_match *xt_find_match(int af, const char *name, u8 revision)
186 struct xt_match *m;
187 int err = 0;
189 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
190 return ERR_PTR(-EINTR);
192 list_for_each_entry(m, &xt[af].match, list) {
193 if (strcmp(m->name, name) == 0) {
194 if (m->revision == revision) {
195 if (try_module_get(m->me)) {
196 mutex_unlock(&xt[af].mutex);
197 return m;
199 } else
200 err = -EPROTOTYPE; /* Found something. */
203 mutex_unlock(&xt[af].mutex);
204 return ERR_PTR(err);
206 EXPORT_SYMBOL(xt_find_match);
208 /* Find target, grabs ref. Returns ERR_PTR() on error. */
209 struct xt_target *xt_find_target(int af, const char *name, u8 revision)
211 struct xt_target *t;
212 int err = 0;
214 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
215 return ERR_PTR(-EINTR);
217 list_for_each_entry(t, &xt[af].target, list) {
218 if (strcmp(t->name, name) == 0) {
219 if (t->revision == revision) {
220 if (try_module_get(t->me)) {
221 mutex_unlock(&xt[af].mutex);
222 return t;
224 } else
225 err = -EPROTOTYPE; /* Found something. */
228 mutex_unlock(&xt[af].mutex);
229 return ERR_PTR(err);
231 EXPORT_SYMBOL(xt_find_target);
233 struct xt_target *xt_request_find_target(int af, const char *name, u8 revision)
235 struct xt_target *target;
237 target = try_then_request_module(xt_find_target(af, name, revision),
238 "%st_%s", xt_prefix[af], name);
239 if (IS_ERR(target) || !target)
240 return NULL;
241 return target;
243 EXPORT_SYMBOL_GPL(xt_request_find_target);
245 static int match_revfn(int af, const char *name, u8 revision, int *bestp)
247 struct xt_match *m;
248 int have_rev = 0;
250 list_for_each_entry(m, &xt[af].match, list) {
251 if (strcmp(m->name, name) == 0) {
252 if (m->revision > *bestp)
253 *bestp = m->revision;
254 if (m->revision == revision)
255 have_rev = 1;
258 return have_rev;
261 static int target_revfn(int af, const char *name, u8 revision, int *bestp)
263 struct xt_target *t;
264 int have_rev = 0;
266 list_for_each_entry(t, &xt[af].target, list) {
267 if (strcmp(t->name, name) == 0) {
268 if (t->revision > *bestp)
269 *bestp = t->revision;
270 if (t->revision == revision)
271 have_rev = 1;
274 return have_rev;
277 /* Returns true or false (if no such extension at all) */
278 int xt_find_revision(int af, const char *name, u8 revision, int target,
279 int *err)
281 int have_rev, best = -1;
283 if (mutex_lock_interruptible(&xt[af].mutex) != 0) {
284 *err = -EINTR;
285 return 1;
287 if (target == 1)
288 have_rev = target_revfn(af, name, revision, &best);
289 else
290 have_rev = match_revfn(af, name, revision, &best);
291 mutex_unlock(&xt[af].mutex);
293 /* Nothing at all? Return 0 to try loading module. */
294 if (best == -1) {
295 *err = -ENOENT;
296 return 0;
299 *err = best;
300 if (!have_rev)
301 *err = -EPROTONOSUPPORT;
302 return 1;
304 EXPORT_SYMBOL_GPL(xt_find_revision);
306 int xt_check_match(const struct xt_match *match, unsigned short family,
307 unsigned int size, const char *table, unsigned int hook_mask,
308 unsigned short proto, int inv_proto)
310 if (XT_ALIGN(match->matchsize) != size) {
311 printk("%s_tables: %s match: invalid size %Zu != %u\n",
312 xt_prefix[family], match->name,
313 XT_ALIGN(match->matchsize), size);
314 return -EINVAL;
316 if (match->table && strcmp(match->table, table)) {
317 printk("%s_tables: %s match: only valid in %s table, not %s\n",
318 xt_prefix[family], match->name, match->table, table);
319 return -EINVAL;
321 if (match->hooks && (hook_mask & ~match->hooks) != 0) {
322 printk("%s_tables: %s match: bad hook_mask %u\n",
323 xt_prefix[family], match->name, hook_mask);
324 return -EINVAL;
326 if (match->proto && (match->proto != proto || inv_proto)) {
327 printk("%s_tables: %s match: only valid for protocol %u\n",
328 xt_prefix[family], match->name, match->proto);
329 return -EINVAL;
331 return 0;
333 EXPORT_SYMBOL_GPL(xt_check_match);
335 #ifdef CONFIG_COMPAT
336 int xt_compat_match_offset(struct xt_match *match)
338 u_int16_t csize = match->compatsize ? : match->matchsize;
339 return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
341 EXPORT_SYMBOL_GPL(xt_compat_match_offset);
343 void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
344 int *size)
346 struct xt_match *match = m->u.kernel.match;
347 struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
348 int pad, off = xt_compat_match_offset(match);
349 u_int16_t msize = cm->u.user.match_size;
351 m = *dstptr;
352 memcpy(m, cm, sizeof(*cm));
353 if (match->compat_from_user)
354 match->compat_from_user(m->data, cm->data);
355 else
356 memcpy(m->data, cm->data, msize - sizeof(*cm));
357 pad = XT_ALIGN(match->matchsize) - match->matchsize;
358 if (pad > 0)
359 memset(m->data + match->matchsize, 0, pad);
361 msize += off;
362 m->u.user.match_size = msize;
364 *size += off;
365 *dstptr += msize;
367 EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
369 int xt_compat_match_to_user(struct xt_entry_match *m, void __user **dstptr,
370 int *size)
372 struct xt_match *match = m->u.kernel.match;
373 struct compat_xt_entry_match __user *cm = *dstptr;
374 int off = xt_compat_match_offset(match);
375 u_int16_t msize = m->u.user.match_size - off;
377 if (copy_to_user(cm, m, sizeof(*cm)) ||
378 put_user(msize, &cm->u.user.match_size))
379 return -EFAULT;
381 if (match->compat_to_user) {
382 if (match->compat_to_user((void __user *)cm->data, m->data))
383 return -EFAULT;
384 } else {
385 if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
386 return -EFAULT;
389 *size -= off;
390 *dstptr += msize;
391 return 0;
393 EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
394 #endif /* CONFIG_COMPAT */
396 int xt_check_target(const struct xt_target *target, unsigned short family,
397 unsigned int size, const char *table, unsigned int hook_mask,
398 unsigned short proto, int inv_proto)
400 if (XT_ALIGN(target->targetsize) != size) {
401 printk("%s_tables: %s target: invalid size %Zu != %u\n",
402 xt_prefix[family], target->name,
403 XT_ALIGN(target->targetsize), size);
404 return -EINVAL;
406 if (target->table && strcmp(target->table, table)) {
407 printk("%s_tables: %s target: only valid in %s table, not %s\n",
408 xt_prefix[family], target->name, target->table, table);
409 return -EINVAL;
411 if (target->hooks && (hook_mask & ~target->hooks) != 0) {
412 printk("%s_tables: %s target: bad hook_mask %u\n",
413 xt_prefix[family], target->name, hook_mask);
414 return -EINVAL;
416 if (target->proto && (target->proto != proto || inv_proto)) {
417 printk("%s_tables: %s target: only valid for protocol %u\n",
418 xt_prefix[family], target->name, target->proto);
419 return -EINVAL;
421 return 0;
423 EXPORT_SYMBOL_GPL(xt_check_target);
425 #ifdef CONFIG_COMPAT
426 int xt_compat_target_offset(struct xt_target *target)
428 u_int16_t csize = target->compatsize ? : target->targetsize;
429 return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
431 EXPORT_SYMBOL_GPL(xt_compat_target_offset);
433 void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
434 int *size)
436 struct xt_target *target = t->u.kernel.target;
437 struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
438 int pad, off = xt_compat_target_offset(target);
439 u_int16_t tsize = ct->u.user.target_size;
441 t = *dstptr;
442 memcpy(t, ct, sizeof(*ct));
443 if (target->compat_from_user)
444 target->compat_from_user(t->data, ct->data);
445 else
446 memcpy(t->data, ct->data, tsize - sizeof(*ct));
447 pad = XT_ALIGN(target->targetsize) - target->targetsize;
448 if (pad > 0)
449 memset(t->data + target->targetsize, 0, pad);
451 tsize += off;
452 t->u.user.target_size = tsize;
454 *size += off;
455 *dstptr += tsize;
457 EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
459 int xt_compat_target_to_user(struct xt_entry_target *t, void __user **dstptr,
460 int *size)
462 struct xt_target *target = t->u.kernel.target;
463 struct compat_xt_entry_target __user *ct = *dstptr;
464 int off = xt_compat_target_offset(target);
465 u_int16_t tsize = t->u.user.target_size - off;
467 if (copy_to_user(ct, t, sizeof(*ct)) ||
468 put_user(tsize, &ct->u.user.target_size))
469 return -EFAULT;
471 if (target->compat_to_user) {
472 if (target->compat_to_user((void __user *)ct->data, t->data))
473 return -EFAULT;
474 } else {
475 if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
476 return -EFAULT;
479 *size -= off;
480 *dstptr += tsize;
481 return 0;
483 EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
484 #endif
486 struct xt_table_info *xt_alloc_table_info(unsigned int size)
488 struct xt_table_info *newinfo;
489 int cpu;
491 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
492 if ((SMP_ALIGN(size) >> PAGE_SHIFT) + 2 > num_physpages)
493 return NULL;
495 newinfo = kzalloc(sizeof(struct xt_table_info), GFP_KERNEL);
496 if (!newinfo)
497 return NULL;
499 newinfo->size = size;
501 for_each_possible_cpu(cpu) {
502 if (size <= PAGE_SIZE)
503 newinfo->entries[cpu] = kmalloc_node(size,
504 GFP_KERNEL,
505 cpu_to_node(cpu));
506 else
507 newinfo->entries[cpu] = vmalloc_node(size,
508 cpu_to_node(cpu));
510 if (newinfo->entries[cpu] == NULL) {
511 xt_free_table_info(newinfo);
512 return NULL;
516 return newinfo;
518 EXPORT_SYMBOL(xt_alloc_table_info);
520 void xt_free_table_info(struct xt_table_info *info)
522 int cpu;
524 for_each_possible_cpu(cpu) {
525 if (info->size <= PAGE_SIZE)
526 kfree(info->entries[cpu]);
527 else
528 vfree(info->entries[cpu]);
530 kfree(info);
532 EXPORT_SYMBOL(xt_free_table_info);
534 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
535 struct xt_table *xt_find_table_lock(int af, const char *name)
537 struct xt_table *t;
539 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
540 return ERR_PTR(-EINTR);
542 list_for_each_entry(t, &xt[af].tables, list)
543 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
544 return t;
545 mutex_unlock(&xt[af].mutex);
546 return NULL;
548 EXPORT_SYMBOL_GPL(xt_find_table_lock);
550 void xt_table_unlock(struct xt_table *table)
552 mutex_unlock(&xt[table->af].mutex);
554 EXPORT_SYMBOL_GPL(xt_table_unlock);
556 #ifdef CONFIG_COMPAT
557 void xt_compat_lock(int af)
559 mutex_lock(&xt[af].compat_mutex);
561 EXPORT_SYMBOL_GPL(xt_compat_lock);
563 void xt_compat_unlock(int af)
565 mutex_unlock(&xt[af].compat_mutex);
567 EXPORT_SYMBOL_GPL(xt_compat_unlock);
568 #endif
570 struct xt_table_info *
571 xt_replace_table(struct xt_table *table,
572 unsigned int num_counters,
573 struct xt_table_info *newinfo,
574 int *error)
576 struct xt_table_info *oldinfo, *private;
578 /* Do the substitution. */
579 write_lock_bh(&table->lock);
580 private = table->private;
581 /* Check inside lock: is the old number correct? */
582 if (num_counters != private->number) {
583 duprintf("num_counters != table->private->number (%u/%u)\n",
584 num_counters, private->number);
585 write_unlock_bh(&table->lock);
586 *error = -EAGAIN;
587 return NULL;
589 oldinfo = private;
590 table->private = newinfo;
591 newinfo->initial_entries = oldinfo->initial_entries;
592 write_unlock_bh(&table->lock);
594 return oldinfo;
596 EXPORT_SYMBOL_GPL(xt_replace_table);
598 int xt_register_table(struct xt_table *table,
599 struct xt_table_info *bootstrap,
600 struct xt_table_info *newinfo)
602 int ret;
603 struct xt_table_info *private;
604 struct xt_table *t;
606 ret = mutex_lock_interruptible(&xt[table->af].mutex);
607 if (ret != 0)
608 return ret;
610 /* Don't autoload: we'd eat our tail... */
611 list_for_each_entry(t, &xt[table->af].tables, list) {
612 if (strcmp(t->name, table->name) == 0) {
613 ret = -EEXIST;
614 goto unlock;
618 /* Simplifies replace_table code. */
619 table->private = bootstrap;
620 rwlock_init(&table->lock);
621 if (!xt_replace_table(table, 0, newinfo, &ret))
622 goto unlock;
624 private = table->private;
625 duprintf("table->private->number = %u\n", private->number);
627 /* save number of initial entries */
628 private->initial_entries = private->number;
630 list_add(&table->list, &xt[table->af].tables);
632 ret = 0;
633 unlock:
634 mutex_unlock(&xt[table->af].mutex);
635 return ret;
637 EXPORT_SYMBOL_GPL(xt_register_table);
639 void *xt_unregister_table(struct xt_table *table)
641 struct xt_table_info *private;
643 mutex_lock(&xt[table->af].mutex);
644 private = table->private;
645 list_del(&table->list);
646 mutex_unlock(&xt[table->af].mutex);
648 return private;
650 EXPORT_SYMBOL_GPL(xt_unregister_table);
652 #ifdef CONFIG_PROC_FS
653 static char *xt_proto_prefix[NPROTO] = {
654 [AF_INET] = "ip",
655 [AF_INET6] = "ip6",
656 [NF_ARP] = "arp",
659 static struct list_head *xt_get_idx(struct list_head *list, struct seq_file *seq, loff_t pos)
661 struct list_head *head = list->next;
663 if (!head || list_empty(list))
664 return NULL;
666 while (pos && (head = head->next)) {
667 if (head == list)
668 return NULL;
669 pos--;
671 return pos ? NULL : head;
674 static struct list_head *type2list(u_int16_t af, u_int16_t type)
676 struct list_head *list;
678 switch (type) {
679 case TARGET:
680 list = &xt[af].target;
681 break;
682 case MATCH:
683 list = &xt[af].match;
684 break;
685 case TABLE:
686 list = &xt[af].tables;
687 break;
688 default:
689 list = NULL;
690 break;
693 return list;
696 static void *xt_tgt_seq_start(struct seq_file *seq, loff_t *pos)
698 struct proc_dir_entry *pde = (struct proc_dir_entry *) seq->private;
699 u_int16_t af = (unsigned long)pde->data & 0xffff;
700 u_int16_t type = (unsigned long)pde->data >> 16;
701 struct list_head *list;
703 if (af >= NPROTO)
704 return NULL;
706 list = type2list(af, type);
707 if (!list)
708 return NULL;
710 if (mutex_lock_interruptible(&xt[af].mutex) != 0)
711 return NULL;
713 return xt_get_idx(list, seq, *pos);
716 static void *xt_tgt_seq_next(struct seq_file *seq, void *v, loff_t *pos)
718 struct proc_dir_entry *pde = seq->private;
719 u_int16_t af = (unsigned long)pde->data & 0xffff;
720 u_int16_t type = (unsigned long)pde->data >> 16;
721 struct list_head *list;
723 if (af >= NPROTO)
724 return NULL;
726 list = type2list(af, type);
727 if (!list)
728 return NULL;
730 (*pos)++;
731 return xt_get_idx(list, seq, *pos);
734 static void xt_tgt_seq_stop(struct seq_file *seq, void *v)
736 struct proc_dir_entry *pde = seq->private;
737 u_int16_t af = (unsigned long)pde->data & 0xffff;
739 mutex_unlock(&xt[af].mutex);
742 static int xt_name_seq_show(struct seq_file *seq, void *v)
744 char *name = (char *)v + sizeof(struct list_head);
746 if (strlen(name))
747 return seq_printf(seq, "%s\n", name);
748 else
749 return 0;
752 static struct seq_operations xt_tgt_seq_ops = {
753 .start = xt_tgt_seq_start,
754 .next = xt_tgt_seq_next,
755 .stop = xt_tgt_seq_stop,
756 .show = xt_name_seq_show,
759 static int xt_tgt_open(struct inode *inode, struct file *file)
761 int ret;
763 ret = seq_open(file, &xt_tgt_seq_ops);
764 if (!ret) {
765 struct seq_file *seq = file->private_data;
766 struct proc_dir_entry *pde = PDE(inode);
768 seq->private = pde;
771 return ret;
774 static struct file_operations xt_file_ops = {
775 .owner = THIS_MODULE,
776 .open = xt_tgt_open,
777 .read = seq_read,
778 .llseek = seq_lseek,
779 .release = seq_release,
782 #define FORMAT_TABLES "_tables_names"
783 #define FORMAT_MATCHES "_tables_matches"
784 #define FORMAT_TARGETS "_tables_targets"
786 #endif /* CONFIG_PROC_FS */
788 int xt_proto_init(int af)
790 #ifdef CONFIG_PROC_FS
791 char buf[XT_FUNCTION_MAXNAMELEN];
792 struct proc_dir_entry *proc;
793 #endif
795 if (af >= NPROTO)
796 return -EINVAL;
799 #ifdef CONFIG_PROC_FS
800 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
801 strlcat(buf, FORMAT_TABLES, sizeof(buf));
802 proc = proc_net_fops_create(buf, 0440, &xt_file_ops);
803 if (!proc)
804 goto out;
805 proc->data = (void *) ((unsigned long) af | (TABLE << 16));
808 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
809 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
810 proc = proc_net_fops_create(buf, 0440, &xt_file_ops);
811 if (!proc)
812 goto out_remove_tables;
813 proc->data = (void *) ((unsigned long) af | (MATCH << 16));
815 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
816 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
817 proc = proc_net_fops_create(buf, 0440, &xt_file_ops);
818 if (!proc)
819 goto out_remove_matches;
820 proc->data = (void *) ((unsigned long) af | (TARGET << 16));
821 #endif
823 return 0;
825 #ifdef CONFIG_PROC_FS
826 out_remove_matches:
827 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
828 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
829 proc_net_remove(buf);
831 out_remove_tables:
832 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
833 strlcat(buf, FORMAT_TABLES, sizeof(buf));
834 proc_net_remove(buf);
835 out:
836 return -1;
837 #endif
839 EXPORT_SYMBOL_GPL(xt_proto_init);
841 void xt_proto_fini(int af)
843 #ifdef CONFIG_PROC_FS
844 char buf[XT_FUNCTION_MAXNAMELEN];
846 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
847 strlcat(buf, FORMAT_TABLES, sizeof(buf));
848 proc_net_remove(buf);
850 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
851 strlcat(buf, FORMAT_TARGETS, sizeof(buf));
852 proc_net_remove(buf);
854 strlcpy(buf, xt_proto_prefix[af], sizeof(buf));
855 strlcat(buf, FORMAT_MATCHES, sizeof(buf));
856 proc_net_remove(buf);
857 #endif /*CONFIG_PROC_FS*/
859 EXPORT_SYMBOL_GPL(xt_proto_fini);
862 static int __init xt_init(void)
864 int i;
866 xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
867 if (!xt)
868 return -ENOMEM;
870 for (i = 0; i < NPROTO; i++) {
871 mutex_init(&xt[i].mutex);
872 #ifdef CONFIG_COMPAT
873 mutex_init(&xt[i].compat_mutex);
874 #endif
875 INIT_LIST_HEAD(&xt[i].target);
876 INIT_LIST_HEAD(&xt[i].match);
877 INIT_LIST_HEAD(&xt[i].tables);
879 return 0;
882 static void __exit xt_fini(void)
884 kfree(xt);
887 module_init(xt_init);
888 module_exit(xt_fini);