1 // SPDX-License-Identifier: GPL-2.0-only
3 * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
5 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
6 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/netfilter_ipv4/ip_tables.h>
12 #include <linux/slab.h>
15 MODULE_LICENSE("GPL");
16 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
17 MODULE_DESCRIPTION("iptables filter table");
19 #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
20 (1 << NF_INET_FORWARD) | \
21 (1 << NF_INET_LOCAL_OUT))
22 static int __net_init iptable_filter_table_init(struct net *net);
24 static const struct xt_table packet_filter = {
26 .valid_hooks = FILTER_VALID_HOOKS,
29 .priority = NF_IP_PRI_FILTER,
30 .table_init = iptable_filter_table_init,
34 iptable_filter_hook(void *priv, struct sk_buff *skb,
35 const struct nf_hook_state *state)
37 return ipt_do_table(skb, state, state->net->ipv4.iptable_filter);
40 static struct nf_hook_ops *filter_ops __read_mostly;
42 /* Default to forward because I got too much mail already. */
43 static bool forward __read_mostly = true;
44 module_param(forward, bool, 0000);
46 static int __net_init iptable_filter_table_init(struct net *net)
48 struct ipt_replace *repl;
51 if (net->ipv4.iptable_filter)
54 repl = ipt_alloc_initial_table(&packet_filter);
57 /* Entry 1 is the FORWARD hook */
58 ((struct ipt_standard *)repl->entries)[1].target.verdict =
59 forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
61 err = ipt_register_table(net, &packet_filter, repl, filter_ops,
62 &net->ipv4.iptable_filter);
67 static int __net_init iptable_filter_net_init(struct net *net)
69 if (net == &init_net || !forward)
70 return iptable_filter_table_init(net);
75 static void __net_exit iptable_filter_net_exit(struct net *net)
77 if (!net->ipv4.iptable_filter)
79 ipt_unregister_table(net, net->ipv4.iptable_filter, filter_ops);
80 net->ipv4.iptable_filter = NULL;
83 static struct pernet_operations iptable_filter_net_ops = {
84 .init = iptable_filter_net_init,
85 .exit = iptable_filter_net_exit,
88 static int __init iptable_filter_init(void)
92 filter_ops = xt_hook_ops_alloc(&packet_filter, iptable_filter_hook);
93 if (IS_ERR(filter_ops))
94 return PTR_ERR(filter_ops);
96 ret = register_pernet_subsys(&iptable_filter_net_ops);
103 static void __exit iptable_filter_fini(void)
105 unregister_pernet_subsys(&iptable_filter_net_ops);
109 module_init(iptable_filter_init);
110 module_exit(iptable_filter_fini);