[NETFILTER]: x_tables: return new table from {arp,ip,ip6}t_register_table()
[safe/jmp/linux-2.6] / net / ipv4 / netfilter / iptable_filter.c
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
18 MODULE_LICENSE("GPL");
19 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
20 MODULE_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
26 static struct
27 {
28         struct ipt_replace repl;
29         struct ipt_standard entries[3];
30         struct ipt_error term;
31 } initial_table __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
56 static 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 static struct xt_table *packet_filter;
64
65 /* The work comes in here from netfilter.c. */
66 static unsigned int
67 ipt_hook(unsigned int hook,
68          struct sk_buff *skb,
69          const struct net_device *in,
70          const struct net_device *out,
71          int (*okfn)(struct sk_buff *))
72 {
73         return ipt_do_table(skb, hook, in, out, packet_filter);
74 }
75
76 static unsigned int
77 ipt_local_out_hook(unsigned int hook,
78                    struct sk_buff *skb,
79                    const struct net_device *in,
80                    const struct net_device *out,
81                    int (*okfn)(struct sk_buff *))
82 {
83         /* root is playing with raw sockets. */
84         if (skb->len < sizeof(struct iphdr) ||
85             ip_hdrlen(skb) < sizeof(struct iphdr)) {
86                 if (net_ratelimit())
87                         printk("iptable_filter: ignoring short SOCK_RAW "
88                                "packet.\n");
89                 return NF_ACCEPT;
90         }
91
92         return ipt_do_table(skb, hook, in, out, packet_filter);
93 }
94
95 static struct nf_hook_ops ipt_ops[] __read_mostly = {
96         {
97                 .hook           = ipt_hook,
98                 .owner          = THIS_MODULE,
99                 .pf             = PF_INET,
100                 .hooknum        = NF_INET_LOCAL_IN,
101                 .priority       = NF_IP_PRI_FILTER,
102         },
103         {
104                 .hook           = ipt_hook,
105                 .owner          = THIS_MODULE,
106                 .pf             = PF_INET,
107                 .hooknum        = NF_INET_FORWARD,
108                 .priority       = NF_IP_PRI_FILTER,
109         },
110         {
111                 .hook           = ipt_local_out_hook,
112                 .owner          = THIS_MODULE,
113                 .pf             = PF_INET,
114                 .hooknum        = NF_INET_LOCAL_OUT,
115                 .priority       = NF_IP_PRI_FILTER,
116         },
117 };
118
119 /* Default to forward because I got too much mail already. */
120 static int forward = NF_ACCEPT;
121 module_param(forward, bool, 0000);
122
123 static int __init iptable_filter_init(void)
124 {
125         int ret;
126
127         if (forward < 0 || forward > NF_MAX_VERDICT) {
128                 printk("iptables forward must be 0 or 1\n");
129                 return -EINVAL;
130         }
131
132         /* Entry 1 is the FORWARD hook */
133         initial_table.entries[1].target.verdict = -forward - 1;
134
135         /* Register table */
136         packet_filter = ipt_register_table(&init_net, &__packet_filter,
137                                            &initial_table.repl);
138         if (IS_ERR(packet_filter))
139                 return PTR_ERR(packet_filter);
140
141         /* Register hooks */
142         ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
143         if (ret < 0)
144                 goto cleanup_table;
145
146         return ret;
147
148  cleanup_table:
149         ipt_unregister_table(packet_filter);
150         return ret;
151 }
152
153 static void __exit iptable_filter_fini(void)
154 {
155         nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
156         ipt_unregister_table(packet_filter);
157 }
158
159 module_init(iptable_filter_init);
160 module_exit(iptable_filter_fini);