[NETFILTER]: annotate xtables targets with const and remove casts
[deliverable/linux.git] / net / ipv4 / netfilter / iptable_filter.c
CommitLineData
1da177e4
LT
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>
c9bdd4b5 16#include <net/ip.h>
1da177e4
LT
17
18MODULE_LICENSE("GPL");
19MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
20MODULE_DESCRIPTION("iptables filter table");
21
6e23ae2a
PM
22#define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
23 (1 << NF_INET_FORWARD) | \
24 (1 << NF_INET_LOCAL_OUT))
1da177e4
LT
25
26static struct
27{
28 struct ipt_replace repl;
29 struct ipt_standard entries[3];
30 struct ipt_error term;
9335f047 31} initial_table __net_initdata = {
3c2ad469
PM
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 = {
6e23ae2a
PM
38 [NF_INET_LOCAL_IN] = 0,
39 [NF_INET_FORWARD] = sizeof(struct ipt_standard),
40 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
3c2ad469
PM
41 },
42 .underflow = {
6e23ae2a
PM
43 [NF_INET_LOCAL_IN] = 0,
44 [NF_INET_FORWARD] = sizeof(struct ipt_standard),
45 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
3c2ad469
PM
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 */
1da177e4
LT
54};
55
9335f047 56static struct xt_table packet_filter = {
1da177e4
LT
57 .name = "filter",
58 .valid_hooks = FILTER_VALID_HOOKS,
fdccecd0 59 .lock = __RW_LOCK_UNLOCKED(packet_filter.lock),
2e4e6a17
HW
60 .me = THIS_MODULE,
61 .af = AF_INET,
1da177e4
LT
62};
63
64/* The work comes in here from netfilter.c. */
666953df
AD
65static unsigned int
66ipt_local_in_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,
73 nf_local_in_net(in, out)->ipv4.iptable_filter);
74}
75
1da177e4
LT
76static unsigned int
77ipt_hook(unsigned int hook,
3db05fea 78 struct sk_buff *skb,
1da177e4
LT
79 const struct net_device *in,
80 const struct net_device *out,
81 int (*okfn)(struct sk_buff *))
82{
666953df
AD
83 return ipt_do_table(skb, hook, in, out,
84 nf_forward_net(in, out)->ipv4.iptable_filter);
1da177e4
LT
85}
86
87static unsigned int
88ipt_local_out_hook(unsigned int hook,
3db05fea 89 struct sk_buff *skb,
1da177e4
LT
90 const struct net_device *in,
91 const struct net_device *out,
92 int (*okfn)(struct sk_buff *))
93{
94 /* root is playing with raw sockets. */
3db05fea
HX
95 if (skb->len < sizeof(struct iphdr) ||
96 ip_hdrlen(skb) < sizeof(struct iphdr)) {
1da177e4 97 if (net_ratelimit())
4a176c1a
PM
98 printk("iptable_filter: ignoring short SOCK_RAW "
99 "packet.\n");
1da177e4
LT
100 return NF_ACCEPT;
101 }
102
666953df
AD
103 return ipt_do_table(skb, hook, in, out,
104 nf_local_out_net(in, out)->ipv4.iptable_filter);
1da177e4
LT
105}
106
1999414a 107static struct nf_hook_ops ipt_ops[] __read_mostly = {
1da177e4 108 {
666953df 109 .hook = ipt_local_in_hook,
1da177e4
LT
110 .owner = THIS_MODULE,
111 .pf = PF_INET,
6e23ae2a 112 .hooknum = NF_INET_LOCAL_IN,
1da177e4
LT
113 .priority = NF_IP_PRI_FILTER,
114 },
115 {
116 .hook = ipt_hook,
117 .owner = THIS_MODULE,
118 .pf = PF_INET,
6e23ae2a 119 .hooknum = NF_INET_FORWARD,
1da177e4
LT
120 .priority = NF_IP_PRI_FILTER,
121 },
122 {
123 .hook = ipt_local_out_hook,
124 .owner = THIS_MODULE,
125 .pf = PF_INET,
6e23ae2a 126 .hooknum = NF_INET_LOCAL_OUT,
1da177e4
LT
127 .priority = NF_IP_PRI_FILTER,
128 },
129};
130
131/* Default to forward because I got too much mail already. */
132static int forward = NF_ACCEPT;
133module_param(forward, bool, 0000);
134
9335f047
AD
135static int __net_init iptable_filter_net_init(struct net *net)
136{
137 /* Register table */
138 net->ipv4.iptable_filter =
139 ipt_register_table(net, &packet_filter, &initial_table.repl);
140 if (IS_ERR(net->ipv4.iptable_filter))
141 return PTR_ERR(net->ipv4.iptable_filter);
142 return 0;
143}
144
145static void __net_exit iptable_filter_net_exit(struct net *net)
146{
147 ipt_unregister_table(net->ipv4.iptable_filter);
148}
149
150static struct pernet_operations iptable_filter_net_ops = {
151 .init = iptable_filter_net_init,
152 .exit = iptable_filter_net_exit,
153};
154
65b4b4e8 155static int __init iptable_filter_init(void)
1da177e4
LT
156{
157 int ret;
158
159 if (forward < 0 || forward > NF_MAX_VERDICT) {
160 printk("iptables forward must be 0 or 1\n");
161 return -EINVAL;
162 }
163
164 /* Entry 1 is the FORWARD hook */
165 initial_table.entries[1].target.verdict = -forward - 1;
166
9335f047
AD
167 ret = register_pernet_subsys(&iptable_filter_net_ops);
168 if (ret < 0)
169 return ret;
1da177e4
LT
170
171 /* Register hooks */
964ddaa1 172 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
1da177e4
LT
173 if (ret < 0)
174 goto cleanup_table;
175
1da177e4
LT
176 return ret;
177
1da177e4 178 cleanup_table:
9335f047 179 unregister_pernet_subsys(&iptable_filter_net_ops);
1da177e4
LT
180 return ret;
181}
182
65b4b4e8 183static void __exit iptable_filter_fini(void)
1da177e4 184{
964ddaa1 185 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
9335f047 186 unregister_pernet_subsys(&iptable_filter_net_ops);
1da177e4
LT
187}
188
65b4b4e8
AM
189module_init(iptable_filter_init);
190module_exit(iptable_filter_fini);
This page took 0.384209 seconds and 5 git commands to generate.