blob: aef5d1fbe77dc39b5e6b951a97897e02ead47a62 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <net/sock.h>
17#include <net/route.h>
18#include <linux/ip.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030019#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
23MODULE_DESCRIPTION("iptables mangle table");
24
Patrick McHardy6e23ae22007-11-19 18:53:30 -080025#define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
26 (1 << NF_INET_LOCAL_IN) | \
27 (1 << NF_INET_FORWARD) | \
28 (1 << NF_INET_LOCAL_OUT) | \
29 (1 << NF_INET_POST_ROUTING))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020031static const struct xt_table packet_mangler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .name = "mangle",
33 .valid_hooks = MANGLE_VALID_HOOKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 .me = THIS_MODULE,
Jan Engelhardtf88e6a82009-06-13 06:25:44 +020035 .af = NFPROTO_IPV4,
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020036 .priority = NF_IP_PRI_MANGLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static unsigned int
Jan Engelhardtfa96a0e2009-11-01 00:36:40 +010040ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 unsigned int ret;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070043 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 u_int8_t tos;
Al Viro6a19d612006-09-28 14:22:24 -070045 __be32 saddr, daddr;
Thomas Graf82e91ff2006-11-09 15:19:14 -080046 u_int32_t mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 /* root is playing with raw sockets. */
Joe Perches3666ed12009-11-23 23:17:06 +010049 if (skb->len < sizeof(struct iphdr) ||
50 ip_hdrlen(skb) < sizeof(struct iphdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 /* Save things which could affect route */
Herbert Xu3db05fe2007-10-15 00:53:15 -070054 mark = skb->mark;
55 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070056 saddr = iph->saddr;
57 daddr = iph->daddr;
58 tos = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jan Engelhardtfa96a0e2009-11-01 00:36:40 +010060 ret = ipt_do_table(skb, NF_INET_LOCAL_OUT, NULL, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +020061 dev_net(out)->ipv4.iptable_mangle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* Reroute for ANY change. */
Florian Westphal28a51ba2011-01-20 10:23:26 +010063 if (ret != NF_DROP && ret != NF_STOLEN) {
Herbert Xu3db05fe2007-10-15 00:53:15 -070064 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070065
66 if (iph->saddr != saddr ||
67 iph->daddr != daddr ||
Herbert Xu3db05fe2007-10-15 00:53:15 -070068 skb->mark != mark ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070069 iph->tos != tos)
Herbert Xu3db05fe2007-10-15 00:53:15 -070070 if (ip_route_me_harder(skb, RTN_UNSPEC))
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070071 ret = NF_DROP;
72 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 return ret;
75}
76
Jan Engelhardt737535c2009-06-13 06:46:36 +020077/* The work comes in here from netfilter.c. */
78static unsigned int
79iptable_mangle_hook(unsigned int hook,
80 struct sk_buff *skb,
81 const struct net_device *in,
82 const struct net_device *out,
83 int (*okfn)(struct sk_buff *))
84{
85 if (hook == NF_INET_LOCAL_OUT)
Jan Engelhardtfa96a0e2009-11-01 00:36:40 +010086 return ipt_mangle_out(skb, out);
Alexey Dobriyanb2907e52010-02-11 18:41:35 +010087 if (hook == NF_INET_POST_ROUTING)
88 return ipt_do_table(skb, hook, in, out,
89 dev_net(out)->ipv4.iptable_mangle);
Jan Engelhardt737535c2009-06-13 06:46:36 +020090 /* PREROUTING/INPUT/FORWARD: */
91 return ipt_do_table(skb, hook, in, out,
92 dev_net(in)->ipv4.iptable_mangle);
93}
94
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020095static struct nf_hook_ops *mangle_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Alexey Dobriyan9335f042008-01-31 04:03:23 -080097static int __net_init iptable_mangle_net_init(struct net *net)
98{
Jan Engelhardte3eaa992009-06-17 22:14:54 +020099 struct ipt_replace *repl;
100
101 repl = ipt_alloc_initial_table(&packet_mangler);
102 if (repl == NULL)
103 return -ENOMEM;
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800104 net->ipv4.iptable_mangle =
Jan Engelhardte3eaa992009-06-17 22:14:54 +0200105 ipt_register_table(net, &packet_mangler, repl);
106 kfree(repl);
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800107 if (IS_ERR(net->ipv4.iptable_mangle))
108 return PTR_ERR(net->ipv4.iptable_mangle);
109 return 0;
110}
111
112static void __net_exit iptable_mangle_net_exit(struct net *net)
113{
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100114 ipt_unregister_table(net, net->ipv4.iptable_mangle);
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800115}
116
117static struct pernet_operations iptable_mangle_net_ops = {
118 .init = iptable_mangle_net_init,
119 .exit = iptable_mangle_net_exit,
120};
121
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800122static int __init iptable_mangle_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 int ret;
125
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800126 ret = register_pernet_subsys(&iptable_mangle_net_ops);
127 if (ret < 0)
128 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* Register hooks */
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200131 mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
132 if (IS_ERR(mangle_ops)) {
133 ret = PTR_ERR(mangle_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto cleanup_table;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return ret;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 cleanup_table:
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800140 unregister_pernet_subsys(&iptable_mangle_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return ret;
142}
143
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800144static void __exit iptable_mangle_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200146 xt_hook_unlink(&packet_mangler, mangle_ops);
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800147 unregister_pernet_subsys(&iptable_mangle_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800150module_init(iptable_mangle_init);
151module_exit(iptable_mangle_fini);