Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 13 | #include <linux/netdevice.h> |
| 14 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <net/sock.h> |
| 17 | #include <net/route.h> |
| 18 | #include <linux/ip.h> |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 19 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | MODULE_LICENSE("GPL"); |
| 22 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 23 | MODULE_DESCRIPTION("iptables mangle table"); |
| 24 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 25 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 31 | static int __net_init iptable_mangle_table_init(struct net *net); |
| 32 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 33 | static const struct xt_table packet_mangler = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | .name = "mangle", |
| 35 | .valid_hooks = MANGLE_VALID_HOOKS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | .me = THIS_MODULE, |
Jan Engelhardt | f88e6a8 | 2009-06-13 06:25:44 +0200 | [diff] [blame] | 37 | .af = NFPROTO_IPV4, |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 38 | .priority = NF_IP_PRI_MANGLE, |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 39 | .table_init = iptable_mangle_table_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static unsigned int |
David S. Miller | 1c491ba | 2015-04-03 20:56:08 -0400 | [diff] [blame] | 43 | ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | { |
| 45 | unsigned int ret; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 46 | const struct iphdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | u_int8_t tos; |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 48 | __be32 saddr, daddr; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 49 | u_int32_t mark; |
Patrick McHardy | c9e1673 | 2013-04-05 06:41:10 +0000 | [diff] [blame] | 50 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* root is playing with raw sockets. */ |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 53 | if (skb->len < sizeof(struct iphdr) || |
| 54 | ip_hdrlen(skb) < sizeof(struct iphdr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* Save things which could affect route */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 58 | mark = skb->mark; |
| 59 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 60 | saddr = iph->saddr; |
| 61 | daddr = iph->daddr; |
| 62 | tos = iph->tos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Eric W. Biederman | 6cb8ff3f1 | 2015-09-18 14:32:55 -0500 | [diff] [blame] | 64 | ret = ipt_do_table(skb, state, state->net->ipv4.iptable_mangle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* Reroute for ANY change. */ |
Florian Westphal | 28a51ba | 2011-01-20 10:23:26 +0100 | [diff] [blame] | 66 | if (ret != NF_DROP && ret != NF_STOLEN) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 67 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 68 | |
| 69 | if (iph->saddr != saddr || |
| 70 | iph->daddr != daddr || |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 71 | skb->mark != mark || |
Patrick McHardy | c9e1673 | 2013-04-05 06:41:10 +0000 | [diff] [blame] | 72 | iph->tos != tos) { |
Eric W. Biederman | e45f506 | 2015-09-25 15:07:30 -0500 | [diff] [blame] | 73 | err = ip_route_me_harder(state->net, skb, RTN_UNSPEC); |
Patrick McHardy | c9e1673 | 2013-04-05 06:41:10 +0000 | [diff] [blame] | 74 | if (err < 0) |
| 75 | ret = NF_DROP_ERR(err); |
| 76 | } |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 77 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 82 | /* The work comes in here from netfilter.c. */ |
| 83 | static unsigned int |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 84 | iptable_mangle_hook(void *priv, |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 85 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 86 | const struct nf_hook_state *state) |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 87 | { |
Eric W. Biederman | 6cb8ff3f1 | 2015-09-18 14:32:55 -0500 | [diff] [blame] | 88 | if (state->hook == NF_INET_LOCAL_OUT) |
David S. Miller | 1c491ba | 2015-04-03 20:56:08 -0400 | [diff] [blame] | 89 | return ipt_mangle_out(skb, state); |
Eric W. Biederman | 6cb8ff3f1 | 2015-09-18 14:32:55 -0500 | [diff] [blame] | 90 | return ipt_do_table(skb, state, state->net->ipv4.iptable_mangle); |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 93 | static struct nf_hook_ops *mangle_ops __read_mostly; |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 94 | static int __net_init iptable_mangle_table_init(struct net *net) |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 95 | { |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 96 | struct ipt_replace *repl; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 97 | int ret; |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 98 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 99 | if (net->ipv4.iptable_mangle) |
| 100 | return 0; |
| 101 | |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 102 | repl = ipt_alloc_initial_table(&packet_mangler); |
| 103 | if (repl == NULL) |
| 104 | return -ENOMEM; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 105 | ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops, |
| 106 | &net->ipv4.iptable_mangle); |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 107 | kfree(repl); |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 108 | return ret; |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static void __net_exit iptable_mangle_net_exit(struct net *net) |
| 112 | { |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 113 | if (!net->ipv4.iptable_mangle) |
| 114 | return; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 115 | ipt_unregister_table(net, net->ipv4.iptable_mangle, mangle_ops); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 116 | net->ipv4.iptable_mangle = NULL; |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static struct pernet_operations iptable_mangle_net_ops = { |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 120 | .exit = iptable_mangle_net_exit, |
| 121 | }; |
| 122 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 123 | static int __init iptable_mangle_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | int ret; |
| 126 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 127 | mangle_ops = xt_hook_ops_alloc(&packet_mangler, iptable_mangle_hook); |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 128 | if (IS_ERR(mangle_ops)) { |
| 129 | ret = PTR_ERR(mangle_ops); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | ret = register_pernet_subsys(&iptable_mangle_net_ops); |
| 134 | if (ret < 0) { |
| 135 | kfree(mangle_ops); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | ret = iptable_mangle_table_init(&init_net); |
| 140 | if (ret) { |
Jean Sacren | 90efbed | 2012-08-19 15:11:32 +0000 | [diff] [blame] | 141 | unregister_pernet_subsys(&iptable_mangle_net_ops); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 142 | kfree(mangle_ops); |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 148 | static void __exit iptable_mangle_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 150 | unregister_pernet_subsys(&iptable_mangle_net_ops); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 151 | kfree(mangle_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 154 | module_init(iptable_mangle_init); |
| 155 | module_exit(iptable_mangle_fini); |