1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
61da177e4SLinus Torvalds  * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/moduleparam.h>
111da177e4SLinus Torvalds #include <linux/netfilter_ipv6/ip6_tables.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds MODULE_LICENSE("GPL");
151da177e4SLinus Torvalds MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
161da177e4SLinus Torvalds MODULE_DESCRIPTION("ip6tables filter table");
171da177e4SLinus Torvalds 
186e23ae2aSPatrick McHardy #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
196e23ae2aSPatrick McHardy 			    (1 << NF_INET_FORWARD) | \
206e23ae2aSPatrick McHardy 			    (1 << NF_INET_LOCAL_OUT))
211da177e4SLinus Torvalds 
2235aad0ffSJan Engelhardt static const struct xt_table packet_filter = {
231da177e4SLinus Torvalds 	.name		= "filter",
241da177e4SLinus Torvalds 	.valid_hooks	= FILTER_VALID_HOOKS,
251da177e4SLinus Torvalds 	.me		= THIS_MODULE,
26f88e6a8aSJan Engelhardt 	.af		= NFPROTO_IPV6,
272b95efe7SJan Engelhardt 	.priority	= NF_IP6_PRI_FILTER,
281da177e4SLinus Torvalds };
291da177e4SLinus Torvalds 
302b95efe7SJan Engelhardt static struct nf_hook_ops *filter_ops __read_mostly;
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /* Default to forward because I got too much mail already. */
33523f610eSRusty Russell static bool forward = true;
341da177e4SLinus Torvalds module_param(forward, bool, 0000);
351da177e4SLinus Torvalds 
ip6table_filter_table_init(struct net * net)36fdacd57cSFlorian Westphal static int ip6table_filter_table_init(struct net *net)
378280aa61SAlexey Dobriyan {
38e3eaa991SJan Engelhardt 	struct ip6t_replace *repl;
39a67dd266SFlorian Westphal 	int err;
40e3eaa991SJan Engelhardt 
41e3eaa991SJan Engelhardt 	repl = ip6t_alloc_initial_table(&packet_filter);
42e3eaa991SJan Engelhardt 	if (repl == NULL)
43e3eaa991SJan Engelhardt 		return -ENOMEM;
44e3eaa991SJan Engelhardt 	/* Entry 1 is the FORWARD hook */
45e3eaa991SJan Engelhardt 	((struct ip6t_standard *)repl->entries)[1].target.verdict =
46523f610eSRusty Russell 		forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
47e3eaa991SJan Engelhardt 
48ee177a54SFlorian Westphal 	err = ip6t_register_table(net, &packet_filter, repl, filter_ops);
49e3eaa991SJan Engelhardt 	kfree(repl);
50a67dd266SFlorian Westphal 	return err;
518280aa61SAlexey Dobriyan }
528280aa61SAlexey Dobriyan 
ip6table_filter_net_init(struct net * net)53b9e69e12SFlorian Westphal static int __net_init ip6table_filter_net_init(struct net *net)
54b9e69e12SFlorian Westphal {
55fdacd57cSFlorian Westphal 	if (!forward)
56b9e69e12SFlorian Westphal 		return ip6table_filter_table_init(net);
57b9e69e12SFlorian Westphal 
58b9e69e12SFlorian Westphal 	return 0;
59b9e69e12SFlorian Westphal }
60b9e69e12SFlorian Westphal 
ip6table_filter_net_pre_exit(struct net * net)615f027bc7SDavid Wilder static void __net_exit ip6table_filter_net_pre_exit(struct net *net)
625f027bc7SDavid Wilder {
63ee177a54SFlorian Westphal 	ip6t_unregister_table_pre_exit(net, "filter");
645f027bc7SDavid Wilder }
655f027bc7SDavid Wilder 
ip6table_filter_net_exit(struct net * net)668280aa61SAlexey Dobriyan static void __net_exit ip6table_filter_net_exit(struct net *net)
678280aa61SAlexey Dobriyan {
686c071754SFlorian Westphal 	ip6t_unregister_table_exit(net, "filter");
698280aa61SAlexey Dobriyan }
708280aa61SAlexey Dobriyan 
718280aa61SAlexey Dobriyan static struct pernet_operations ip6table_filter_net_ops = {
728280aa61SAlexey Dobriyan 	.init = ip6table_filter_net_init,
735f027bc7SDavid Wilder 	.pre_exit = ip6table_filter_net_pre_exit,
748280aa61SAlexey Dobriyan 	.exit = ip6table_filter_net_exit,
758280aa61SAlexey Dobriyan };
768280aa61SAlexey Dobriyan 
ip6table_filter_init(void)7765b4b4e8SAndrew Morton static int __init ip6table_filter_init(void)
781da177e4SLinus Torvalds {
79fdacd57cSFlorian Westphal 	int ret = xt_register_template(&packet_filter,
80fdacd57cSFlorian Westphal 					ip6table_filter_table_init);
81fdacd57cSFlorian Westphal 
82fdacd57cSFlorian Westphal 	if (ret < 0)
83fdacd57cSFlorian Westphal 		return ret;
841da177e4SLinus Torvalds 
85*44b5990eSFlorian Westphal 	filter_ops = xt_hook_ops_alloc(&packet_filter, ip6t_do_table);
86fdacd57cSFlorian Westphal 	if (IS_ERR(filter_ops)) {
87fdacd57cSFlorian Westphal 		xt_unregister_template(&packet_filter);
88b9e69e12SFlorian Westphal 		return PTR_ERR(filter_ops);
89fdacd57cSFlorian Westphal 	}
90b9e69e12SFlorian Westphal 
918280aa61SAlexey Dobriyan 	ret = register_pernet_subsys(&ip6table_filter_net_ops);
92fdacd57cSFlorian Westphal 	if (ret < 0) {
93fdacd57cSFlorian Westphal 		xt_unregister_template(&packet_filter);
94b9e69e12SFlorian Westphal 		kfree(filter_ops);
95fdacd57cSFlorian Westphal 		return ret;
96fdacd57cSFlorian Westphal 	}
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	return ret;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
ip6table_filter_fini(void)10165b4b4e8SAndrew Morton static void __exit ip6table_filter_fini(void)
1021da177e4SLinus Torvalds {
1038280aa61SAlexey Dobriyan 	unregister_pernet_subsys(&ip6table_filter_net_ops);
104fdacd57cSFlorian Westphal 	xt_unregister_template(&packet_filter);
105b9e69e12SFlorian Westphal 	kfree(filter_ops);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
10865b4b4e8SAndrew Morton module_init(ip6table_filter_init);
10965b4b4e8SAndrew Morton module_exit(ip6table_filter_fini);
110