blob: b9b83464cbf4bf91d745c01e25b1d45bbc12546d [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
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020030static const struct xt_table packet_mangler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .name = "mangle",
32 .valid_hooks = MANGLE_VALID_HOOKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 .me = THIS_MODULE,
Jan Engelhardtf88e6a82009-06-13 06:25:44 +020034 .af = NFPROTO_IPV4,
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020035 .priority = NF_IP_PRI_MANGLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static unsigned int
Jan Engelhardtfa96a0e2009-11-01 00:36:40 +010039ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 unsigned int ret;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070042 const struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 u_int8_t tos;
Al Viro6a19d612006-09-28 14:22:24 -070044 __be32 saddr, daddr;
Thomas Graf82e91ff2006-11-09 15:19:14 -080045 u_int32_t mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 /* root is playing with raw sockets. */
Joe Perches3666ed12009-11-23 23:17:06 +010048 if (skb->len < sizeof(struct iphdr) ||
49 ip_hdrlen(skb) < sizeof(struct iphdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 /* Save things which could affect route */
Herbert Xu3db05fe2007-10-15 00:53:15 -070053 mark = skb->mark;
54 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070055 saddr = iph->saddr;
56 daddr = iph->daddr;
57 tos = iph->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jan Engelhardtfa96a0e2009-11-01 00:36:40 +010059 ret = ipt_do_table(skb, NF_INET_LOCAL_OUT, NULL, out,
Alexey Dobriyan48dc7862008-10-08 11:35:01 +020060 dev_net(out)->ipv4.iptable_mangle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* Reroute for ANY change. */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070062 if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
Herbert Xu3db05fe2007-10-15 00:53:15 -070063 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070064
65 if (iph->saddr != saddr ||
66 iph->daddr != daddr ||
Herbert Xu3db05fe2007-10-15 00:53:15 -070067 skb->mark != mark ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070068 iph->tos != tos)
Herbert Xu3db05fe2007-10-15 00:53:15 -070069 if (ip_route_me_harder(skb, RTN_UNSPEC))
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070070 ret = NF_DROP;
71 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 return ret;
74}
75
Jan Engelhardt737535c2009-06-13 06:46:36 +020076/* The work comes in here from netfilter.c. */
77static unsigned int
78iptable_mangle_hook(unsigned int hook,
79 struct sk_buff *skb,
80 const struct net_device *in,
81 const struct net_device *out,
82 int (*okfn)(struct sk_buff *))
83{
84 if (hook == NF_INET_LOCAL_OUT)
Jan Engelhardtfa96a0e2009-11-01 00:36:40 +010085 return ipt_mangle_out(skb, out);
Alexey Dobriyanb2907e52010-02-11 18:41:35 +010086 if (hook == NF_INET_POST_ROUTING)
87 return ipt_do_table(skb, hook, in, out,
88 dev_net(out)->ipv4.iptable_mangle);
Jan Engelhardt737535c2009-06-13 06:46:36 +020089 /* PREROUTING/INPUT/FORWARD: */
90 return ipt_do_table(skb, hook, in, out,
91 dev_net(in)->ipv4.iptable_mangle);
92}
93
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020094static struct nf_hook_ops *mangle_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Alexey Dobriyan9335f042008-01-31 04:03:23 -080096static int __net_init iptable_mangle_net_init(struct net *net)
97{
Jan Engelhardte3eaa992009-06-17 22:14:54 +020098 struct ipt_replace *repl;
99
100 repl = ipt_alloc_initial_table(&packet_mangler);
101 if (repl == NULL)
102 return -ENOMEM;
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800103 net->ipv4.iptable_mangle =
Jan Engelhardte3eaa992009-06-17 22:14:54 +0200104 ipt_register_table(net, &packet_mangler, repl);
105 kfree(repl);
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800106 if (IS_ERR(net->ipv4.iptable_mangle))
107 return PTR_ERR(net->ipv4.iptable_mangle);
108 return 0;
109}
110
111static void __net_exit iptable_mangle_net_exit(struct net *net)
112{
Alexey Dobriyanf54e9362010-01-18 08:25:47 +0100113 ipt_unregister_table(net, net->ipv4.iptable_mangle);
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800114}
115
116static struct pernet_operations iptable_mangle_net_ops = {
117 .init = iptable_mangle_net_init,
118 .exit = iptable_mangle_net_exit,
119};
120
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800121static int __init iptable_mangle_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 int ret;
124
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800125 ret = register_pernet_subsys(&iptable_mangle_net_ops);
126 if (ret < 0)
127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* Register hooks */
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200130 mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
131 if (IS_ERR(mangle_ops)) {
132 ret = PTR_ERR(mangle_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto cleanup_table;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return ret;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 cleanup_table:
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800139 unregister_pernet_subsys(&iptable_mangle_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return ret;
141}
142
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800143static void __exit iptable_mangle_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200145 xt_hook_unlink(&packet_mangler, mangle_ops);
Alexey Dobriyan9335f042008-01-31 04:03:23 -0800146 unregister_pernet_subsys(&iptable_mangle_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800149module_init(iptable_mangle_init);
150module_exit(iptable_mangle_fini);