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 */ |
| 31 | static struct |
| 32 | { |
| 33 | struct ipt_replace repl; |
| 34 | struct ipt_standard entries[5]; |
| 35 | struct ipt_error term; |
Patrick McHardy | 3c2ad46 | 2007-05-10 14:14:16 -0700 | [diff] [blame] | 36 | } initial_table __initdata = { |
| 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 | e60a13e | 2007-02-07 15:12:33 -0800 | [diff] [blame] | 67 | static 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, |
| 70 | .lock = RW_LOCK_UNLOCKED, |
| 71 | .me = THIS_MODULE, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 72 | .af = AF_INET, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /* The work comes in here from netfilter.c. */ |
| 76 | static unsigned int |
| 77 | ipt_route_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 78 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | const struct net_device *in, |
| 80 | const struct net_device *out, |
| 81 | int (*okfn)(struct sk_buff *)) |
| 82 | { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 83 | return ipt_do_table(skb, hook, in, out, &packet_mangler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static unsigned int |
| 87 | ipt_local_hook(unsigned int hook, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 88 | struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | const struct net_device *in, |
| 90 | const struct net_device *out, |
| 91 | int (*okfn)(struct sk_buff *)) |
| 92 | { |
| 93 | unsigned int ret; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 94 | const struct iphdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | u_int8_t tos; |
Al Viro | 6a19d61 | 2006-09-28 14:22:24 -0700 | [diff] [blame] | 96 | __be32 saddr, daddr; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 97 | u_int32_t mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 100 | if (skb->len < sizeof(struct iphdr) |
| 101 | || ip_hdrlen(skb) < sizeof(struct iphdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (net_ratelimit()) |
Patrick McHardy | 4a176c1 | 2007-05-10 14:16:56 -0700 | [diff] [blame] | 103 | printk("iptable_mangle: ignoring short SOCK_RAW " |
| 104 | "packet.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return NF_ACCEPT; |
| 106 | } |
| 107 | |
| 108 | /* Save things which could affect route */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 109 | mark = skb->mark; |
| 110 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 111 | saddr = iph->saddr; |
| 112 | daddr = iph->daddr; |
| 113 | tos = iph->tos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 115 | ret = ipt_do_table(skb, hook, in, out, &packet_mangler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* Reroute for ANY change. */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 117 | if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 118 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 119 | |
| 120 | if (iph->saddr != saddr || |
| 121 | iph->daddr != daddr || |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 122 | skb->mark != mark || |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 123 | iph->tos != tos) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 124 | if (ip_route_me_harder(skb, RTN_UNSPEC)) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 125 | ret = NF_DROP; |
| 126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | static struct nf_hook_ops ipt_ops[] = { |
| 132 | { |
| 133 | .hook = ipt_route_hook, |
| 134 | .owner = THIS_MODULE, |
| 135 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 136 | .hooknum = NF_INET_PRE_ROUTING, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | .priority = NF_IP_PRI_MANGLE, |
| 138 | }, |
| 139 | { |
| 140 | .hook = ipt_route_hook, |
| 141 | .owner = THIS_MODULE, |
| 142 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 143 | .hooknum = NF_INET_LOCAL_IN, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | .priority = NF_IP_PRI_MANGLE, |
| 145 | }, |
| 146 | { |
| 147 | .hook = ipt_route_hook, |
| 148 | .owner = THIS_MODULE, |
| 149 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 150 | .hooknum = NF_INET_FORWARD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | .priority = NF_IP_PRI_MANGLE, |
| 152 | }, |
| 153 | { |
| 154 | .hook = ipt_local_hook, |
| 155 | .owner = THIS_MODULE, |
| 156 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 157 | .hooknum = NF_INET_LOCAL_OUT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | .priority = NF_IP_PRI_MANGLE, |
| 159 | }, |
| 160 | { |
| 161 | .hook = ipt_route_hook, |
| 162 | .owner = THIS_MODULE, |
| 163 | .pf = PF_INET, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame^] | 164 | .hooknum = NF_INET_POST_ROUTING, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | .priority = NF_IP_PRI_MANGLE, |
| 166 | }, |
| 167 | }; |
| 168 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 169 | static int __init iptable_mangle_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
| 171 | int ret; |
| 172 | |
| 173 | /* Register table */ |
| 174 | ret = ipt_register_table(&packet_mangler, &initial_table.repl); |
| 175 | if (ret < 0) |
| 176 | return ret; |
| 177 | |
| 178 | /* Register hooks */ |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 179 | ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (ret < 0) |
| 181 | goto cleanup_table; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return ret; |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | cleanup_table: |
| 186 | ipt_unregister_table(&packet_mangler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return ret; |
| 188 | } |
| 189 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 190 | static void __exit iptable_mangle_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 192 | nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | ipt_unregister_table(&packet_mangler); |
| 194 | } |
| 195 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 196 | module_init(iptable_mangle_init); |
| 197 | module_exit(iptable_mangle_fini); |