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 #include <linux/module.h>
12 #include <linux/netfilter_ipv4/ip_tables.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
16 #include <net/route.h>
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
22 MODULE_DESCRIPTION("iptables mangle table");
24 #define MANGLE_VALID_HOOKS ((1 << NF_IP_PRE_ROUTING) | \
25 (1 << NF_IP_LOCAL_IN) | \
26 (1 << NF_IP_FORWARD) | \
27 (1 << NF_IP_LOCAL_OUT) | \
28 (1 << NF_IP_POST_ROUTING))
30 /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
33 struct ipt_replace repl;
34 struct ipt_standard entries[5];
35 struct ipt_error term;
36 } initial_table __initdata
37 = { { "mangle", MANGLE_VALID_HOOKS, 6,
38 sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
39 { [NF_IP_PRE_ROUTING] = 0,
40 [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard),
41 [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2,
42 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
43 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4 },
44 { [NF_IP_PRE_ROUTING] = 0,
45 [NF_IP_LOCAL_IN] = sizeof(struct ipt_standard),
46 [NF_IP_FORWARD] = sizeof(struct ipt_standard) * 2,
47 [NF_IP_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
48 [NF_IP_POST_ROUTING] = sizeof(struct ipt_standard) * 4 },
52 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
54 sizeof(struct ipt_entry),
55 sizeof(struct ipt_standard),
57 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
60 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
62 sizeof(struct ipt_entry),
63 sizeof(struct ipt_standard),
65 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
68 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
70 sizeof(struct ipt_entry),
71 sizeof(struct ipt_standard),
73 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
76 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
78 sizeof(struct ipt_entry),
79 sizeof(struct ipt_standard),
81 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
84 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
86 sizeof(struct ipt_entry),
87 sizeof(struct ipt_standard),
89 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
93 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
95 sizeof(struct ipt_entry),
96 sizeof(struct ipt_error),
98 { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
105 static struct xt_table packet_mangler = {
107 .valid_hooks = MANGLE_VALID_HOOKS,
108 .lock = RW_LOCK_UNLOCKED,
113 /* The work comes in here from netfilter.c. */
115 ipt_route_hook(unsigned int hook,
116 struct sk_buff **pskb,
117 const struct net_device *in,
118 const struct net_device *out,
119 int (*okfn)(struct sk_buff *))
121 return ipt_do_table(pskb, hook, in, out, &packet_mangler);
125 ipt_local_hook(unsigned int hook,
126 struct sk_buff **pskb,
127 const struct net_device *in,
128 const struct net_device *out,
129 int (*okfn)(struct sk_buff *))
132 const struct iphdr *iph;
137 /* root is playing with raw sockets. */
138 if ((*pskb)->len < sizeof(struct iphdr)
139 || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
141 printk("ipt_hook: happy cracking.\n");
145 /* Save things which could affect route */
146 mark = (*pskb)->mark;
152 ret = ipt_do_table(pskb, hook, in, out, &packet_mangler);
153 /* Reroute for ANY change. */
154 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
157 if (iph->saddr != saddr ||
158 iph->daddr != daddr ||
159 (*pskb)->mark != mark ||
161 if (ip_route_me_harder(pskb, RTN_UNSPEC))
168 static struct nf_hook_ops ipt_ops[] = {
170 .hook = ipt_route_hook,
171 .owner = THIS_MODULE,
173 .hooknum = NF_IP_PRE_ROUTING,
174 .priority = NF_IP_PRI_MANGLE,
177 .hook = ipt_route_hook,
178 .owner = THIS_MODULE,
180 .hooknum = NF_IP_LOCAL_IN,
181 .priority = NF_IP_PRI_MANGLE,
184 .hook = ipt_route_hook,
185 .owner = THIS_MODULE,
187 .hooknum = NF_IP_FORWARD,
188 .priority = NF_IP_PRI_MANGLE,
191 .hook = ipt_local_hook,
192 .owner = THIS_MODULE,
194 .hooknum = NF_IP_LOCAL_OUT,
195 .priority = NF_IP_PRI_MANGLE,
198 .hook = ipt_route_hook,
199 .owner = THIS_MODULE,
201 .hooknum = NF_IP_POST_ROUTING,
202 .priority = NF_IP_PRI_MANGLE,
206 static int __init iptable_mangle_init(void)
211 ret = ipt_register_table(&packet_mangler, &initial_table.repl);
216 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
223 ipt_unregister_table(&packet_mangler);
227 static void __exit iptable_mangle_fini(void)
229 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
230 ipt_unregister_table(&packet_mangler);
233 module_init(iptable_mangle_init);
234 module_exit(iptable_mangle_fini);