Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x. |
| 3 | * |
| 4 | * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling |
| 5 | * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/moduleparam.h> |
| 14 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 15 | |
| 16 | MODULE_LICENSE("GPL"); |
| 17 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 18 | MODULE_DESCRIPTION("ip6tables filter table"); |
| 19 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 20 | #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \ |
| 21 | (1 << NF_INET_FORWARD) | \ |
| 22 | (1 << NF_INET_LOCAL_OUT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | static struct |
| 25 | { |
| 26 | struct ip6t_replace repl; |
| 27 | struct ip6t_standard entries[3]; |
| 28 | struct ip6t_error term; |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 29 | } initial_table __net_initdata = { |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 30 | .repl = { |
| 31 | .name = "filter", |
| 32 | .valid_hooks = FILTER_VALID_HOOKS, |
| 33 | .num_entries = 4, |
| 34 | .size = sizeof(struct ip6t_standard) * 3 + sizeof(struct ip6t_error), |
| 35 | .hook_entry = { |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 36 | [NF_INET_LOCAL_IN] = 0, |
| 37 | [NF_INET_FORWARD] = sizeof(struct ip6t_standard), |
| 38 | [NF_INET_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2 |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 39 | }, |
| 40 | .underflow = { |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 41 | [NF_INET_LOCAL_IN] = 0, |
| 42 | [NF_INET_FORWARD] = sizeof(struct ip6t_standard), |
| 43 | [NF_INET_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2 |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 44 | }, |
| 45 | }, |
| 46 | .entries = { |
| 47 | IP6T_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */ |
| 48 | IP6T_STANDARD_INIT(NF_ACCEPT), /* FORWARD */ |
| 49 | IP6T_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */ |
| 50 | }, |
| 51 | .term = IP6T_ERROR_INIT, /* ERROR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 54 | static struct xt_table packet_filter = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | .name = "filter", |
| 56 | .valid_hooks = FILTER_VALID_HOOKS, |
Robert P. J. Day | fdccecd | 2008-04-14 09:56:03 +0200 | [diff] [blame] | 57 | .lock = __RW_LOCK_UNLOCKED(packet_filter.lock), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | .me = THIS_MODULE, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 59 | .af = AF_INET6, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /* The work comes in here from netfilter.c. */ |
| 63 | static unsigned int |
Alexey Dobriyan | 61d3015 | 2008-11-20 09:58:08 +0100 | [diff] [blame] | 64 | ip6t_in_hook(unsigned int hook, |
Alexey Dobriyan | 43de9dfe | 2008-07-08 02:36:18 -0700 | [diff] [blame] | 65 | struct sk_buff *skb, |
| 66 | const struct net_device *in, |
| 67 | const struct net_device *out, |
| 68 | int (*okfn)(struct sk_buff *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Alexey Dobriyan | 43de9dfe | 2008-07-08 02:36:18 -0700 | [diff] [blame] | 70 | return ip6t_do_table(skb, hook, in, out, |
Alexey Dobriyan | 48dc786 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 71 | dev_net(in)->ipv6.ip6table_filter); |
Alexey Dobriyan | 43de9dfe | 2008-07-08 02:36:18 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static unsigned int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | ip6t_local_out_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 76 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | const struct net_device *in, |
| 78 | const struct net_device *out, |
| 79 | int (*okfn)(struct sk_buff *)) |
| 80 | { |
| 81 | #if 0 |
| 82 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 83 | if (skb->len < sizeof(struct iphdr) |
| 84 | || ip_hdrlen(skb) < sizeof(struct iphdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (net_ratelimit()) |
| 86 | printk("ip6t_hook: happy cracking.\n"); |
| 87 | return NF_ACCEPT; |
| 88 | } |
| 89 | #endif |
| 90 | |
Alexey Dobriyan | 43de9dfe | 2008-07-08 02:36:18 -0700 | [diff] [blame] | 91 | return ip6t_do_table(skb, hook, in, out, |
Alexey Dobriyan | 48dc786 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 92 | dev_net(out)->ipv6.ip6table_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Patrick McHardy | 1999414 | 2007-12-05 01:23:00 -0800 | [diff] [blame] | 95 | static struct nf_hook_ops ip6t_ops[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Alexey Dobriyan | 61d3015 | 2008-11-20 09:58:08 +0100 | [diff] [blame] | 97 | .hook = ip6t_in_hook, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | .owner = THIS_MODULE, |
| 99 | .pf = PF_INET6, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 100 | .hooknum = NF_INET_LOCAL_IN, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | .priority = NF_IP6_PRI_FILTER, |
| 102 | }, |
| 103 | { |
Alexey Dobriyan | 61d3015 | 2008-11-20 09:58:08 +0100 | [diff] [blame] | 104 | .hook = ip6t_in_hook, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | .owner = THIS_MODULE, |
| 106 | .pf = PF_INET6, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 107 | .hooknum = NF_INET_FORWARD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .priority = NF_IP6_PRI_FILTER, |
| 109 | }, |
| 110 | { |
| 111 | .hook = ip6t_local_out_hook, |
| 112 | .owner = THIS_MODULE, |
| 113 | .pf = PF_INET6, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 114 | .hooknum = NF_INET_LOCAL_OUT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | .priority = NF_IP6_PRI_FILTER, |
| 116 | }, |
| 117 | }; |
| 118 | |
| 119 | /* Default to forward because I got too much mail already. */ |
| 120 | static int forward = NF_ACCEPT; |
| 121 | module_param(forward, bool, 0000); |
| 122 | |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 123 | static int __net_init ip6table_filter_net_init(struct net *net) |
| 124 | { |
| 125 | /* Register table */ |
| 126 | net->ipv6.ip6table_filter = |
| 127 | ip6t_register_table(net, &packet_filter, &initial_table.repl); |
| 128 | if (IS_ERR(net->ipv6.ip6table_filter)) |
| 129 | return PTR_ERR(net->ipv6.ip6table_filter); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static void __net_exit ip6table_filter_net_exit(struct net *net) |
| 134 | { |
| 135 | ip6t_unregister_table(net->ipv6.ip6table_filter); |
| 136 | } |
| 137 | |
| 138 | static struct pernet_operations ip6table_filter_net_ops = { |
| 139 | .init = ip6table_filter_net_init, |
| 140 | .exit = ip6table_filter_net_exit, |
| 141 | }; |
| 142 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 143 | static int __init ip6table_filter_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | int ret; |
| 146 | |
| 147 | if (forward < 0 || forward > NF_MAX_VERDICT) { |
| 148 | printk("iptables forward must be 0 or 1\n"); |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | |
| 152 | /* Entry 1 is the FORWARD hook */ |
| 153 | initial_table.entries[1].target.verdict = -forward - 1; |
| 154 | |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 155 | ret = register_pernet_subsys(&ip6table_filter_net_ops); |
| 156 | if (ret < 0) |
| 157 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | /* Register hooks */ |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 160 | ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | if (ret < 0) |
| 162 | goto cleanup_table; |
| 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return ret; |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | cleanup_table: |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 167 | unregister_pernet_subsys(&ip6table_filter_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 171 | static void __exit ip6table_filter_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 173 | nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops)); |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 174 | unregister_pernet_subsys(&ip6table_filter_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 177 | module_init(ip6table_filter_init); |
| 178 | module_exit(ip6table_filter_fini); |