Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Filtering ARP tables module. |
| 3 | * |
| 4 | * Copyright (C) 2002 David S. Miller (davem@redhat.com) |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 9 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/netfilter_arp/arp_tables.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | MODULE_LICENSE("GPL"); |
| 14 | MODULE_AUTHOR("David S. Miller <davem@redhat.com>"); |
| 15 | MODULE_DESCRIPTION("arptables filter table"); |
| 16 | |
| 17 | #define FILTER_VALID_HOOKS ((1 << NF_ARP_IN) | (1 << NF_ARP_OUT) | \ |
| 18 | (1 << NF_ARP_FORWARD)) |
| 19 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 20 | static const struct xt_table packet_filter = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | .name = "filter", |
| 22 | .valid_hooks = FILTER_VALID_HOOKS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | .me = THIS_MODULE, |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 24 | .af = NFPROTO_ARP, |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 25 | .priority = NF_IP_PRI_FILTER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | /* The work comes in here from netfilter.c */ |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 29 | static unsigned int |
| 30 | arptable_filter_hook(unsigned int hook, struct sk_buff *skb, |
| 31 | const struct net_device *in, const struct net_device *out, |
| 32 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Jan Engelhardt | 2b21e05 | 2009-06-13 06:57:10 +0200 | [diff] [blame] | 34 | const struct net *net = dev_net((in != NULL) ? in : out); |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 35 | |
Jan Engelhardt | 2b21e05 | 2009-06-13 06:57:10 +0200 | [diff] [blame] | 36 | return arpt_do_table(skb, hook, in, out, net->ipv4.arptable_filter); |
Alexey Dobriyan | 3918fed5 | 2008-07-26 17:48:59 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 39 | static struct nf_hook_ops *arpfilter_ops __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 41 | static int __net_init arptable_filter_net_init(struct net *net) |
| 42 | { |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 43 | struct arpt_replace *repl; |
| 44 | |
| 45 | repl = arpt_alloc_initial_table(&packet_filter); |
| 46 | if (repl == NULL) |
| 47 | return -ENOMEM; |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 48 | net->ipv4.arptable_filter = |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 49 | arpt_register_table(net, &packet_filter, repl); |
| 50 | kfree(repl); |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 51 | if (IS_ERR(net->ipv4.arptable_filter)) |
| 52 | return PTR_ERR(net->ipv4.arptable_filter); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static void __net_exit arptable_filter_net_exit(struct net *net) |
| 57 | { |
| 58 | arpt_unregister_table(net->ipv4.arptable_filter); |
| 59 | } |
| 60 | |
| 61 | static struct pernet_operations arptable_filter_net_ops = { |
| 62 | .init = arptable_filter_net_init, |
| 63 | .exit = arptable_filter_net_exit, |
| 64 | }; |
| 65 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 66 | static int __init arptable_filter_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 68 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 70 | ret = register_pernet_subsys(&arptable_filter_net_ops); |
| 71 | if (ret < 0) |
| 72 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 74 | arpfilter_ops = xt_hook_link(&packet_filter, arptable_filter_hook); |
| 75 | if (IS_ERR(arpfilter_ops)) { |
| 76 | ret = PTR_ERR(arpfilter_ops); |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 77 | goto cleanup_table; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return ret; |
| 80 | |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 81 | cleanup_table: |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 82 | unregister_pernet_subsys(&arptable_filter_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return ret; |
| 84 | } |
| 85 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 86 | static void __exit arptable_filter_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 88 | xt_hook_unlink(&packet_filter, arpfilter_ops); |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 89 | unregister_pernet_subsys(&arptable_filter_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 92 | module_init(arptable_filter_init); |
| 93 | module_exit(arptable_filter_fini); |