netfilter: xtables: compact table hook functions (1/2)
[deliverable/linux.git] / net / ipv4 / netfilter / iptable_filter.c
... / ...
CommitLineData
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 const struct xt_table packet_filter = {
57 .name = "filter",
58 .valid_hooks = FILTER_VALID_HOOKS,
59 .me = THIS_MODULE,
60 .af = NFPROTO_IPV4,
61};
62
63static unsigned int
64iptable_filter_hook(unsigned int hook, struct sk_buff *skb,
65 const struct net_device *in, const struct net_device *out,
66 int (*okfn)(struct sk_buff *))
67{
68 if (hook == NF_INET_LOCAL_OUT) {
69 if (skb->len < sizeof(struct iphdr) ||
70 ip_hdrlen(skb) < sizeof(struct iphdr))
71 /* root is playing with raw sockets. */
72 return NF_ACCEPT;
73
74 return ipt_do_table(skb, hook, in, out,
75 dev_net(out)->ipv4.iptable_filter);
76 }
77
78 /* LOCAL_IN/FORWARD: */
79 return ipt_do_table(skb, hook, in, out,
80 dev_net(in)->ipv4.iptable_filter);
81}
82
83static struct nf_hook_ops ipt_ops[] __read_mostly = {
84 {
85 .hook = iptable_filter_hook,
86 .owner = THIS_MODULE,
87 .pf = NFPROTO_IPV4,
88 .hooknum = NF_INET_LOCAL_IN,
89 .priority = NF_IP_PRI_FILTER,
90 },
91 {
92 .hook = iptable_filter_hook,
93 .owner = THIS_MODULE,
94 .pf = NFPROTO_IPV4,
95 .hooknum = NF_INET_FORWARD,
96 .priority = NF_IP_PRI_FILTER,
97 },
98 {
99 .hook = iptable_filter_hook,
100 .owner = THIS_MODULE,
101 .pf = NFPROTO_IPV4,
102 .hooknum = NF_INET_LOCAL_OUT,
103 .priority = NF_IP_PRI_FILTER,
104 },
105};
106
107/* Default to forward because I got too much mail already. */
108static int forward = NF_ACCEPT;
109module_param(forward, bool, 0000);
110
111static int __net_init iptable_filter_net_init(struct net *net)
112{
113 /* Register table */
114 net->ipv4.iptable_filter =
115 ipt_register_table(net, &packet_filter, &initial_table.repl);
116 if (IS_ERR(net->ipv4.iptable_filter))
117 return PTR_ERR(net->ipv4.iptable_filter);
118 return 0;
119}
120
121static void __net_exit iptable_filter_net_exit(struct net *net)
122{
123 ipt_unregister_table(net, net->ipv4.iptable_filter);
124}
125
126static struct pernet_operations iptable_filter_net_ops = {
127 .init = iptable_filter_net_init,
128 .exit = iptable_filter_net_exit,
129};
130
131static int __init iptable_filter_init(void)
132{
133 int ret;
134
135 if (forward < 0 || forward > NF_MAX_VERDICT) {
136 printk("iptables forward must be 0 or 1\n");
137 return -EINVAL;
138 }
139
140 /* Entry 1 is the FORWARD hook */
141 initial_table.entries[1].target.verdict = -forward - 1;
142
143 ret = register_pernet_subsys(&iptable_filter_net_ops);
144 if (ret < 0)
145 return ret;
146
147 /* Register hooks */
148 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
149 if (ret < 0)
150 goto cleanup_table;
151
152 return ret;
153
154 cleanup_table:
155 unregister_pernet_subsys(&iptable_filter_net_ops);
156 return ret;
157}
158
159static void __exit iptable_filter_fini(void)
160{
161 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
162 unregister_pernet_subsys(&iptable_filter_net_ops);
163}
164
165module_init(iptable_filter_init);
166module_exit(iptable_filter_fini);
This page took 0.024398 seconds and 5 git commands to generate.