blob: 6f4383ad86f9d01ce20fb9246dfe9f068d6ccfa3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
16MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
18MODULE_DESCRIPTION("ip6tables filter table");
19
Patrick McHardy6e23ae22007-11-19 18:53:30 -080020#define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
21 (1 << NF_INET_FORWARD) | \
22 (1 << NF_INET_LOCAL_OUT))
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static struct
25{
26 struct ip6t_replace repl;
27 struct ip6t_standard entries[3];
28 struct ip6t_error term;
Alexey Dobriyan8280aa62008-01-31 04:04:13 -080029} initial_table __net_initdata = {
Patrick McHardy3c2ad462007-05-10 14:14:16 -070030 .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 McHardy6e23ae22007-11-19 18:53:30 -080036 [NF_INET_LOCAL_IN] = 0,
37 [NF_INET_FORWARD] = sizeof(struct ip6t_standard),
38 [NF_INET_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2
Patrick McHardy3c2ad462007-05-10 14:14:16 -070039 },
40 .underflow = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -080041 [NF_INET_LOCAL_IN] = 0,
42 [NF_INET_FORWARD] = sizeof(struct ip6t_standard),
43 [NF_INET_LOCAL_OUT] = sizeof(struct ip6t_standard) * 2
Patrick McHardy3c2ad462007-05-10 14:14:16 -070044 },
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 Torvalds1da177e2005-04-16 15:20:36 -070052};
53
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020054static const struct xt_table packet_filter = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .name = "filter",
56 .valid_hooks = FILTER_VALID_HOOKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .me = THIS_MODULE,
Jan Engelhardtf88e6a82009-06-13 06:25:44 +020058 .af = NFPROTO_IPV6,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61/* The work comes in here from netfilter.c. */
62static unsigned int
Alexey Dobriyan61d30152008-11-20 09:58:08 +010063ip6t_in_hook(unsigned int hook,
Alexey Dobriyan43de9dfe2008-07-08 02:36:18 -070064 struct sk_buff *skb,
65 const struct net_device *in,
66 const struct net_device *out,
67 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Alexey Dobriyan43de9dfe2008-07-08 02:36:18 -070069 return ip6t_do_table(skb, hook, in, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +020070 dev_net(in)->ipv6.ip6table_filter);
Alexey Dobriyan43de9dfe2008-07-08 02:36:18 -070071}
72
73static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -070074ip6t_local_out_hook(unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -070075 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 const struct net_device *in,
77 const struct net_device *out,
78 int (*okfn)(struct sk_buff *))
79{
80#if 0
81 /* root is playing with raw sockets. */
Herbert Xu3db05fe2007-10-15 00:53:15 -070082 if (skb->len < sizeof(struct iphdr)
83 || ip_hdrlen(skb) < sizeof(struct iphdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (net_ratelimit())
85 printk("ip6t_hook: happy cracking.\n");
86 return NF_ACCEPT;
87 }
88#endif
89
Alexey Dobriyan43de9dfe2008-07-08 02:36:18 -070090 return ip6t_do_table(skb, hook, in, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +020091 dev_net(out)->ipv6.ip6table_filter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Patrick McHardy19994142007-12-05 01:23:00 -080094static struct nf_hook_ops ip6t_ops[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 {
Alexey Dobriyan61d30152008-11-20 09:58:08 +010096 .hook = ip6t_in_hook,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +020098 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -080099 .hooknum = NF_INET_LOCAL_IN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .priority = NF_IP6_PRI_FILTER,
101 },
102 {
Alexey Dobriyan61d30152008-11-20 09:58:08 +0100103 .hook = ip6t_in_hook,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +0200105 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800106 .hooknum = NF_INET_FORWARD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .priority = NF_IP6_PRI_FILTER,
108 },
109 {
110 .hook = ip6t_local_out_hook,
111 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +0200112 .pf = NFPROTO_IPV6,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800113 .hooknum = NF_INET_LOCAL_OUT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .priority = NF_IP6_PRI_FILTER,
115 },
116};
117
118/* Default to forward because I got too much mail already. */
119static int forward = NF_ACCEPT;
120module_param(forward, bool, 0000);
121
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800122static int __net_init ip6table_filter_net_init(struct net *net)
123{
124 /* Register table */
125 net->ipv6.ip6table_filter =
126 ip6t_register_table(net, &packet_filter, &initial_table.repl);
127 if (IS_ERR(net->ipv6.ip6table_filter))
128 return PTR_ERR(net->ipv6.ip6table_filter);
129 return 0;
130}
131
132static void __net_exit ip6table_filter_net_exit(struct net *net)
133{
134 ip6t_unregister_table(net->ipv6.ip6table_filter);
135}
136
137static struct pernet_operations ip6table_filter_net_ops = {
138 .init = ip6table_filter_net_init,
139 .exit = ip6table_filter_net_exit,
140};
141
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800142static int __init ip6table_filter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 int ret;
145
146 if (forward < 0 || forward > NF_MAX_VERDICT) {
147 printk("iptables forward must be 0 or 1\n");
148 return -EINVAL;
149 }
150
151 /* Entry 1 is the FORWARD hook */
152 initial_table.entries[1].target.verdict = -forward - 1;
153
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800154 ret = register_pernet_subsys(&ip6table_filter_net_ops);
155 if (ret < 0)
156 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 /* Register hooks */
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700159 ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (ret < 0)
161 goto cleanup_table;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return ret;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 cleanup_table:
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800166 unregister_pernet_subsys(&ip6table_filter_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return ret;
168}
169
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800170static void __exit ip6table_filter_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700172 nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800173 unregister_pernet_subsys(&ip6table_filter_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800176module_init(ip6table_filter_init);
177module_exit(ip6table_filter_fini);