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> |
| 9 | #include <linux/netfilter_arp/arp_tables.h> |
| 10 | |
| 11 | MODULE_LICENSE("GPL"); |
| 12 | MODULE_AUTHOR("David S. Miller <davem@redhat.com>"); |
| 13 | MODULE_DESCRIPTION("arptables filter table"); |
| 14 | |
| 15 | #define FILTER_VALID_HOOKS ((1 << NF_ARP_IN) | (1 << NF_ARP_OUT) | \ |
| 16 | (1 << NF_ARP_FORWARD)) |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | static struct |
| 19 | { |
| 20 | struct arpt_replace repl; |
| 21 | struct arpt_standard entries[3]; |
| 22 | struct arpt_error term; |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 23 | } initial_table __net_initdata = { |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 24 | .repl = { |
| 25 | .name = "filter", |
| 26 | .valid_hooks = FILTER_VALID_HOOKS, |
| 27 | .num_entries = 4, |
| 28 | .size = sizeof(struct arpt_standard) * 3 + sizeof(struct arpt_error), |
| 29 | .hook_entry = { |
| 30 | [NF_ARP_IN] = 0, |
| 31 | [NF_ARP_OUT] = sizeof(struct arpt_standard), |
| 32 | [NF_ARP_FORWARD] = 2 * sizeof(struct arpt_standard), |
| 33 | }, |
| 34 | .underflow = { |
| 35 | [NF_ARP_IN] = 0, |
| 36 | [NF_ARP_OUT] = sizeof(struct arpt_standard), |
| 37 | [NF_ARP_FORWARD] = 2 * sizeof(struct arpt_standard), |
| 38 | }, |
| 39 | }, |
| 40 | .entries = { |
| 41 | ARPT_STANDARD_INIT(NF_ACCEPT), /* ARP_IN */ |
| 42 | ARPT_STANDARD_INIT(NF_ACCEPT), /* ARP_OUT */ |
| 43 | ARPT_STANDARD_INIT(NF_ACCEPT), /* ARP_FORWARD */ |
| 44 | }, |
| 45 | .term = ARPT_ERROR_INIT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Jan Engelhardt | 4abff07 | 2008-04-14 11:15:43 +0200 | [diff] [blame] | 48 | static struct xt_table packet_filter = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | .name = "filter", |
| 50 | .valid_hooks = FILTER_VALID_HOOKS, |
Robert P. J. Day | fdccecd | 2008-04-14 09:56:03 +0200 | [diff] [blame] | 51 | .lock = __RW_LOCK_UNLOCKED(packet_filter.lock), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | .private = NULL, |
| 53 | .me = THIS_MODULE, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 54 | .af = NF_ARP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /* The work comes in here from netfilter.c */ |
| 58 | static unsigned int arpt_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 59 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | const struct net_device *in, |
| 61 | const struct net_device *out, |
| 62 | int (*okfn)(struct sk_buff *)) |
| 63 | { |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 64 | return arpt_do_table(skb, hook, in, out, init_net.ipv4.arptable_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Patrick McHardy | 1999414 | 2007-12-05 01:23:00 -0800 | [diff] [blame] | 67 | static struct nf_hook_ops arpt_ops[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | .hook = arpt_hook, |
| 70 | .owner = THIS_MODULE, |
| 71 | .pf = NF_ARP, |
| 72 | .hooknum = NF_ARP_IN, |
Jan Engelhardt | c2f9c68 | 2008-04-14 11:15:44 +0200 | [diff] [blame] | 73 | .priority = NF_IP_PRI_FILTER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | }, |
| 75 | { |
| 76 | .hook = arpt_hook, |
| 77 | .owner = THIS_MODULE, |
| 78 | .pf = NF_ARP, |
| 79 | .hooknum = NF_ARP_OUT, |
Jan Engelhardt | c2f9c68 | 2008-04-14 11:15:44 +0200 | [diff] [blame] | 80 | .priority = NF_IP_PRI_FILTER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | }, |
| 82 | { |
| 83 | .hook = arpt_hook, |
| 84 | .owner = THIS_MODULE, |
| 85 | .pf = NF_ARP, |
| 86 | .hooknum = NF_ARP_FORWARD, |
Jan Engelhardt | c2f9c68 | 2008-04-14 11:15:44 +0200 | [diff] [blame] | 87 | .priority = NF_IP_PRI_FILTER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | }, |
| 89 | }; |
| 90 | |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 91 | static int __net_init arptable_filter_net_init(struct net *net) |
| 92 | { |
| 93 | /* Register table */ |
| 94 | net->ipv4.arptable_filter = |
| 95 | arpt_register_table(net, &packet_filter, &initial_table.repl); |
| 96 | if (IS_ERR(net->ipv4.arptable_filter)) |
| 97 | return PTR_ERR(net->ipv4.arptable_filter); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void __net_exit arptable_filter_net_exit(struct net *net) |
| 102 | { |
| 103 | arpt_unregister_table(net->ipv4.arptable_filter); |
| 104 | } |
| 105 | |
| 106 | static struct pernet_operations arptable_filter_net_ops = { |
| 107 | .init = arptable_filter_net_init, |
| 108 | .exit = arptable_filter_net_exit, |
| 109 | }; |
| 110 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 111 | static int __init arptable_filter_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 113 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 115 | ret = register_pernet_subsys(&arptable_filter_net_ops); |
| 116 | if (ret < 0) |
| 117 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 119 | ret = nf_register_hooks(arpt_ops, ARRAY_SIZE(arpt_ops)); |
| 120 | if (ret < 0) |
| 121 | goto cleanup_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return ret; |
| 123 | |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 124 | cleanup_table: |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 125 | unregister_pernet_subsys(&arptable_filter_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | return ret; |
| 127 | } |
| 128 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 129 | static void __exit arptable_filter_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 131 | nf_unregister_hooks(arpt_ops, ARRAY_SIZE(arpt_ops)); |
Alexey Dobriyan | 9ea0cb2 | 2008-01-31 04:05:09 -0800 | [diff] [blame] | 132 | unregister_pernet_subsys(&arptable_filter_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 135 | module_init(arptable_filter_init); |
| 136 | module_exit(arptable_filter_fini); |