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 | |
| 30 | /* Ouch - five different hooks? Maybe this should be a config option..... -- BC */ |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame^] | 31 | static const struct |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | struct ipt_replace repl; |
| 34 | struct ipt_standard entries[5]; |
| 35 | struct ipt_error term; |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 36 | } initial_table __net_initdata = { |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 37 | .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 McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 43 | [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 McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 48 | }, |
| 49 | .underflow = { |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 50 | [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 McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 55 | }, |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame^] | 67 | static const struct xt_table packet_mangler = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | .name = "mangle", |
| 69 | .valid_hooks = MANGLE_VALID_HOOKS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | .me = THIS_MODULE, |
Jan Engelhardt | f88e6a8 | 2009-06-13 06:25:44 +0200 | [diff] [blame] | 71 | .af = NFPROTO_IPV4, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /* The work comes in here from netfilter.c. */ |
| 75 | static unsigned int |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 76 | ipt_pre_routing_hook(unsigned int hook, |
| 77 | struct sk_buff *skb, |
| 78 | const struct net_device *in, |
| 79 | const struct net_device *out, |
| 80 | int (*okfn)(struct sk_buff *)) |
| 81 | { |
| 82 | return ipt_do_table(skb, hook, in, out, |
Alexey Dobriyan | 48dc786 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 83 | dev_net(in)->ipv4.iptable_mangle); |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static unsigned int |
| 87 | ipt_post_routing_hook(unsigned int hook, |
| 88 | struct sk_buff *skb, |
| 89 | const struct net_device *in, |
| 90 | const struct net_device *out, |
| 91 | int (*okfn)(struct sk_buff *)) |
| 92 | { |
| 93 | return ipt_do_table(skb, hook, in, out, |
Alexey Dobriyan | 48dc786 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 94 | dev_net(out)->ipv4.iptable_mangle); |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static unsigned int |
| 98 | ipt_local_in_hook(unsigned int hook, |
| 99 | struct sk_buff *skb, |
| 100 | const struct net_device *in, |
| 101 | const struct net_device *out, |
| 102 | int (*okfn)(struct sk_buff *)) |
| 103 | { |
| 104 | return ipt_do_table(skb, hook, in, out, |
Alexey Dobriyan | 48dc786 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 105 | dev_net(in)->ipv4.iptable_mangle); |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static unsigned int |
| 109 | ipt_forward_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 110 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | const struct net_device *in, |
| 112 | const struct net_device *out, |
| 113 | int (*okfn)(struct sk_buff *)) |
| 114 | { |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 115 | return ipt_do_table(skb, hook, in, out, |
Alexey Dobriyan | 48dc786 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 116 | dev_net(in)->ipv4.iptable_mangle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static unsigned int |
| 120 | ipt_local_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 121 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | const struct net_device *in, |
| 123 | const struct net_device *out, |
| 124 | int (*okfn)(struct sk_buff *)) |
| 125 | { |
| 126 | unsigned int ret; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 127 | const struct iphdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | u_int8_t tos; |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 129 | __be32 saddr, daddr; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 130 | u_int32_t mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 133 | if (skb->len < sizeof(struct iphdr) |
Patrick McHardy | 8884310 | 2009-01-12 00:06:00 +0000 | [diff] [blame] | 134 | || ip_hdrlen(skb) < sizeof(struct iphdr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return NF_ACCEPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | /* Save things which could affect route */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 138 | mark = skb->mark; |
| 139 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 140 | saddr = iph->saddr; |
| 141 | daddr = iph->daddr; |
| 142 | tos = iph->tos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 144 | ret = ipt_do_table(skb, hook, in, out, |
Alexey Dobriyan | 48dc786 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 145 | dev_net(out)->ipv4.iptable_mangle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /* Reroute for ANY change. */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 147 | if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 148 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 149 | |
| 150 | if (iph->saddr != saddr || |
| 151 | iph->daddr != daddr || |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 152 | skb->mark != mark || |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 153 | iph->tos != tos) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 154 | if (ip_route_me_harder(skb, RTN_UNSPEC)) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 155 | ret = NF_DROP; |
| 156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | return ret; |
| 159 | } |
| 160 | |
Patrick McHardy | 1999414 | 2007-12-05 01:23:00 -0800 | [diff] [blame] | 161 | static struct nf_hook_ops ipt_ops[] __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 163 | .hook = ipt_pre_routing_hook, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | .owner = THIS_MODULE, |
Jan Engelhardt | 24c232d | 2009-06-13 06:20:29 +0200 | [diff] [blame] | 165 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 166 | .hooknum = NF_INET_PRE_ROUTING, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | .priority = NF_IP_PRI_MANGLE, |
| 168 | }, |
| 169 | { |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 170 | .hook = ipt_local_in_hook, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | .owner = THIS_MODULE, |
Jan Engelhardt | 24c232d | 2009-06-13 06:20:29 +0200 | [diff] [blame] | 172 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 173 | .hooknum = NF_INET_LOCAL_IN, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | .priority = NF_IP_PRI_MANGLE, |
| 175 | }, |
| 176 | { |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 177 | .hook = ipt_forward_hook, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | .owner = THIS_MODULE, |
Jan Engelhardt | 24c232d | 2009-06-13 06:20:29 +0200 | [diff] [blame] | 179 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 180 | .hooknum = NF_INET_FORWARD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | .priority = NF_IP_PRI_MANGLE, |
| 182 | }, |
| 183 | { |
| 184 | .hook = ipt_local_hook, |
| 185 | .owner = THIS_MODULE, |
Jan Engelhardt | 24c232d | 2009-06-13 06:20:29 +0200 | [diff] [blame] | 186 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 187 | .hooknum = NF_INET_LOCAL_OUT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | .priority = NF_IP_PRI_MANGLE, |
| 189 | }, |
| 190 | { |
Alexey Dobriyan | 666953df | 2008-04-14 09:56:02 +0200 | [diff] [blame] | 191 | .hook = ipt_post_routing_hook, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | .owner = THIS_MODULE, |
Jan Engelhardt | 24c232d | 2009-06-13 06:20:29 +0200 | [diff] [blame] | 193 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 194 | .hooknum = NF_INET_POST_ROUTING, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | .priority = NF_IP_PRI_MANGLE, |
| 196 | }, |
| 197 | }; |
| 198 | |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 199 | static int __net_init iptable_mangle_net_init(struct net *net) |
| 200 | { |
| 201 | /* Register table */ |
| 202 | net->ipv4.iptable_mangle = |
| 203 | ipt_register_table(net, &packet_mangler, &initial_table.repl); |
| 204 | if (IS_ERR(net->ipv4.iptable_mangle)) |
| 205 | return PTR_ERR(net->ipv4.iptable_mangle); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static void __net_exit iptable_mangle_net_exit(struct net *net) |
| 210 | { |
| 211 | ipt_unregister_table(net->ipv4.iptable_mangle); |
| 212 | } |
| 213 | |
| 214 | static struct pernet_operations iptable_mangle_net_ops = { |
| 215 | .init = iptable_mangle_net_init, |
| 216 | .exit = iptable_mangle_net_exit, |
| 217 | }; |
| 218 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 219 | static int __init iptable_mangle_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | int ret; |
| 222 | |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 223 | ret = register_pernet_subsys(&iptable_mangle_net_ops); |
| 224 | if (ret < 0) |
| 225 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | /* Register hooks */ |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 228 | ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | if (ret < 0) |
| 230 | goto cleanup_table; |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | return ret; |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | cleanup_table: |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 235 | unregister_pernet_subsys(&iptable_mangle_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return ret; |
| 237 | } |
| 238 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 239 | static void __exit iptable_mangle_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 241 | nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
Alexey Dobriyan | 9335f04 | 2008-01-31 04:03:23 -0800 | [diff] [blame] | 242 | unregister_pernet_subsys(&iptable_mangle_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 245 | module_init(iptable_mangle_init); |
| 246 | module_exit(iptable_mangle_fini); |