1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
4  *
5  * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
6  * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/netfilter_ipv6/ip6_tables.h>
12 #include <linux/slab.h>
13 
14 MODULE_LICENSE("GPL");
15 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
16 MODULE_DESCRIPTION("ip6tables filter table");
17 
18 #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
19 			    (1 << NF_INET_FORWARD) | \
20 			    (1 << NF_INET_LOCAL_OUT))
21 
22 static int __net_init ip6table_filter_table_init(struct net *net);
23 
24 static const struct xt_table packet_filter = {
25 	.name		= "filter",
26 	.valid_hooks	= FILTER_VALID_HOOKS,
27 	.me		= THIS_MODULE,
28 	.af		= NFPROTO_IPV6,
29 	.priority	= NF_IP6_PRI_FILTER,
30 	.table_init	= ip6table_filter_table_init,
31 };
32 
33 /* The work comes in here from netfilter.c. */
34 static unsigned int
35 ip6table_filter_hook(void *priv, struct sk_buff *skb,
36 		     const struct nf_hook_state *state)
37 {
38 	return ip6t_do_table(skb, state, state->net->ipv6.ip6table_filter);
39 }
40 
41 static struct nf_hook_ops *filter_ops __read_mostly;
42 
43 /* Default to forward because I got too much mail already. */
44 static bool forward = true;
45 module_param(forward, bool, 0000);
46 
47 static int __net_init ip6table_filter_table_init(struct net *net)
48 {
49 	struct ip6t_replace *repl;
50 	int err;
51 
52 	if (net->ipv6.ip6table_filter)
53 		return 0;
54 
55 	repl = ip6t_alloc_initial_table(&packet_filter);
56 	if (repl == NULL)
57 		return -ENOMEM;
58 	/* Entry 1 is the FORWARD hook */
59 	((struct ip6t_standard *)repl->entries)[1].target.verdict =
60 		forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
61 
62 	err = ip6t_register_table(net, &packet_filter, repl, filter_ops,
63 				  &net->ipv6.ip6table_filter);
64 	kfree(repl);
65 	return err;
66 }
67 
68 static int __net_init ip6table_filter_net_init(struct net *net)
69 {
70 	if (net == &init_net || !forward)
71 		return ip6table_filter_table_init(net);
72 
73 	return 0;
74 }
75 
76 static void __net_exit ip6table_filter_net_pre_exit(struct net *net)
77 {
78 	if (net->ipv6.ip6table_filter)
79 		ip6t_unregister_table_pre_exit(net, net->ipv6.ip6table_filter,
80 					       filter_ops);
81 }
82 
83 static void __net_exit ip6table_filter_net_exit(struct net *net)
84 {
85 	if (!net->ipv6.ip6table_filter)
86 		return;
87 	ip6t_unregister_table_exit(net, net->ipv6.ip6table_filter);
88 	net->ipv6.ip6table_filter = NULL;
89 }
90 
91 static struct pernet_operations ip6table_filter_net_ops = {
92 	.init = ip6table_filter_net_init,
93 	.pre_exit = ip6table_filter_net_pre_exit,
94 	.exit = ip6table_filter_net_exit,
95 };
96 
97 static int __init ip6table_filter_init(void)
98 {
99 	int ret;
100 
101 	filter_ops = xt_hook_ops_alloc(&packet_filter, ip6table_filter_hook);
102 	if (IS_ERR(filter_ops))
103 		return PTR_ERR(filter_ops);
104 
105 	ret = register_pernet_subsys(&ip6table_filter_net_ops);
106 	if (ret < 0)
107 		kfree(filter_ops);
108 
109 	return ret;
110 }
111 
112 static void __exit ip6table_filter_fini(void)
113 {
114 	unregister_pernet_subsys(&ip6table_filter_net_ops);
115 	kfree(filter_ops);
116 }
117 
118 module_init(ip6table_filter_init);
119 module_exit(ip6table_filter_fini);
120