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> |
| 15 | #include <net/sock.h> |
| 16 | #include <net/route.h> |
| 17 | #include <linux/ip.h> |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 18 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | MODULE_LICENSE("GPL"); |
| 21 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 22 | MODULE_DESCRIPTION("iptables mangle table"); |
| 23 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 24 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 30 | static const struct xt_table packet_mangler = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | .name = "mangle", |
| 32 | .valid_hooks = MANGLE_VALID_HOOKS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | .me = THIS_MODULE, |
Jan Engelhardt | f88e6a8 | 2009-06-13 06:25:44 +0200 | [diff] [blame] | 34 | .af = NFPROTO_IPV4, |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 35 | .priority = NF_IP_PRI_MANGLE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static unsigned int |
Jan Engelhardt | fa96a0e | 2009-11-01 00:36:40 +0100 | [diff] [blame^] | 39 | ipt_mangle_out(struct sk_buff *skb, const struct net_device *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
| 41 | unsigned int ret; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 42 | const struct iphdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | u_int8_t tos; |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 44 | __be32 saddr, daddr; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 45 | u_int32_t mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* root is playing with raw sockets. */ |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 48 | if (skb->len < sizeof(struct iphdr) || |
| 49 | ip_hdrlen(skb) < sizeof(struct iphdr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* Save things which could affect route */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 53 | mark = skb->mark; |
| 54 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 55 | saddr = iph->saddr; |
| 56 | daddr = iph->daddr; |
| 57 | tos = iph->tos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Jan Engelhardt | fa96a0e | 2009-11-01 00:36:40 +0100 | [diff] [blame^] | 59 | ret = ipt_do_table(skb, NF_INET_LOCAL_OUT, NULL, out, |
Alexey Dobriyan | 48dc786 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 60 | dev_net(out)->ipv4.iptable_mangle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | /* Reroute for ANY change. */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 62 | if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 63 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 64 | |
| 65 | if (iph->saddr != saddr || |
| 66 | iph->daddr != daddr || |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 67 | skb->mark != mark || |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 68 | iph->tos != tos) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 69 | if (ip_route_me_harder(skb, RTN_UNSPEC)) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 70 | ret = NF_DROP; |
| 71 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 76 | /* The work comes in here from netfilter.c. */ |
| 77 | static unsigned int |
| 78 | iptable_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 Engelhardt | fa96a0e | 2009-11-01 00:36:40 +0100 | [diff] [blame^] | 85 | return ipt_mangle_out(skb, out); |
Alexey Dobriyan | b2907e5 | 2010-02-11 18:41:35 +0100 | [diff] [blame] | 86 | if (hook == NF_INET_POST_ROUTING) |
| 87 | return ipt_do_table(skb, hook, in, out, |
| 88 | dev_net(out)->ipv4.iptable_mangle); |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 89 | /* PREROUTING/INPUT/FORWARD: */ |
| 90 | return ipt_do_table(skb, hook, in, out, |
| 91 | dev_net(in)->ipv4.iptable_mangle); |
| 92 | } |
| 93 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 94 | static struct nf_hook_ops *mangle_ops __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 96 | static int __net_init iptable_mangle_net_init(struct net *net) |
| 97 | { |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 98 | struct ipt_replace *repl; |
| 99 | |
| 100 | repl = ipt_alloc_initial_table(&packet_mangler); |
| 101 | if (repl == NULL) |
| 102 | return -ENOMEM; |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 103 | net->ipv4.iptable_mangle = |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 104 | ipt_register_table(net, &packet_mangler, repl); |
| 105 | kfree(repl); |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 106 | if (IS_ERR(net->ipv4.iptable_mangle)) |
| 107 | return PTR_ERR(net->ipv4.iptable_mangle); |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static void __net_exit iptable_mangle_net_exit(struct net *net) |
| 112 | { |
Alexey Dobriyan | f54e936 | 2010-01-18 08:25:47 +0100 | [diff] [blame] | 113 | ipt_unregister_table(net, net->ipv4.iptable_mangle); |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static struct pernet_operations iptable_mangle_net_ops = { |
| 117 | .init = iptable_mangle_net_init, |
| 118 | .exit = iptable_mangle_net_exit, |
| 119 | }; |
| 120 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 121 | static int __init iptable_mangle_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | int ret; |
| 124 | |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 125 | ret = register_pernet_subsys(&iptable_mangle_net_ops); |
| 126 | if (ret < 0) |
| 127 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | /* Register hooks */ |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 130 | mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook); |
| 131 | if (IS_ERR(mangle_ops)) { |
| 132 | ret = PTR_ERR(mangle_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | goto cleanup_table; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return ret; |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | cleanup_table: |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 139 | unregister_pernet_subsys(&iptable_mangle_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | return ret; |
| 141 | } |
| 142 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 143 | static void __exit iptable_mangle_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 145 | xt_hook_unlink(&packet_mangler, mangle_ops); |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 146 | unregister_pernet_subsys(&iptable_mangle_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 149 | module_init(iptable_mangle_init); |
| 150 | module_exit(iptable_mangle_fini); |