[NETFILTER]: x_tables: return new table from {arp,ip,ip6}t_register_table()
[linux-2.6] / net / ipv4 / netfilter / iptable_mangle.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 #include <linux/module.h>
12 #include <linux/netfilter_ipv4/ip_tables.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
15 #include <net/sock.h>
16 #include <net/route.h>
17 #include <linux/ip.h>
18 #include <net/ip.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
22 MODULE_DESCRIPTION("iptables mangle table");
23
24 #define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
25                             (1 << NF_INET_LOCAL_IN) | \
26                             (1 << NF_INET_FORWARD) | \
27                             (1 << NF_INET_LOCAL_OUT) | \
28                             (1 << NF_INET_POST_ROUTING))
29
30 /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
31 static struct
32 {
33         struct ipt_replace repl;
34         struct ipt_standard entries[5];
35         struct ipt_error term;
36 } initial_table __initdata = {
37         .repl = {
38                 .name = "mangle",
39                 .valid_hooks = MANGLE_VALID_HOOKS,
40                 .num_entries = 6,
41                 .size = sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
42                 .hook_entry = {
43                         [NF_INET_PRE_ROUTING]   = 0,
44                         [NF_INET_LOCAL_IN]      = sizeof(struct ipt_standard),
45                         [NF_INET_FORWARD]       = sizeof(struct ipt_standard) * 2,
46                         [NF_INET_LOCAL_OUT]     = sizeof(struct ipt_standard) * 3,
47                         [NF_INET_POST_ROUTING]  = sizeof(struct ipt_standard) * 4,
48                 },
49                 .underflow = {
50                         [NF_INET_PRE_ROUTING]   = 0,
51                         [NF_INET_LOCAL_IN]      = sizeof(struct ipt_standard),
52                         [NF_INET_FORWARD]       = sizeof(struct ipt_standard) * 2,
53                         [NF_INET_LOCAL_OUT]     = sizeof(struct ipt_standard) * 3,
54                         [NF_INET_POST_ROUTING]  = sizeof(struct ipt_standard) * 4,
55                 },
56         },
57         .entries = {
58                 IPT_STANDARD_INIT(NF_ACCEPT),   /* PRE_ROUTING */
59                 IPT_STANDARD_INIT(NF_ACCEPT),   /* LOCAL_IN */
60                 IPT_STANDARD_INIT(NF_ACCEPT),   /* FORWARD */
61                 IPT_STANDARD_INIT(NF_ACCEPT),   /* LOCAL_OUT */
62                 IPT_STANDARD_INIT(NF_ACCEPT),   /* POST_ROUTING */
63         },
64         .term = IPT_ERROR_INIT,                 /* ERROR */
65 };
66
67 static struct xt_table __packet_mangler = {
68         .name           = "mangle",
69         .valid_hooks    = MANGLE_VALID_HOOKS,
70         .lock           = RW_LOCK_UNLOCKED,
71         .me             = THIS_MODULE,
72         .af             = AF_INET,
73 };
74 static struct xt_table *packet_mangler;
75
76 /* The work comes in here from netfilter.c. */
77 static unsigned int
78 ipt_route_hook(unsigned int hook,
79          struct sk_buff *skb,
80          const struct net_device *in,
81          const struct net_device *out,
82          int (*okfn)(struct sk_buff *))
83 {
84         return ipt_do_table(skb, hook, in, out, packet_mangler);
85 }
86
87 static unsigned int
88 ipt_local_hook(unsigned int hook,
89                    struct sk_buff *skb,
90                    const struct net_device *in,
91                    const struct net_device *out,
92                    int (*okfn)(struct sk_buff *))
93 {
94         unsigned int ret;
95         const struct iphdr *iph;
96         u_int8_t tos;
97         __be32 saddr, daddr;
98         u_int32_t mark;
99
100         /* root is playing with raw sockets. */
101         if (skb->len < sizeof(struct iphdr)
102             || ip_hdrlen(skb) < sizeof(struct iphdr)) {
103                 if (net_ratelimit())
104                         printk("iptable_mangle: ignoring short SOCK_RAW "
105                                "packet.\n");
106                 return NF_ACCEPT;
107         }
108
109         /* Save things which could affect route */
110         mark = skb->mark;
111         iph = ip_hdr(skb);
112         saddr = iph->saddr;
113         daddr = iph->daddr;
114         tos = iph->tos;
115
116         ret = ipt_do_table(skb, hook, in, out, packet_mangler);
117         /* Reroute for ANY change. */
118         if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
119                 iph = ip_hdr(skb);
120
121                 if (iph->saddr != saddr ||
122                     iph->daddr != daddr ||
123                     skb->mark != mark ||
124                     iph->tos != tos)
125                         if (ip_route_me_harder(skb, RTN_UNSPEC))
126                                 ret = NF_DROP;
127         }
128
129         return ret;
130 }
131
132 static struct nf_hook_ops ipt_ops[] __read_mostly = {
133         {
134                 .hook           = ipt_route_hook,
135                 .owner          = THIS_MODULE,
136                 .pf             = PF_INET,
137                 .hooknum        = NF_INET_PRE_ROUTING,
138                 .priority       = NF_IP_PRI_MANGLE,
139         },
140         {
141                 .hook           = ipt_route_hook,
142                 .owner          = THIS_MODULE,
143                 .pf             = PF_INET,
144                 .hooknum        = NF_INET_LOCAL_IN,
145                 .priority       = NF_IP_PRI_MANGLE,
146         },
147         {
148                 .hook           = ipt_route_hook,
149                 .owner          = THIS_MODULE,
150                 .pf             = PF_INET,
151                 .hooknum        = NF_INET_FORWARD,
152                 .priority       = NF_IP_PRI_MANGLE,
153         },
154         {
155                 .hook           = ipt_local_hook,
156                 .owner          = THIS_MODULE,
157                 .pf             = PF_INET,
158                 .hooknum        = NF_INET_LOCAL_OUT,
159                 .priority       = NF_IP_PRI_MANGLE,
160         },
161         {
162                 .hook           = ipt_route_hook,
163                 .owner          = THIS_MODULE,
164                 .pf             = PF_INET,
165                 .hooknum        = NF_INET_POST_ROUTING,
166                 .priority       = NF_IP_PRI_MANGLE,
167         },
168 };
169
170 static int __init iptable_mangle_init(void)
171 {
172         int ret;
173
174         /* Register table */
175         packet_mangler = ipt_register_table(&init_net, &__packet_mangler,
176                                             &initial_table.repl);
177         if (IS_ERR(packet_mangler))
178                 return PTR_ERR(packet_mangler);
179
180         /* Register hooks */
181         ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
182         if (ret < 0)
183                 goto cleanup_table;
184
185         return ret;
186
187  cleanup_table:
188         ipt_unregister_table(packet_mangler);
189         return ret;
190 }
191
192 static void __exit iptable_mangle_fini(void)
193 {
194         nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
195         ipt_unregister_table(packet_mangler);
196 }
197
198 module_init(iptable_mangle_init);
199 module_exit(iptable_mangle_fini);