blob: 4e699cd275c61c4c926778d632308d3a1973ee95 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/netfilter_ipv4/ip_tables.h>
13#include <linux/netdevice.h>
14#include <linux/skbuff.h>
15#include <net/sock.h>
16#include <net/route.h>
17#include <linux/ip.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030018#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20MODULE_LICENSE("GPL");
21MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
22MODULE_DESCRIPTION("iptables mangle table");
23
Patrick McHardy6e23ae22007-11-19 18:53:30 -080024#define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
25 (1 << NF_INET_LOCAL_IN) | \
26 (1 << NF_INET_FORWARD) | \
27 (1 << NF_INET_LOCAL_OUT) | \
28 (1 << NF_INET_POST_ROUTING))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30/* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020031static const struct
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 struct ipt_replace repl;
34 struct ipt_standard entries[5];
35 struct ipt_error term;
Alexey Dobriyan9335f042008-01-31 04:03:23 -080036} initial_table __net_initdata = {
Patrick McHardy3c2ad462007-05-10 14:14:16 -070037 .repl = {
38 .name = "mangle",
39 .valid_hooks = MANGLE_VALID_HOOKS,
40 .num_entries = 6,
41 .size = sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
42 .hook_entry = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -080043 [NF_INET_PRE_ROUTING] = 0,
44 [NF_INET_LOCAL_IN] = sizeof(struct ipt_standard),
45 [NF_INET_FORWARD] = sizeof(struct ipt_standard) * 2,
46 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
47 [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard) * 4,
Patrick McHardy3c2ad462007-05-10 14:14:16 -070048 },
49 .underflow = {
Patrick McHardy6e23ae22007-11-19 18:53:30 -080050 [NF_INET_PRE_ROUTING] = 0,
51 [NF_INET_LOCAL_IN] = sizeof(struct ipt_standard),
52 [NF_INET_FORWARD] = sizeof(struct ipt_standard) * 2,
53 [NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
54 [NF_INET_POST_ROUTING] = sizeof(struct ipt_standard) * 4,
Patrick McHardy3c2ad462007-05-10 14:14:16 -070055 },
56 },
57 .entries = {
58 IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
59 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */
60 IPT_STANDARD_INIT(NF_ACCEPT), /* FORWARD */
61 IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
62 IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */
63 },
64 .term = IPT_ERROR_INIT, /* ERROR */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020067static const struct xt_table packet_mangler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .name = "mangle",
69 .valid_hooks = MANGLE_VALID_HOOKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .me = THIS_MODULE,
Jan Engelhardtf88e6a82009-06-13 06:25:44 +020071 .af = NFPROTO_IPV4,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static unsigned int
75ipt_local_hook(unsigned int hook,
Herbert Xu3db05fe2007-10-15 00:53:15 -070076 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 const struct net_device *in,
78 const struct net_device *out,
79 int (*okfn)(struct sk_buff *))
80{
81 unsigned int ret;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070082 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 u_int8_t tos;
Al Viro6a19d612006-09-28 14:22:24 -070084 __be32 saddr, daddr;
Thomas Graf82e91ff2006-11-09 15:19:14 -080085 u_int32_t mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /* root is playing with raw sockets. */
Joe Perches3666ed12009-11-23 23:17:06 +010088 if (skb->len < sizeof(struct iphdr) ||
89 ip_hdrlen(skb) < sizeof(struct iphdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* Save things which could affect route */
Herbert Xu3db05fe2007-10-15 00:53:15 -070093 mark = skb->mark;
94 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070095 saddr = iph->saddr;
96 daddr = iph->daddr;
97 tos = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Alexey Dobriyan666953df2008-04-14 09:56:02 +020099 ret = ipt_do_table(skb, hook, in, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +0200100 dev_net(out)->ipv4.iptable_mangle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* Reroute for ANY change. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700102 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
Herbert Xu3db05fe2007-10-15 00:53:15 -0700103 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700104
105 if (iph->saddr != saddr ||
106 iph->daddr != daddr ||
Herbert Xu3db05fe2007-10-15 00:53:15 -0700107 skb->mark != mark ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700108 iph->tos != tos)
Herbert Xu3db05fe2007-10-15 00:53:15 -0700109 if (ip_route_me_harder(skb, RTN_UNSPEC))
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700110 ret = NF_DROP;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 return ret;
114}
115
Jan Engelhardt737535c2009-06-13 06:46:36 +0200116/* The work comes in here from netfilter.c. */
117static unsigned int
118iptable_mangle_hook(unsigned int hook,
119 struct sk_buff *skb,
120 const struct net_device *in,
121 const struct net_device *out,
122 int (*okfn)(struct sk_buff *))
123{
124 if (hook == NF_INET_LOCAL_OUT)
125 return ipt_local_hook(hook, skb, in, out, okfn);
126
127 /* PREROUTING/INPUT/FORWARD: */
128 return ipt_do_table(skb, hook, in, out,
129 dev_net(in)->ipv4.iptable_mangle);
130}
131
Patrick McHardy19994142007-12-05 01:23:00 -0800132static struct nf_hook_ops ipt_ops[] __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 {
Jan Engelhardt737535c2009-06-13 06:46:36 +0200134 .hook = iptable_mangle_hook,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +0200136 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800137 .hooknum = NF_INET_PRE_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .priority = NF_IP_PRI_MANGLE,
139 },
140 {
Jan Engelhardt737535c2009-06-13 06:46:36 +0200141 .hook = iptable_mangle_hook,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +0200143 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800144 .hooknum = NF_INET_LOCAL_IN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .priority = NF_IP_PRI_MANGLE,
146 },
147 {
Jan Engelhardt737535c2009-06-13 06:46:36 +0200148 .hook = iptable_mangle_hook,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +0200150 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800151 .hooknum = NF_INET_FORWARD,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 .priority = NF_IP_PRI_MANGLE,
153 },
154 {
Jan Engelhardt737535c2009-06-13 06:46:36 +0200155 .hook = iptable_mangle_hook,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +0200157 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800158 .hooknum = NF_INET_LOCAL_OUT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 .priority = NF_IP_PRI_MANGLE,
160 },
161 {
Jan Engelhardt737535c2009-06-13 06:46:36 +0200162 .hook = iptable_mangle_hook,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 .owner = THIS_MODULE,
Jan Engelhardt24c232d2009-06-13 06:20:29 +0200164 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800165 .hooknum = NF_INET_POST_ROUTING,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .priority = NF_IP_PRI_MANGLE,
167 },
168};
169
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800170static int __net_init iptable_mangle_net_init(struct net *net)
171{
172 /* Register table */
173 net->ipv4.iptable_mangle =
174 ipt_register_table(net, &packet_mangler, &initial_table.repl);
175 if (IS_ERR(net->ipv4.iptable_mangle))
176 return PTR_ERR(net->ipv4.iptable_mangle);
177 return 0;
178}
179
180static void __net_exit iptable_mangle_net_exit(struct net *net)
181{
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100182 ipt_unregister_table(net, net->ipv4.iptable_mangle);
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800183}
184
185static struct pernet_operations iptable_mangle_net_ops = {
186 .init = iptable_mangle_net_init,
187 .exit = iptable_mangle_net_exit,
188};
189
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800190static int __init iptable_mangle_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 int ret;
193
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800194 ret = register_pernet_subsys(&iptable_mangle_net_ops);
195 if (ret < 0)
196 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* Register hooks */
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700199 ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (ret < 0)
201 goto cleanup_table;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return ret;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 cleanup_table:
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800206 unregister_pernet_subsys(&iptable_mangle_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return ret;
208}
209
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800210static void __exit iptable_mangle_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700212 nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800213 unregister_pernet_subsys(&iptable_mangle_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800216module_init(iptable_mangle_init);
217module_exit(iptable_mangle_fini);