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))
32 /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
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 },
54 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
56 sizeof(struct ipt_entry
),
57 sizeof(struct ipt_standard
),
59 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target
)), "" } }, { } },
62 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
64 sizeof(struct ipt_entry
),
65 sizeof(struct ipt_standard
),
67 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target
)), "" } }, { } },
70 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
72 sizeof(struct ipt_entry
),
73 sizeof(struct ipt_standard
),
75 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target
)), "" } }, { } },
78 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
80 sizeof(struct ipt_entry
),
81 sizeof(struct ipt_standard
),
83 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target
)), "" } }, { } },
86 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
88 sizeof(struct ipt_entry
),
89 sizeof(struct ipt_standard
),
91 { { { { IPT_ALIGN(sizeof(struct ipt_standard_target
)), "" } }, { } },
95 { { { { 0 }, { 0 }, { 0 }, { 0 }, "", "", { 0 }, { 0 }, 0, 0, 0 },
97 sizeof(struct ipt_entry
),
98 sizeof(struct ipt_error
),
100 { { { { IPT_ALIGN(sizeof(struct ipt_error_target
)), IPT_ERROR_TARGET
} },
107 static struct ipt_table packet_mangler
= {
109 .valid_hooks
= MANGLE_VALID_HOOKS
,
110 .lock
= RW_LOCK_UNLOCKED
,
114 /* The work comes in here from netfilter.c. */
116 ipt_route_hook(unsigned int hook
,
117 struct sk_buff
**pskb
,
118 const struct net_device
*in
,
119 const struct net_device
*out
,
120 int (*okfn
)(struct sk_buff
*))
122 return ipt_do_table(pskb
, hook
, in
, out
, &packet_mangler
, NULL
);
126 ipt_local_hook(unsigned int hook
,
127 struct sk_buff
**pskb
,
128 const struct net_device
*in
,
129 const struct net_device
*out
,
130 int (*okfn
)(struct sk_buff
*))
134 u_int32_t saddr
, daddr
;
135 unsigned long nfmark
;
137 /* root is playing with raw sockets. */
138 if ((*pskb
)->len
< sizeof(struct iphdr
)
139 || (*pskb
)->nh
.iph
->ihl
* 4 < sizeof(struct iphdr
)) {
141 printk("ipt_hook: happy cracking.\n");
145 /* Save things which could affect route */
146 nfmark
= (*pskb
)->nfmark
;
147 saddr
= (*pskb
)->nh
.iph
->saddr
;
148 daddr
= (*pskb
)->nh
.iph
->daddr
;
149 tos
= (*pskb
)->nh
.iph
->tos
;
151 ret
= ipt_do_table(pskb
, hook
, in
, out
, &packet_mangler
, NULL
);
152 /* Reroute for ANY change. */
153 if (ret
!= NF_DROP
&& ret
!= NF_STOLEN
&& ret
!= NF_QUEUE
154 && ((*pskb
)->nh
.iph
->saddr
!= saddr
155 || (*pskb
)->nh
.iph
->daddr
!= daddr
156 #ifdef CONFIG_IP_ROUTE_FWMARK
157 || (*pskb
)->nfmark
!= nfmark
159 || (*pskb
)->nh
.iph
->tos
!= tos
))
160 return ip_route_me_harder(pskb
) == 0 ? ret
: NF_DROP
;
165 static struct nf_hook_ops ipt_ops
[] = {
167 .hook
= ipt_route_hook
,
168 .owner
= THIS_MODULE
,
170 .hooknum
= NF_IP_PRE_ROUTING
,
171 .priority
= NF_IP_PRI_MANGLE
,
174 .hook
= ipt_route_hook
,
175 .owner
= THIS_MODULE
,
177 .hooknum
= NF_IP_LOCAL_IN
,
178 .priority
= NF_IP_PRI_MANGLE
,
181 .hook
= ipt_route_hook
,
182 .owner
= THIS_MODULE
,
184 .hooknum
= NF_IP_FORWARD
,
185 .priority
= NF_IP_PRI_MANGLE
,
188 .hook
= ipt_local_hook
,
189 .owner
= THIS_MODULE
,
191 .hooknum
= NF_IP_LOCAL_OUT
,
192 .priority
= NF_IP_PRI_MANGLE
,
195 .hook
= ipt_route_hook
,
196 .owner
= THIS_MODULE
,
198 .hooknum
= NF_IP_POST_ROUTING
,
199 .priority
= NF_IP_PRI_MANGLE
,
203 static int __init
init(void)
208 ret
= ipt_register_table(&packet_mangler
, &initial_table
.repl
);
213 ret
= nf_register_hook(&ipt_ops
[0]);
217 ret
= nf_register_hook(&ipt_ops
[1]);
221 ret
= nf_register_hook(&ipt_ops
[2]);
225 ret
= nf_register_hook(&ipt_ops
[3]);
229 ret
= nf_register_hook(&ipt_ops
[4]);
236 nf_unregister_hook(&ipt_ops
[3]);
238 nf_unregister_hook(&ipt_ops
[2]);
240 nf_unregister_hook(&ipt_ops
[1]);
242 nf_unregister_hook(&ipt_ops
[0]);
244 ipt_unregister_table(&packet_mangler
);
249 static void __exit
fini(void)
253 for (i
= 0; i
< sizeof(ipt_ops
)/sizeof(struct nf_hook_ops
); i
++)
254 nf_unregister_hook(&ipt_ops
[i
]);
256 ipt_unregister_table(&packet_mangler
);