]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/netfilter/iptable_mangle.c
2355a229f8ee87535d88c3e40c9f9960ab3f6488
[net-next-2.6.git] / 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 const struct
32 {
33         struct ipt_replace repl;
34         struct ipt_standard entries[5];
35         struct ipt_error term;
36 } initial_table __net_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 const struct xt_table packet_mangler = {
68         .name           = "mangle",
69         .valid_hooks    = MANGLE_VALID_HOOKS,
70         .me             = THIS_MODULE,
71         .af             = NFPROTO_IPV4,
72         .priority       = NF_IP_PRI_MANGLE,
73 };
74
75 static unsigned int
76 ipt_local_hook(unsigned int hook,
77                    struct sk_buff *skb,
78                    const struct net_device *in,
79                    const struct net_device *out,
80                    int (*okfn)(struct sk_buff *))
81 {
82         unsigned int ret;
83         const struct iphdr *iph;
84         u_int8_t tos;
85         __be32 saddr, daddr;
86         u_int32_t mark;
87
88         /* root is playing with raw sockets. */
89         if (skb->len < sizeof(struct iphdr) ||
90             ip_hdrlen(skb) < sizeof(struct iphdr))
91                 return NF_ACCEPT;
92
93         /* Save things which could affect route */
94         mark = skb->mark;
95         iph = ip_hdr(skb);
96         saddr = iph->saddr;
97         daddr = iph->daddr;
98         tos = iph->tos;
99
100         ret = ipt_do_table(skb, hook, in, out,
101                            dev_net(out)->ipv4.iptable_mangle);
102         /* Reroute for ANY change. */
103         if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
104                 iph = ip_hdr(skb);
105
106                 if (iph->saddr != saddr ||
107                     iph->daddr != daddr ||
108                     skb->mark != mark ||
109                     iph->tos != tos)
110                         if (ip_route_me_harder(skb, RTN_UNSPEC))
111                                 ret = NF_DROP;
112         }
113
114         return ret;
115 }
116
117 /* The work comes in here from netfilter.c. */
118 static unsigned int
119 iptable_mangle_hook(unsigned int hook,
120                      struct sk_buff *skb,
121                      const struct net_device *in,
122                      const struct net_device *out,
123                      int (*okfn)(struct sk_buff *))
124 {
125         if (hook == NF_INET_LOCAL_OUT)
126                 return ipt_local_hook(hook, skb, in, out, okfn);
127
128         /* PREROUTING/INPUT/FORWARD: */
129         return ipt_do_table(skb, hook, in, out,
130                             dev_net(in)->ipv4.iptable_mangle);
131 }
132
133 static struct nf_hook_ops *mangle_ops __read_mostly;
134
135 static int __net_init iptable_mangle_net_init(struct net *net)
136 {
137         /* Register table */
138         net->ipv4.iptable_mangle =
139                 ipt_register_table(net, &packet_mangler, &initial_table.repl);
140         if (IS_ERR(net->ipv4.iptable_mangle))
141                 return PTR_ERR(net->ipv4.iptable_mangle);
142         return 0;
143 }
144
145 static void __net_exit iptable_mangle_net_exit(struct net *net)
146 {
147         ipt_unregister_table(net, net->ipv4.iptable_mangle);
148 }
149
150 static struct pernet_operations iptable_mangle_net_ops = {
151         .init = iptable_mangle_net_init,
152         .exit = iptable_mangle_net_exit,
153 };
154
155 static int __init iptable_mangle_init(void)
156 {
157         int ret;
158
159         ret = register_pernet_subsys(&iptable_mangle_net_ops);
160         if (ret < 0)
161                 return ret;
162
163         /* Register hooks */
164         mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
165         if (IS_ERR(mangle_ops)) {
166                 ret = PTR_ERR(mangle_ops);
167                 goto cleanup_table;
168         }
169
170         return ret;
171
172  cleanup_table:
173         unregister_pernet_subsys(&iptable_mangle_net_ops);
174         return ret;
175 }
176
177 static void __exit iptable_mangle_fini(void)
178 {
179         xt_hook_unlink(&packet_mangler, mangle_ops);
180         unregister_pernet_subsys(&iptable_mangle_net_ops);
181 }
182
183 module_init(iptable_mangle_init);
184 module_exit(iptable_mangle_fini);