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 const struct xt_table packet_filter = { |
23 | .name = "filter" , |
24 | .valid_hooks = FILTER_VALID_HOOKS, |
25 | .me = THIS_MODULE, |
26 | .af = NFPROTO_IPV6, |
27 | .priority = NF_IP6_PRI_FILTER, |
28 | }; |
29 | |
30 | static struct nf_hook_ops *filter_ops __read_mostly; |
31 | |
32 | /* Default to forward because I got too much mail already. */ |
33 | static bool forward = true; |
34 | module_param(forward, bool, 0000); |
35 | |
36 | static int ip6table_filter_table_init(struct net *net) |
37 | { |
38 | struct ip6t_replace *repl; |
39 | int err; |
40 | |
41 | repl = ip6t_alloc_initial_table(&packet_filter); |
42 | if (repl == NULL) |
43 | return -ENOMEM; |
44 | /* Entry 1 is the FORWARD hook */ |
45 | ((struct ip6t_standard *)repl->entries)[1].target.verdict = |
46 | forward ? -NF_ACCEPT - 1 : -NF_DROP - 1; |
47 | |
48 | err = ip6t_register_table(net, table: &packet_filter, repl, ops: filter_ops); |
49 | kfree(objp: repl); |
50 | return err; |
51 | } |
52 | |
53 | static int __net_init ip6table_filter_net_init(struct net *net) |
54 | { |
55 | if (!forward) |
56 | return ip6table_filter_table_init(net); |
57 | |
58 | return 0; |
59 | } |
60 | |
61 | static void __net_exit ip6table_filter_net_pre_exit(struct net *net) |
62 | { |
63 | ip6t_unregister_table_pre_exit(net, name: "filter" ); |
64 | } |
65 | |
66 | static void __net_exit ip6table_filter_net_exit(struct net *net) |
67 | { |
68 | ip6t_unregister_table_exit(net, name: "filter" ); |
69 | } |
70 | |
71 | static struct pernet_operations ip6table_filter_net_ops = { |
72 | .init = ip6table_filter_net_init, |
73 | .pre_exit = ip6table_filter_net_pre_exit, |
74 | .exit = ip6table_filter_net_exit, |
75 | }; |
76 | |
77 | static int __init ip6table_filter_init(void) |
78 | { |
79 | int ret = xt_register_template(t: &packet_filter, |
80 | table_init: ip6table_filter_table_init); |
81 | |
82 | if (ret < 0) |
83 | return ret; |
84 | |
85 | filter_ops = xt_hook_ops_alloc(&packet_filter, ip6t_do_table); |
86 | if (IS_ERR(ptr: filter_ops)) { |
87 | xt_unregister_template(t: &packet_filter); |
88 | return PTR_ERR(ptr: filter_ops); |
89 | } |
90 | |
91 | ret = register_pernet_subsys(&ip6table_filter_net_ops); |
92 | if (ret < 0) { |
93 | xt_unregister_template(t: &packet_filter); |
94 | kfree(objp: filter_ops); |
95 | return ret; |
96 | } |
97 | |
98 | return ret; |
99 | } |
100 | |
101 | static void __exit ip6table_filter_fini(void) |
102 | { |
103 | unregister_pernet_subsys(&ip6table_filter_net_ops); |
104 | xt_unregister_template(t: &packet_filter); |
105 | kfree(objp: filter_ops); |
106 | } |
107 | |
108 | module_init(ip6table_filter_init); |
109 | module_exit(ip6table_filter_fini); |
110 | |