Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | * Extended to all five netfilter hooks by Brad Chapman & Harald Welte | |
12 | */ | |
1da177e4 LT |
13 | #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> | |
c9bdd4b5 | 20 | #include <net/ip.h> |
1da177e4 LT |
21 | |
22 | MODULE_LICENSE("GPL"); | |
23 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
24 | MODULE_DESCRIPTION("iptables mangle table"); | |
25 | ||
26 | #define MANGLE_VALID_HOOKS ((1 << NF_IP_PRE_ROUTING) | \ | |
27 | (1 << NF_IP_LOCAL_IN) | \ | |
28 | (1 << NF_IP_FORWARD) | \ | |
29 | (1 << NF_IP_LOCAL_OUT) | \ | |
30 | (1 << NF_IP_POST_ROUTING)) | |
31 | ||
32 | /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */ | |
33 | static struct | |
34 | { | |
35 | struct ipt_replace repl; | |
36 | struct ipt_standard entries[5]; | |
37 | struct ipt_error term; | |
38 | } initial_table __initdata | |
39 | = { { "mangle", MANGLE_VALID_HOOKS, 6, | |
40 | sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error), | |
41 | { [NF_IP_PRE_ROUTING] = 0, | |
42 | [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard), | |
43 | [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2, | |
44 | [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3, | |
45 | [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4 }, | |
46 | { [NF_IP_PRE_ROUTING] = 0, | |
47 | [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard), | |
48 | [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2, | |
49 | [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3, | |
50 | [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4 }, | |
51 | 0, NULL, { } }, | |
52 | { | |
53 | /* PRE_ROUTING */ | |
54 | { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 }, | |
55 | 0, | |
56 | sizeof(struct ipt_entry), | |
57 | sizeof(struct ipt_standard), | |
58 | 0, { 0, 0 }, { } }, | |
59 | { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } }, | |
60 | -NF_ACCEPT - 1 } }, | |
61 | /* LOCAL_IN */ | |
e905a9ed | 62 | { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 }, |
1da177e4 LT |
63 | 0, |
64 | sizeof(struct ipt_entry), | |
65 | sizeof(struct ipt_standard), | |
66 | 0, { 0, 0 }, { } }, | |
67 | { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } }, | |
68 | -NF_ACCEPT - 1 } }, | |
69 | /* FORWARD */ | |
e905a9ed | 70 | { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 }, |
1da177e4 LT |
71 | 0, |
72 | sizeof(struct ipt_entry), | |
73 | sizeof(struct ipt_standard), | |
74 | 0, { 0, 0 }, { } }, | |
75 | { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } }, | |
76 | -NF_ACCEPT - 1 } }, | |
77 | /* LOCAL_OUT */ | |
78 | { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 }, | |
79 | 0, | |
80 | sizeof(struct ipt_entry), | |
81 | sizeof(struct ipt_standard), | |
82 | 0, { 0, 0 }, { } }, | |
83 | { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } }, | |
84 | -NF_ACCEPT - 1 } }, | |
85 | /* POST_ROUTING */ | |
86 | { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 }, | |
87 | 0, | |
88 | sizeof(struct ipt_entry), | |
89 | sizeof(struct ipt_standard), | |
90 | 0, { 0, 0 }, { } }, | |
91 | { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } }, | |
92 | -NF_ACCEPT - 1 } }, | |
93 | }, | |
94 | /* ERROR */ | |
95 | { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 }, | |
96 | 0, | |
97 | sizeof(struct ipt_entry), | |
98 | sizeof(struct ipt_error), | |
99 | 0, { 0, 0 }, { } }, | |
100 | { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } }, | |
101 | { } }, | |
102 | "ERROR" | |
103 | } | |
104 | } | |
105 | }; | |
106 | ||
e60a13e0 | 107 | static struct xt_table packet_mangler = { |
1da177e4 LT |
108 | .name = "mangle", |
109 | .valid_hooks = MANGLE_VALID_HOOKS, | |
110 | .lock = RW_LOCK_UNLOCKED, | |
111 | .me = THIS_MODULE, | |
2e4e6a17 | 112 | .af = AF_INET, |
1da177e4 LT |
113 | }; |
114 | ||
115 | /* The work comes in here from netfilter.c. */ | |
116 | static unsigned int | |
117 | ipt_route_hook(unsigned int hook, | |
118 | struct sk_buff **pskb, | |
119 | const struct net_device *in, | |
120 | const struct net_device *out, | |
121 | int (*okfn)(struct sk_buff *)) | |
122 | { | |
fe1cb108 | 123 | return ipt_do_table(pskb, hook, in, out, &packet_mangler); |
1da177e4 LT |
124 | } |
125 | ||
126 | static unsigned int | |
127 | ipt_local_hook(unsigned int hook, | |
128 | struct sk_buff **pskb, | |
129 | const struct net_device *in, | |
130 | const struct net_device *out, | |
131 | int (*okfn)(struct sk_buff *)) | |
132 | { | |
133 | unsigned int ret; | |
eddc9ec5 | 134 | const struct iphdr *iph; |
1da177e4 | 135 | u_int8_t tos; |
6a19d614 | 136 | __be32 saddr, daddr; |
82e91ffe | 137 | u_int32_t mark; |
1da177e4 LT |
138 | |
139 | /* root is playing with raw sockets. */ | |
140 | if ((*pskb)->len < sizeof(struct iphdr) | |
c9bdd4b5 | 141 | || ip_hdrlen(*pskb) < sizeof(struct iphdr)) { |
1da177e4 LT |
142 | if (net_ratelimit()) |
143 | printk("ipt_hook: happy cracking.\n"); | |
144 | return NF_ACCEPT; | |
145 | } | |
146 | ||
147 | /* Save things which could affect route */ | |
82e91ffe | 148 | mark = (*pskb)->mark; |
eddc9ec5 ACM |
149 | iph = ip_hdr(*pskb); |
150 | saddr = iph->saddr; | |
151 | daddr = iph->daddr; | |
152 | tos = iph->tos; | |
1da177e4 | 153 | |
fe1cb108 | 154 | ret = ipt_do_table(pskb, hook, in, out, &packet_mangler); |
1da177e4 | 155 | /* Reroute for ANY change. */ |
eddc9ec5 ACM |
156 | if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) { |
157 | iph = ip_hdr(*pskb); | |
158 | ||
159 | if (iph->saddr != saddr || | |
160 | iph->daddr != daddr || | |
161 | (*pskb)->mark != mark || | |
162 | iph->tos != tos) | |
163 | if (ip_route_me_harder(pskb, RTN_UNSPEC)) | |
164 | ret = NF_DROP; | |
165 | } | |
1da177e4 LT |
166 | |
167 | return ret; | |
168 | } | |
169 | ||
170 | static struct nf_hook_ops ipt_ops[] = { | |
171 | { | |
172 | .hook = ipt_route_hook, | |
173 | .owner = THIS_MODULE, | |
174 | .pf = PF_INET, | |
e905a9ed | 175 | .hooknum = NF_IP_PRE_ROUTING, |
1da177e4 LT |
176 | .priority = NF_IP_PRI_MANGLE, |
177 | }, | |
178 | { | |
179 | .hook = ipt_route_hook, | |
180 | .owner = THIS_MODULE, | |
181 | .pf = PF_INET, | |
182 | .hooknum = NF_IP_LOCAL_IN, | |
183 | .priority = NF_IP_PRI_MANGLE, | |
184 | }, | |
185 | { | |
186 | .hook = ipt_route_hook, | |
187 | .owner = THIS_MODULE, | |
188 | .pf = PF_INET, | |
189 | .hooknum = NF_IP_FORWARD, | |
190 | .priority = NF_IP_PRI_MANGLE, | |
191 | }, | |
192 | { | |
193 | .hook = ipt_local_hook, | |
194 | .owner = THIS_MODULE, | |
195 | .pf = PF_INET, | |
196 | .hooknum = NF_IP_LOCAL_OUT, | |
197 | .priority = NF_IP_PRI_MANGLE, | |
198 | }, | |
199 | { | |
200 | .hook = ipt_route_hook, | |
201 | .owner = THIS_MODULE, | |
202 | .pf = PF_INET, | |
203 | .hooknum = NF_IP_POST_ROUTING, | |
204 | .priority = NF_IP_PRI_MANGLE, | |
205 | }, | |
206 | }; | |
207 | ||
65b4b4e8 | 208 | static int __init iptable_mangle_init(void) |
1da177e4 LT |
209 | { |
210 | int ret; | |
211 | ||
212 | /* Register table */ | |
213 | ret = ipt_register_table(&packet_mangler, &initial_table.repl); | |
214 | if (ret < 0) | |
215 | return ret; | |
216 | ||
217 | /* Register hooks */ | |
964ddaa1 | 218 | ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
1da177e4 LT |
219 | if (ret < 0) |
220 | goto cleanup_table; | |
221 | ||
1da177e4 LT |
222 | return ret; |
223 | ||
1da177e4 LT |
224 | cleanup_table: |
225 | ipt_unregister_table(&packet_mangler); | |
1da177e4 LT |
226 | return ret; |
227 | } | |
228 | ||
65b4b4e8 | 229 | static void __exit iptable_mangle_fini(void) |
1da177e4 | 230 | { |
964ddaa1 | 231 | nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
1da177e4 LT |
232 | ipt_unregister_table(&packet_mangler); |
233 | } | |
234 | ||
65b4b4e8 AM |
235 | module_init(iptable_mangle_init); |
236 | module_exit(iptable_mangle_fini); |