blob: 6cc3245f676af73142f3a37bfa8d2f7cbdf69c60 [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>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030020#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22MODULE_LICENSE("GPL");
23MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
24MODULE_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 */
33static 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 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090062 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 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 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090070 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 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
Jan Engelhardte60a13e2007-02-07 15:12:33 -0800107static struct xt_table packet_mangler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .name = "mangle",
109 .valid_hooks = MANGLE_VALID_HOOKS,
110 .lock = RW_LOCK_UNLOCKED,
111 .me = THIS_MODULE,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800112 .af = AF_INET,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115/* The work comes in here from netfilter.c. */
116static unsigned int
117ipt_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{
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700123 return ipt_do_table(pskb, hook, in, out, &packet_mangler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static unsigned int
127ipt_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;
134 u_int8_t tos;
Al Viro6a19d612006-09-28 14:22:24 -0700135 __be32 saddr, daddr;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800136 u_int32_t mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* root is playing with raw sockets. */
139 if ((*pskb)->len < sizeof(struct iphdr)
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300140 || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (net_ratelimit())
142 printk("ipt_hook: happy cracking.\n");
143 return NF_ACCEPT;
144 }
145
146 /* Save things which could affect route */
Thomas Graf82e91ff2006-11-09 15:19:14 -0800147 mark = (*pskb)->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 saddr = (*pskb)->nh.iph->saddr;
149 daddr = (*pskb)->nh.iph->daddr;
150 tos = (*pskb)->nh.iph->tos;
151
Patrick McHardyfe1cb102006-08-22 00:35:47 -0700152 ret = ipt_do_table(pskb, hook, in, out, &packet_mangler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* Reroute for ANY change. */
154 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE
155 && ((*pskb)->nh.iph->saddr != saddr
156 || (*pskb)->nh.iph->daddr != daddr
Thomas Graf82e91ff2006-11-09 15:19:14 -0800157 || (*pskb)->mark != mark
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 || (*pskb)->nh.iph->tos != tos))
Simon Hormanb4c4ed12006-10-02 16:11:13 -0700159 if (ip_route_me_harder(pskb, RTN_UNSPEC))
160 ret = NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 return ret;
163}
164
165static struct nf_hook_ops ipt_ops[] = {
166 {
167 .hook = ipt_route_hook,
168 .owner = THIS_MODULE,
169 .pf = PF_INET,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900170 .hooknum = NF_IP_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .priority = NF_IP_PRI_MANGLE,
172 },
173 {
174 .hook = ipt_route_hook,
175 .owner = THIS_MODULE,
176 .pf = PF_INET,
177 .hooknum = NF_IP_LOCAL_IN,
178 .priority = NF_IP_PRI_MANGLE,
179 },
180 {
181 .hook = ipt_route_hook,
182 .owner = THIS_MODULE,
183 .pf = PF_INET,
184 .hooknum = NF_IP_FORWARD,
185 .priority = NF_IP_PRI_MANGLE,
186 },
187 {
188 .hook = ipt_local_hook,
189 .owner = THIS_MODULE,
190 .pf = PF_INET,
191 .hooknum = NF_IP_LOCAL_OUT,
192 .priority = NF_IP_PRI_MANGLE,
193 },
194 {
195 .hook = ipt_route_hook,
196 .owner = THIS_MODULE,
197 .pf = PF_INET,
198 .hooknum = NF_IP_POST_ROUTING,
199 .priority = NF_IP_PRI_MANGLE,
200 },
201};
202
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800203static int __init iptable_mangle_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 int ret;
206
207 /* Register table */
208 ret = ipt_register_table(&packet_mangler, &initial_table.repl);
209 if (ret < 0)
210 return ret;
211
212 /* Register hooks */
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700213 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (ret < 0)
215 goto cleanup_table;
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return ret;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 cleanup_table:
220 ipt_unregister_table(&packet_mangler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return ret;
222}
223
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800224static void __exit iptable_mangle_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700226 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ipt_unregister_table(&packet_mangler);
228}
229
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800230module_init(iptable_mangle_init);
231module_exit(iptable_mangle_fini);