blob: 6e95d0614ca9cb9aa16ae6e03758ef9514de8028 [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,
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020059 .priority = NF_IP6_PRI_FILTER,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
62/* The work comes in here from netfilter.c. */
63static unsigned int
Jan Engelhardt737535c2009-06-13 06:46:36 +020064ip6table_filter_hook(unsigned int hook, struct sk_buff *skb,
65 const struct net_device *in, const struct net_device *out,
66 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Jan Engelhardt2b21e052009-06-13 06:57:10 +020068 const struct net *net = dev_net((in != NULL) ? in : out);
Jan Engelhardt737535c2009-06-13 06:46:36 +020069
Jan Engelhardt2b21e052009-06-13 06:57:10 +020070 return ip6t_do_table(skb, hook, in, out, net->ipv6.ip6table_filter);
Alexey Dobriyan43de9dfe2008-07-08 02:36:18 -070071}
72
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020073static struct nf_hook_ops *filter_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/* Default to forward because I got too much mail already. */
76static int forward = NF_ACCEPT;
77module_param(forward, bool, 0000);
78
Alexey Dobriyan8280aa62008-01-31 04:04:13 -080079static int __net_init ip6table_filter_net_init(struct net *net)
80{
81 /* Register table */
82 net->ipv6.ip6table_filter =
83 ip6t_register_table(net, &packet_filter, &initial_table.repl);
84 if (IS_ERR(net->ipv6.ip6table_filter))
85 return PTR_ERR(net->ipv6.ip6table_filter);
86 return 0;
87}
88
89static void __net_exit ip6table_filter_net_exit(struct net *net)
90{
Alexey Dobriyanf54e9362010-01-18 08:25:47 +010091 ip6t_unregister_table(net, net->ipv6.ip6table_filter);
Alexey Dobriyan8280aa62008-01-31 04:04:13 -080092}
93
94static struct pernet_operations ip6table_filter_net_ops = {
95 .init = ip6table_filter_net_init,
96 .exit = ip6table_filter_net_exit,
97};
98
Andrew Morton65b4b4e2006-03-28 16:37:06 -080099static int __init ip6table_filter_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 int ret;
102
103 if (forward < 0 || forward > NF_MAX_VERDICT) {
104 printk("iptables forward must be 0 or 1\n");
105 return -EINVAL;
106 }
107
108 /* Entry 1 is the FORWARD hook */
109 initial_table.entries[1].target.verdict = -forward - 1;
110
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800111 ret = register_pernet_subsys(&ip6table_filter_net_ops);
112 if (ret < 0)
113 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /* Register hooks */
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200116 filter_ops = xt_hook_link(&packet_filter, ip6table_filter_hook);
117 if (IS_ERR(filter_ops)) {
118 ret = PTR_ERR(filter_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 goto cleanup_table;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return ret;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 cleanup_table:
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800125 unregister_pernet_subsys(&ip6table_filter_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return ret;
127}
128
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800129static void __exit ip6table_filter_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200131 xt_hook_unlink(&packet_filter, filter_ops);
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800132 unregister_pernet_subsys(&ip6table_filter_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800135module_init(ip6table_filter_init);
136module_exit(ip6table_filter_fini);