]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
1da177e4 | 10 | */ |
1da177e4 LT |
11 | #include <linux/module.h> |
12 | #include <linux/netfilter_ipv4/ip_tables.h> | |
13 | #include <linux/netdevice.h> | |
14 | #include <linux/skbuff.h> | |
5a0e3ad6 | 15 | #include <linux/slab.h> |
1da177e4 LT |
16 | #include <net/sock.h> |
17 | #include <net/route.h> | |
18 | #include <linux/ip.h> | |
c9bdd4b5 | 19 | #include <net/ip.h> |
1da177e4 LT |
20 | |
21 | MODULE_LICENSE("GPL"); | |
22 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
23 | MODULE_DESCRIPTION("iptables mangle table"); | |
24 | ||
6e23ae2a PM |
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)) | |
1da177e4 | 30 | |
b9e69e12 FW |
31 | static int __net_init iptable_mangle_table_init(struct net *net); |
32 | ||
35aad0ff | 33 | static const struct xt_table packet_mangler = { |
1da177e4 LT |
34 | .name = "mangle", |
35 | .valid_hooks = MANGLE_VALID_HOOKS, | |
1da177e4 | 36 | .me = THIS_MODULE, |
f88e6a8a | 37 | .af = NFPROTO_IPV4, |
2b95efe7 | 38 | .priority = NF_IP_PRI_MANGLE, |
b9e69e12 | 39 | .table_init = iptable_mangle_table_init, |
1da177e4 LT |
40 | }; |
41 | ||
1da177e4 | 42 | static unsigned int |
1c491ba2 | 43 | ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state) |
1da177e4 LT |
44 | { |
45 | unsigned int ret; | |
eddc9ec5 | 46 | const struct iphdr *iph; |
1da177e4 | 47 | u_int8_t tos; |
6a19d614 | 48 | __be32 saddr, daddr; |
82e91ffe | 49 | u_int32_t mark; |
c9e1673a | 50 | int err; |
1da177e4 | 51 | |
1da177e4 | 52 | /* Save things which could affect route */ |
3db05fea HX |
53 | mark = skb->mark; |
54 | iph = ip_hdr(skb); | |
eddc9ec5 ACM |
55 | saddr = iph->saddr; |
56 | daddr = iph->daddr; | |
57 | tos = iph->tos; | |
1da177e4 | 58 | |
6cb8ff3f | 59 | ret = ipt_do_table(skb, state, state->net->ipv4.iptable_mangle); |
1da177e4 | 60 | /* Reroute for ANY change. */ |
28a51ba5 | 61 | if (ret != NF_DROP && ret != NF_STOLEN) { |
3db05fea | 62 | iph = ip_hdr(skb); |
eddc9ec5 ACM |
63 | |
64 | if (iph->saddr != saddr || | |
65 | iph->daddr != daddr || | |
3db05fea | 66 | skb->mark != mark || |
c9e1673a | 67 | iph->tos != tos) { |
e45f5066 | 68 | err = ip_route_me_harder(state->net, skb, RTN_UNSPEC); |
c9e1673a PM |
69 | if (err < 0) |
70 | ret = NF_DROP_ERR(err); | |
71 | } | |
eddc9ec5 | 72 | } |
1da177e4 LT |
73 | |
74 | return ret; | |
75 | } | |
76 | ||
737535c5 JE |
77 | /* The work comes in here from netfilter.c. */ |
78 | static unsigned int | |
06198b34 | 79 | iptable_mangle_hook(void *priv, |
737535c5 | 80 | struct sk_buff *skb, |
238e54c9 | 81 | const struct nf_hook_state *state) |
737535c5 | 82 | { |
6cb8ff3f | 83 | if (state->hook == NF_INET_LOCAL_OUT) |
1c491ba2 | 84 | return ipt_mangle_out(skb, state); |
6cb8ff3f | 85 | return ipt_do_table(skb, state, state->net->ipv4.iptable_mangle); |
737535c5 JE |
86 | } |
87 | ||
2b95efe7 | 88 | static struct nf_hook_ops *mangle_ops __read_mostly; |
b9e69e12 | 89 | static int __net_init iptable_mangle_table_init(struct net *net) |
9335f047 | 90 | { |
e3eaa991 | 91 | struct ipt_replace *repl; |
a67dd266 | 92 | int ret; |
e3eaa991 | 93 | |
b9e69e12 FW |
94 | if (net->ipv4.iptable_mangle) |
95 | return 0; | |
96 | ||
e3eaa991 JE |
97 | repl = ipt_alloc_initial_table(&packet_mangler); |
98 | if (repl == NULL) | |
99 | return -ENOMEM; | |
a67dd266 FW |
100 | ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops, |
101 | &net->ipv4.iptable_mangle); | |
e3eaa991 | 102 | kfree(repl); |
a67dd266 | 103 | return ret; |
9335f047 AD |
104 | } |
105 | ||
106 | static void __net_exit iptable_mangle_net_exit(struct net *net) | |
107 | { | |
b9e69e12 FW |
108 | if (!net->ipv4.iptable_mangle) |
109 | return; | |
a67dd266 | 110 | ipt_unregister_table(net, net->ipv4.iptable_mangle, mangle_ops); |
b9e69e12 | 111 | net->ipv4.iptable_mangle = NULL; |
9335f047 AD |
112 | } |
113 | ||
114 | static struct pernet_operations iptable_mangle_net_ops = { | |
9335f047 AD |
115 | .exit = iptable_mangle_net_exit, |
116 | }; | |
117 | ||
65b4b4e8 | 118 | static int __init iptable_mangle_init(void) |
1da177e4 LT |
119 | { |
120 | int ret; | |
121 | ||
b9e69e12 FW |
122 | mangle_ops = xt_hook_ops_alloc(&packet_mangler, iptable_mangle_hook); |
123 | if (IS_ERR(mangle_ops)) { | |
124 | ret = PTR_ERR(mangle_ops); | |
125 | return ret; | |
126 | } | |
127 | ||
9335f047 | 128 | ret = register_pernet_subsys(&iptable_mangle_net_ops); |
b9e69e12 FW |
129 | if (ret < 0) { |
130 | kfree(mangle_ops); | |
9335f047 | 131 | return ret; |
b9e69e12 | 132 | } |
1da177e4 | 133 | |
b9e69e12 FW |
134 | ret = iptable_mangle_table_init(&init_net); |
135 | if (ret) { | |
90efbed1 | 136 | unregister_pernet_subsys(&iptable_mangle_net_ops); |
b9e69e12 | 137 | kfree(mangle_ops); |
2b95efe7 | 138 | } |
1da177e4 | 139 | |
1da177e4 | 140 | return ret; |
1da177e4 LT |
141 | } |
142 | ||
65b4b4e8 | 143 | static void __exit iptable_mangle_fini(void) |
1da177e4 | 144 | { |
9335f047 | 145 | unregister_pernet_subsys(&iptable_mangle_net_ops); |
b9e69e12 | 146 | kfree(mangle_ops); |
1da177e4 LT |
147 | } |
148 | ||
65b4b4e8 AM |
149 | module_init(iptable_mangle_init); |
150 | module_exit(iptable_mangle_fini); |