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
6 * Extended to all five netfilter hooks by Brad Chapman & Harald Welte
8 #include <linux/config.h>
9 #include <linux/module.h>
10 #include <linux/netfilter_ipv4/ip_tables.h>
11 #include <linux/netdevice.h>
12 #include <linux/skbuff.h>
14 #include <net/route.h>
17 #define MANGLE_VALID_HOOKS ((1 << NF_IP_PRE_ROUTING) | \
18 (1 << NF_IP_LOCAL_IN) | \
19 (1 << NF_IP_FORWARD) | \
20 (1 << NF_IP_LOCAL_OUT) | \
21 (1 << NF_IP_POST_ROUTING))
26 struct ipt_entry entry;
27 struct ipt_standard_target target;
30 struct ipt_error_target
32 struct ipt_entry_target target;
33 char errorname[IPT_FUNCTION_MAXNAMELEN];
38 struct ipt_entry entry;
39 struct ipt_error_target target;
42 /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
45 struct ipt_replace repl;
46 struct ipt_standard entries[5];
47 struct ipt_error term;
48 } initial_table __initdata
49 = { { "mangle", MANGLE_VALID_HOOKS, 6,
50 sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
51 { [NF_IP_PRE_ROUTING] 0,
52 [NF_IP_LOCAL_IN] sizeof(struct ipt_standard),
53 [NF_IP_FORWARD] sizeof(struct ipt_standard) * 2,
54 [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 3,
55 [NF_IP_POST_ROUTING] sizeof(struct ipt_standard) * 4 },
56 { [NF_IP_PRE_ROUTING] 0,
57 [NF_IP_LOCAL_IN] sizeof(struct ipt_standard),
58 [NF_IP_FORWARD] sizeof(struct ipt_standard) * 2,
59 [NF_IP_LOCAL_OUT] sizeof(struct ipt_standard) * 3,
60 [NF_IP_POST_ROUTING] sizeof(struct ipt_standard) * 4 },
64 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
66 sizeof(struct ipt_entry),
67 sizeof(struct ipt_standard),
69 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
72 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
74 sizeof(struct ipt_entry),
75 sizeof(struct ipt_standard),
77 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
80 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
82 sizeof(struct ipt_entry),
83 sizeof(struct ipt_standard),
85 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
88 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
90 sizeof(struct ipt_entry),
91 sizeof(struct ipt_standard),
93 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target)), "" } }, { } },
96 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
98 sizeof(struct ipt_entry),
99 sizeof(struct ipt_standard),
101 { { { { 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_error),
110 { { { { IPT_ALIGN(sizeof(struct ipt_error_target)), IPT_ERROR_TARGET } },
117 static struct ipt_table packet_mangler
118 = { { NULL, NULL }, "mangle", &initial_table.repl,
119 MANGLE_VALID_HOOKS, RW_LOCK_UNLOCKED, NULL, THIS_MODULE };
121 /* The work comes in here from netfilter.c. */
123 ipt_route_hook(unsigned int hook,
124 struct sk_buff **pskb,
125 const struct net_device *in,
126 const struct net_device *out,
127 int (*okfn)(struct sk_buff *))
129 return ipt_do_table(pskb, hook, in, out, &packet_mangler, NULL);
133 ipt_local_hook(unsigned int hook,
134 struct sk_buff **pskb,
135 const struct net_device *in,
136 const struct net_device *out,
137 int (*okfn)(struct sk_buff *))
141 u_int32_t saddr, daddr;
142 unsigned long nfmark;
144 /* root is playing with raw sockets. */
145 if ((*pskb)->len < sizeof(struct iphdr)
146 || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
148 printk("ipt_hook: happy cracking.\n");
152 /* Save things which could affect route */
153 nfmark = (*pskb)->nfmark;
154 saddr = (*pskb)->nh.iph->saddr;
155 daddr = (*pskb)->nh.iph->daddr;
156 tos = (*pskb)->nh.iph->tos;
158 ret = ipt_do_table(pskb, hook, in, out, &packet_mangler, NULL);
159 /* Reroute for ANY change. */
160 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE
161 && ((*pskb)->nh.iph->saddr != saddr
162 || (*pskb)->nh.iph->daddr != daddr
163 #ifdef CONFIG_IP_ROUTE_FWMARK
164 || (*pskb)->nfmark != nfmark
166 || (*pskb)->nh.iph->tos != tos))
167 return ip_route_me_harder(pskb) == 0 ? ret : NF_DROP;
172 static struct nf_hook_ops ipt_ops[]
173 = { { { NULL, NULL }, ipt_route_hook, PF_INET, NF_IP_PRE_ROUTING,
175 { { NULL, NULL }, ipt_route_hook, PF_INET, NF_IP_LOCAL_IN,
177 { { NULL, NULL }, ipt_route_hook, PF_INET, NF_IP_FORWARD,
179 { { NULL, NULL }, ipt_local_hook, PF_INET, NF_IP_LOCAL_OUT,
181 { { NULL, NULL }, ipt_route_hook, PF_INET, NF_IP_POST_ROUTING,
185 static int __init init(void)
190 ret = ipt_register_table(&packet_mangler);
195 ret = nf_register_hook(&ipt_ops[0]);
199 ret = nf_register_hook(&ipt_ops[1]);
203 ret = nf_register_hook(&ipt_ops[2]);
207 ret = nf_register_hook(&ipt_ops[3]);
211 ret = nf_register_hook(&ipt_ops[4]);
218 nf_unregister_hook(&ipt_ops[3]);
220 nf_unregister_hook(&ipt_ops[2]);
222 nf_unregister_hook(&ipt_ops[1]);
224 nf_unregister_hook(&ipt_ops[0]);
226 ipt_unregister_table(&packet_mangler);
231 static void __exit fini(void)
235 for (i = 0; i < sizeof(ipt_ops)/sizeof(struct nf_hook_ops); i++)
236 nf_unregister_hook(&ipt_ops[i]);
238 ipt_unregister_table(&packet_mangler);
243 MODULE_LICENSE("GPL");