iptable_filter.c revision 9335f047fe61587ec82ff12fbb1220bcfdd32006
1/*
2 * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
3 *
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/netfilter_ipv4/ip_tables.h>
16#include <net/ip.h>
17
18MODULE_LICENSE("GPL");
19MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
20MODULE_DESCRIPTION("iptables filter table");
21
22#define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
23			    (1 << NF_INET_FORWARD) | \
24			    (1 << NF_INET_LOCAL_OUT))
25
26static struct
27{
28	struct ipt_replace repl;
29	struct ipt_standard entries[3];
30	struct ipt_error term;
31} initial_table __net_initdata = {
32	.repl = {
33		.name = "filter",
34		.valid_hooks = FILTER_VALID_HOOKS,
35		.num_entries = 4,
36		.size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
37		.hook_entry = {
38			[NF_INET_LOCAL_IN] = 0,
39			[NF_INET_FORWARD] = sizeof(struct ipt_standard),
40			[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
41		},
42		.underflow = {
43			[NF_INET_LOCAL_IN] = 0,
44			[NF_INET_FORWARD] = sizeof(struct ipt_standard),
45			[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
46		},
47	},
48	.entries = {
49		IPT_STANDARD_INIT(NF_ACCEPT),	/* LOCAL_IN */
50		IPT_STANDARD_INIT(NF_ACCEPT),	/* FORWARD */
51		IPT_STANDARD_INIT(NF_ACCEPT),	/* LOCAL_OUT */
52	},
53	.term = IPT_ERROR_INIT,			/* ERROR */
54};
55
56static struct xt_table packet_filter = {
57	.name		= "filter",
58	.valid_hooks	= FILTER_VALID_HOOKS,
59	.lock		= RW_LOCK_UNLOCKED,
60	.me		= THIS_MODULE,
61	.af		= AF_INET,
62};
63
64/* The work comes in here from netfilter.c. */
65static unsigned int
66ipt_hook(unsigned int hook,
67	 struct sk_buff *skb,
68	 const struct net_device *in,
69	 const struct net_device *out,
70	 int (*okfn)(struct sk_buff *))
71{
72	return ipt_do_table(skb, hook, in, out, init_net.ipv4.iptable_filter);
73}
74
75static unsigned int
76ipt_local_out_hook(unsigned int hook,
77		   struct sk_buff *skb,
78		   const struct net_device *in,
79		   const struct net_device *out,
80		   int (*okfn)(struct sk_buff *))
81{
82	/* root is playing with raw sockets. */
83	if (skb->len < sizeof(struct iphdr) ||
84	    ip_hdrlen(skb) < sizeof(struct iphdr)) {
85		if (net_ratelimit())
86			printk("iptable_filter: ignoring short SOCK_RAW "
87			       "packet.\n");
88		return NF_ACCEPT;
89	}
90
91	return ipt_do_table(skb, hook, in, out, init_net.ipv4.iptable_filter);
92}
93
94static struct nf_hook_ops ipt_ops[] __read_mostly = {
95	{
96		.hook		= ipt_hook,
97		.owner		= THIS_MODULE,
98		.pf		= PF_INET,
99		.hooknum	= NF_INET_LOCAL_IN,
100		.priority	= NF_IP_PRI_FILTER,
101	},
102	{
103		.hook		= ipt_hook,
104		.owner		= THIS_MODULE,
105		.pf		= PF_INET,
106		.hooknum	= NF_INET_FORWARD,
107		.priority	= NF_IP_PRI_FILTER,
108	},
109	{
110		.hook		= ipt_local_out_hook,
111		.owner		= THIS_MODULE,
112		.pf		= PF_INET,
113		.hooknum	= NF_INET_LOCAL_OUT,
114		.priority	= NF_IP_PRI_FILTER,
115	},
116};
117
118/* Default to forward because I got too much mail already. */
119static int forward = NF_ACCEPT;
120module_param(forward, bool, 0000);
121
122static int __net_init iptable_filter_net_init(struct net *net)
123{
124	/* Register table */
125	net->ipv4.iptable_filter =
126		ipt_register_table(net, &packet_filter, &initial_table.repl);
127	if (IS_ERR(net->ipv4.iptable_filter))
128		return PTR_ERR(net->ipv4.iptable_filter);
129	return 0;
130}
131
132static void __net_exit iptable_filter_net_exit(struct net *net)
133{
134	ipt_unregister_table(net->ipv4.iptable_filter);
135}
136
137static struct pernet_operations iptable_filter_net_ops = {
138	.init = iptable_filter_net_init,
139	.exit = iptable_filter_net_exit,
140};
141
142static int __init iptable_filter_init(void)
143{
144	int ret;
145
146	if (forward < 0 || forward > NF_MAX_VERDICT) {
147		printk("iptables forward must be 0 or 1\n");
148		return -EINVAL;
149	}
150
151	/* Entry 1 is the FORWARD hook */
152	initial_table.entries[1].target.verdict = -forward - 1;
153
154	ret = register_pernet_subsys(&iptable_filter_net_ops);
155	if (ret < 0)
156		return ret;
157
158	/* Register hooks */
159	ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
160	if (ret < 0)
161		goto cleanup_table;
162
163	return ret;
164
165 cleanup_table:
166	unregister_pernet_subsys(&iptable_filter_net_ops);
167	return ret;
168}
169
170static void __exit iptable_filter_fini(void)
171{
172	nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
173	unregister_pernet_subsys(&iptable_filter_net_ops);
174}
175
176module_init(iptable_filter_init);
177module_exit(iptable_filter_fini);
178