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_ipv4/ip_tables.h>
12 #include <linux/slab.h>
13 #include <net/ip.h>
14
15 MODULE_LICENSE("GPL");
16 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
17 MODULE_DESCRIPTION("iptables filter table");
18
19 #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
20 (1 << NF_INET_FORWARD) | \
21 (1 << NF_INET_LOCAL_OUT))
22
23 static const struct xt_table packet_filter = {
24 .name = "filter",
25 .valid_hooks = FILTER_VALID_HOOKS,
26 .me = THIS_MODULE,
27 .af = NFPROTO_IPV4,
28 .priority = NF_IP_PRI_FILTER,
29 };
30
31 static struct nf_hook_ops *filter_ops __read_mostly;
32
33 /* Default to forward because I got too much mail already. */
34 static bool forward __read_mostly = true;
35 module_param(forward, bool, 0000);
36
iptable_filter_table_init(struct net * net)37 static int iptable_filter_table_init(struct net *net)
38 {
39 struct ipt_replace *repl;
40 int err;
41
42 repl = ipt_alloc_initial_table(&packet_filter);
43 if (repl == NULL)
44 return -ENOMEM;
45 /* Entry 1 is the FORWARD hook */
46 ((struct ipt_standard *)repl->entries)[1].target.verdict =
47 forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
48
49 err = ipt_register_table(net, &packet_filter, repl, filter_ops);
50 kfree(repl);
51 return err;
52 }
53
iptable_filter_net_init(struct net * net)54 static int __net_init iptable_filter_net_init(struct net *net)
55 {
56 if (!forward)
57 return iptable_filter_table_init(net);
58
59 return 0;
60 }
61
iptable_filter_net_pre_exit(struct net * net)62 static void __net_exit iptable_filter_net_pre_exit(struct net *net)
63 {
64 ipt_unregister_table_pre_exit(net, "filter");
65 }
66
iptable_filter_net_exit(struct net * net)67 static void __net_exit iptable_filter_net_exit(struct net *net)
68 {
69 ipt_unregister_table_exit(net, "filter");
70 }
71
72 static struct pernet_operations iptable_filter_net_ops = {
73 .init = iptable_filter_net_init,
74 .pre_exit = iptable_filter_net_pre_exit,
75 .exit = iptable_filter_net_exit,
76 };
77
iptable_filter_init(void)78 static int __init iptable_filter_init(void)
79 {
80 int ret = xt_register_template(&packet_filter,
81 iptable_filter_table_init);
82
83 if (ret < 0)
84 return ret;
85
86 filter_ops = xt_hook_ops_alloc(&packet_filter, ipt_do_table);
87 if (IS_ERR(filter_ops)) {
88 xt_unregister_template(&packet_filter);
89 return PTR_ERR(filter_ops);
90 }
91
92 ret = register_pernet_subsys(&iptable_filter_net_ops);
93 if (ret < 0) {
94 xt_unregister_template(&packet_filter);
95 kfree(filter_ops);
96 return ret;
97 }
98
99 return 0;
100 }
101
iptable_filter_fini(void)102 static void __exit iptable_filter_fini(void)
103 {
104 unregister_pernet_subsys(&iptable_filter_net_ops);
105 xt_unregister_template(&packet_filter);
106 kfree(filter_ops);
107 }
108
109 module_init(iptable_filter_init);
110 module_exit(iptable_filter_fini);
111