2 * This is the 1999 rewrite of IP Firewalling, aiming for kernel 2.3.x.
4 * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
5 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
11 * Extended to all five netfilter hooks by Brad Chapman & Harald Welte
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
16 #include <linux/netdevice.h>
17 #include <linux/skbuff.h>
19 #include <net/route.h>
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
24 MODULE_DESCRIPTION("iptables mangle table");
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))
35 struct ipt_entry entry;
36 struct ipt_standard_target target;
39 struct ipt_error_target
41 struct ipt_entry_target target;
42 char errorname[IPT_FUNCTION_MAXNAMELEN];
47 struct ipt_entry entry;
48 struct ipt_error_target target;
51 /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
54 struct ipt_replace repl;
55 struct ipt_standard entries[5];
56 struct ipt_error term;
57 } initial_table __initdata
58 = { { "mangle", MANGLE_VALID_HOOKS, 6,
59 sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
60 { [NF_IP_PRE_ROUTING] = 0,
61 [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard),
62 [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2,
63 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
64 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4 },
65 { [NF_IP_PRE_ROUTING] = 0,
66 [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard),
67 [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2,
68 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
69 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4 },
73 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
75 sizeof(struct ipt_entry),
76 sizeof(struct ipt_standard),
78 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
81 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
83 sizeof(struct ipt_entry),
84 sizeof(struct ipt_standard),
86 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
89 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
91 sizeof(struct ipt_entry),
92 sizeof(struct ipt_standard),
94 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
97 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
99 sizeof(struct ipt_entry),
100 sizeof(struct ipt_standard),
102 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
105 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
107 sizeof(struct ipt_entry),
108 sizeof(struct ipt_standard),
110 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
114 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
116 sizeof(struct ipt_entry),
117 sizeof(struct ipt_error),
119 { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
126 static struct ipt_table packet_mangler = {
128 .table = &initial_table.repl,
129 .valid_hooks = MANGLE_VALID_HOOKS,
130 .lock = RW_LOCK_UNLOCKED,
134 /* The work comes in here from netfilter.c. */
136 ipt_route_hook(unsigned int hook,
137 struct sk_buff **pskb,
138 const struct net_device *in,
139 const struct net_device *out,
140 int (*okfn)(struct sk_buff *))
142 return ipt_do_table(pskb, hook, in, out, &packet_mangler, NULL);
146 ipt_local_hook(unsigned int hook,
147 struct sk_buff **pskb,
148 const struct net_device *in,
149 const struct net_device *out,
150 int (*okfn)(struct sk_buff *))
154 u_int32_t saddr, daddr;
155 unsigned long nfmark;
157 /* root is playing with raw sockets. */
158 if ((*pskb)->len < sizeof(struct iphdr)
159 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
161 printk("ipt_hook: happy cracking.\n");
165 /* Save things which could affect route */
166 nfmark = (*pskb)->nfmark;
167 saddr = (*pskb)->nh.iph->saddr;
168 daddr = (*pskb)->nh.iph->daddr;
169 tos = (*pskb)->nh.iph->tos;
171 ret = ipt_do_table(pskb, hook, in, out, &packet_mangler, NULL);
172 /* Reroute for ANY change. */
173 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE
174 && ((*pskb)->nh.iph->saddr != saddr
175 || (*pskb)->nh.iph->daddr != daddr
176 || (*pskb)->nfmark != nfmark
177 || (*pskb)->nh.iph->tos != tos))
178 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
183 static struct nf_hook_ops ipt_ops[] = {
185 .hook = ipt_route_hook,
186 .owner = THIS_MODULE,
188 .hooknum = NF_IP_PRE_ROUTING,
189 .priority = NF_IP_PRI_MANGLE,
192 .hook = ipt_route_hook,
193 .owner = THIS_MODULE,
195 .hooknum = NF_IP_LOCAL_IN,
196 .priority = NF_IP_PRI_MANGLE,
199 .hook = ipt_route_hook,
200 .owner = THIS_MODULE,
202 .hooknum = NF_IP_FORWARD,
203 .priority = NF_IP_PRI_MANGLE,
206 .hook = ipt_local_hook,
207 .owner = THIS_MODULE,
209 .hooknum = NF_IP_LOCAL_OUT,
210 .priority = NF_IP_PRI_MANGLE,
213 .hook = ipt_route_hook,
214 .owner = THIS_MODULE,
216 .hooknum = NF_IP_POST_ROUTING,
217 .priority = NF_IP_PRI_MANGLE,
221 static int __init init(void)
226 ret = ipt_register_table(&packet_mangler);
231 ret = nf_register_hook(&ipt_ops[0]);
235 ret = nf_register_hook(&ipt_ops[1]);
239 ret = nf_register_hook(&ipt_ops[2]);
243 ret = nf_register_hook(&ipt_ops[3]);
247 ret = nf_register_hook(&ipt_ops[4]);
254 nf_unregister_hook(&ipt_ops[3]);
256 nf_unregister_hook(&ipt_ops[2]);
258 nf_unregister_hook(&ipt_ops[1]);
260 nf_unregister_hook(&ipt_ops[0]);
262 ipt_unregister_table(&packet_mangler);
267 static void __exit fini(void)
271 for (i = 0; i < sizeof(ipt_ops)/sizeof(struct nf_hook_ops); i++)
272 nf_unregister_hook(&ipt_ops[i]);
274 ipt_unregister_table(&packet_mangler);