blob: a532e4d843326c33babab65e949cd03741e8bfe4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 * Extended to all five netfilter hooks by Brad Chapman & Harald Welte
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/netfilter_ipv4/ip_tables.h>
15#include <linux/netdevice.h>
16#include <linux/skbuff.h>
17#include <net/sock.h>
18#include <net/route.h>
19#include <linux/ip.h>
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
23MODULE_DESCRIPTION("iptables mangle table");
24
25#define MANGLE_VALID_HOOKS ((1 << NF_IP_PRE_ROUTING) | \
26 (1 << NF_IP_LOCAL_IN) | \
27 (1 << NF_IP_FORWARD) | \
28 (1 << NF_IP_LOCAL_OUT) | \
29 (1 << NF_IP_POST_ROUTING))
30
31/* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
32static struct
33{
34 struct ipt_replace repl;
35 struct ipt_standard entries[5];
36 struct ipt_error term;
37} initial_table __initdata
38= { { "mangle", MANGLE_VALID_HOOKS, 6,
39 sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
40 { [NF_IP_PRE_ROUTING] = 0,
41 [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard),
42 [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2,
43 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
44 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4 },
45 { [NF_IP_PRE_ROUTING] = 0,
46 [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard),
47 [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2,
48 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
49 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4 },
50 0, NULL, { } },
51 {
52 /* PRE_ROUTING */
53 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
54 0,
55 sizeof(struct ipt_entry),
56 sizeof(struct ipt_standard),
57 0, { 0, 0 }, { } },
58 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
59 -NF_ACCEPT - 1 } },
60 /* LOCAL_IN */
61 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
62 0,
63 sizeof(struct ipt_entry),
64 sizeof(struct ipt_standard),
65 0, { 0, 0 }, { } },
66 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
67 -NF_ACCEPT - 1 } },
68 /* FORWARD */
69 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
70 0,
71 sizeof(struct ipt_entry),
72 sizeof(struct ipt_standard),
73 0, { 0, 0 }, { } },
74 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
75 -NF_ACCEPT - 1 } },
76 /* LOCAL_OUT */
77 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
78 0,
79 sizeof(struct ipt_entry),
80 sizeof(struct ipt_standard),
81 0, { 0, 0 }, { } },
82 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
83 -NF_ACCEPT - 1 } },
84 /* POST_ROUTING */
85 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
86 0,
87 sizeof(struct ipt_entry),
88 sizeof(struct ipt_standard),
89 0, { 0, 0 }, { } },
90 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
91 -NF_ACCEPT - 1 } },
92 },
93 /* ERROR */
94 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
95 0,
96 sizeof(struct ipt_entry),
97 sizeof(struct ipt_error),
98 0, { 0, 0 }, { } },
99 { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
100 { } },
101 "ERROR"
102 }
103 }
104};
105
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800106static struct xt_table packet_mangler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .name = "mangle",
108 .valid_hooks = MANGLE_VALID_HOOKS,
109 .lock = RW_LOCK_UNLOCKED,
110 .me = THIS_MODULE,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800111 .af = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
114/* The work comes in here from netfilter.c. */
115static unsigned int
116ipt_route_hook(unsigned int hook,
117 struct sk_buff **pskb,
118 const struct net_device *in,
119 const struct net_device *out,
120 int (*okfn)(struct sk_buff *))
121{
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700122 return ipt_do_table(pskb, hook, in, out, &packet_mangler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static unsigned int
126ipt_local_hook(unsigned int hook,
127 struct sk_buff **pskb,
128 const struct net_device *in,
129 const struct net_device *out,
130 int (*okfn)(struct sk_buff *))
131{
132 unsigned int ret;
133 u_int8_t tos;
Al Viro6a19d612006-09-28 14:22:24 -0700134 __be32 saddr, daddr;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800135 u_int32_t mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /* root is playing with raw sockets. */
138 if ((*pskb)->len < sizeof(struct iphdr)
139 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
140 if (net_ratelimit())
141 printk("ipt_hook: happy cracking.\n");
142 return NF_ACCEPT;
143 }
144
145 /* Save things which could affect route */
Thomas Graf82e91ff2006-11-09 15:19:14 -0800146 mark = (*pskb)->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 saddr = (*pskb)->nh.iph->saddr;
148 daddr = (*pskb)->nh.iph->daddr;
149 tos = (*pskb)->nh.iph->tos;
150
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700151 ret = ipt_do_table(pskb, hook, in, out, &packet_mangler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /* Reroute for ANY change. */
153 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE
154 && ((*pskb)->nh.iph->saddr != saddr
155 || (*pskb)->nh.iph->daddr != daddr
Thomas Graf82e91ff2006-11-09 15:19:14 -0800156 || (*pskb)->mark != mark
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 || (*pskb)->nh.iph->tos != tos))
Simon Hormanb4c4ed12006-10-02 16:11:13 -0700158 if (ip_route_me_harder(pskb, RTN_UNSPEC))
159 ret = NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return ret;
162}
163
164static struct nf_hook_ops ipt_ops[] = {
165 {
166 .hook = ipt_route_hook,
167 .owner = THIS_MODULE,
168 .pf = PF_INET,
169 .hooknum = NF_IP_PRE_ROUTING,
170 .priority = NF_IP_PRI_MANGLE,
171 },
172 {
173 .hook = ipt_route_hook,
174 .owner = THIS_MODULE,
175 .pf = PF_INET,
176 .hooknum = NF_IP_LOCAL_IN,
177 .priority = NF_IP_PRI_MANGLE,
178 },
179 {
180 .hook = ipt_route_hook,
181 .owner = THIS_MODULE,
182 .pf = PF_INET,
183 .hooknum = NF_IP_FORWARD,
184 .priority = NF_IP_PRI_MANGLE,
185 },
186 {
187 .hook = ipt_local_hook,
188 .owner = THIS_MODULE,
189 .pf = PF_INET,
190 .hooknum = NF_IP_LOCAL_OUT,
191 .priority = NF_IP_PRI_MANGLE,
192 },
193 {
194 .hook = ipt_route_hook,
195 .owner = THIS_MODULE,
196 .pf = PF_INET,
197 .hooknum = NF_IP_POST_ROUTING,
198 .priority = NF_IP_PRI_MANGLE,
199 },
200};
201
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800202static int __init iptable_mangle_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int ret;
205
206 /* Register table */
207 ret = ipt_register_table(&packet_mangler, &initial_table.repl);
208 if (ret < 0)
209 return ret;
210
211 /* Register hooks */
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700212 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (ret < 0)
214 goto cleanup_table;
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return ret;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 cleanup_table:
219 ipt_unregister_table(&packet_mangler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return ret;
221}
222
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800223static void __exit iptable_mangle_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700225 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ipt_unregister_table(&packet_mangler);
227}
228
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800229module_init(iptable_mangle_init);
230module_exit(iptable_mangle_fini);