netfilter: xtables: fix mangle tables
[safe/jmp/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 static const struct xt_table packet_mangler = {
31         .name           = "mangle",
32         .valid_hooks    = MANGLE_VALID_HOOKS,
33         .me             = THIS_MODULE,
34         .af             = NFPROTO_IPV4,
35         .priority       = NF_IP_PRI_MANGLE,
36 };
37
38 static unsigned int
39 ipt_local_hook(unsigned int hook,
40                    struct sk_buff *skb,
41                    const struct net_device *in,
42                    const struct net_device *out,
43                    int (*okfn)(struct sk_buff *))
44 {
45         unsigned int ret;
46         const struct iphdr *iph;
47         u_int8_t tos;
48         __be32 saddr, daddr;
49         u_int32_t mark;
50
51         /* root is playing with raw sockets. */
52         if (skb->len < sizeof(struct iphdr) ||
53             ip_hdrlen(skb) < sizeof(struct iphdr))
54                 return NF_ACCEPT;
55
56         /* Save things which could affect route */
57         mark = skb->mark;
58         iph = ip_hdr(skb);
59         saddr = iph->saddr;
60         daddr = iph->daddr;
61         tos = iph->tos;
62
63         ret = ipt_do_table(skb, hook, in, out,
64                            dev_net(out)->ipv4.iptable_mangle);
65         /* Reroute for ANY change. */
66         if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
67                 iph = ip_hdr(skb);
68
69                 if (iph->saddr != saddr ||
70                     iph->daddr != daddr ||
71                     skb->mark != mark ||
72                     iph->tos != tos)
73                         if (ip_route_me_harder(skb, RTN_UNSPEC))
74                                 ret = NF_DROP;
75         }
76
77         return ret;
78 }
79
80 /* The work comes in here from netfilter.c. */
81 static unsigned int
82 iptable_mangle_hook(unsigned int hook,
83                      struct sk_buff *skb,
84                      const struct net_device *in,
85                      const struct net_device *out,
86                      int (*okfn)(struct sk_buff *))
87 {
88         if (hook == NF_INET_LOCAL_OUT)
89                 return ipt_local_hook(hook, skb, in, out, okfn);
90         if (hook == NF_INET_POST_ROUTING)
91                 return ipt_do_table(skb, hook, in, out,
92                                     dev_net(out)->ipv4.iptable_mangle);
93         /* PREROUTING/INPUT/FORWARD: */
94         return ipt_do_table(skb, hook, in, out,
95                             dev_net(in)->ipv4.iptable_mangle);
96 }
97
98 static struct nf_hook_ops *mangle_ops __read_mostly;
99
100 static int __net_init iptable_mangle_net_init(struct net *net)
101 {
102         struct ipt_replace *repl;
103
104         repl = ipt_alloc_initial_table(&packet_mangler);
105         if (repl == NULL)
106                 return -ENOMEM;
107         net->ipv4.iptable_mangle =
108                 ipt_register_table(net, &packet_mangler, repl);
109         kfree(repl);
110         if (IS_ERR(net->ipv4.iptable_mangle))
111                 return PTR_ERR(net->ipv4.iptable_mangle);
112         return 0;
113 }
114
115 static void __net_exit iptable_mangle_net_exit(struct net *net)
116 {
117         ipt_unregister_table(net, net->ipv4.iptable_mangle);
118 }
119
120 static struct pernet_operations iptable_mangle_net_ops = {
121         .init = iptable_mangle_net_init,
122         .exit = iptable_mangle_net_exit,
123 };
124
125 static int __init iptable_mangle_init(void)
126 {
127         int ret;
128
129         ret = register_pernet_subsys(&iptable_mangle_net_ops);
130         if (ret < 0)
131                 return ret;
132
133         /* Register hooks */
134         mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
135         if (IS_ERR(mangle_ops)) {
136                 ret = PTR_ERR(mangle_ops);
137                 goto cleanup_table;
138         }
139
140         return ret;
141
142  cleanup_table:
143         unregister_pernet_subsys(&iptable_mangle_net_ops);
144         return ret;
145 }
146
147 static void __exit iptable_mangle_fini(void)
148 {
149         xt_hook_unlink(&packet_mangler, mangle_ops);
150         unregister_pernet_subsys(&iptable_mangle_net_ops);
151 }
152
153 module_init(iptable_mangle_init);
154 module_exit(iptable_mangle_fini);