1
2
3
4
5
6
7
8
9
10
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 <linux/slab.h>
16#include <net/sock.h>
17#include <net/route.h>
18#include <linux/ip.h>
19#include <net/ip.h>
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
23MODULE_DESCRIPTION("iptables mangle table");
24
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))
30
31static int __net_init iptable_mangle_table_init(struct net *net);
32
33static const struct xt_table packet_mangler = {
34 .name = "mangle",
35 .valid_hooks = MANGLE_VALID_HOOKS,
36 .me = THIS_MODULE,
37 .af = NFPROTO_IPV4,
38 .priority = NF_IP_PRI_MANGLE,
39 .table_init = iptable_mangle_table_init,
40};
41
42static unsigned int
43ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
44{
45 unsigned int ret;
46 const struct iphdr *iph;
47 u_int8_t tos;
48 __be32 saddr, daddr;
49 u_int32_t mark;
50 int err;
51
52
53 if (skb->len < sizeof(struct iphdr) ||
54 ip_hdrlen(skb) < sizeof(struct iphdr))
55 return NF_ACCEPT;
56
57
58 mark = skb->mark;
59 iph = ip_hdr(skb);
60 saddr = iph->saddr;
61 daddr = iph->daddr;
62 tos = iph->tos;
63
64 ret = ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
65
66 if (ret != NF_DROP && ret != NF_STOLEN) {
67 iph = ip_hdr(skb);
68
69 if (iph->saddr != saddr ||
70 iph->daddr != daddr ||
71 skb->mark != mark ||
72 iph->tos != tos) {
73 err = ip_route_me_harder(state->net, skb, RTN_UNSPEC);
74 if (err < 0)
75 ret = NF_DROP_ERR(err);
76 }
77 }
78
79 return ret;
80}
81
82
83static unsigned int
84iptable_mangle_hook(void *priv,
85 struct sk_buff *skb,
86 const struct nf_hook_state *state)
87{
88 if (state->hook == NF_INET_LOCAL_OUT)
89 return ipt_mangle_out(skb, state);
90 if (state->hook == NF_INET_POST_ROUTING)
91 return ipt_do_table(skb, state,
92 state->net->ipv4.iptable_mangle);
93
94 return ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
95}
96
97static struct nf_hook_ops *mangle_ops __read_mostly;
98static int __net_init iptable_mangle_table_init(struct net *net)
99{
100 struct ipt_replace *repl;
101 int ret;
102
103 if (net->ipv4.iptable_mangle)
104 return 0;
105
106 repl = ipt_alloc_initial_table(&packet_mangler);
107 if (repl == NULL)
108 return -ENOMEM;
109 ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops,
110 &net->ipv4.iptable_mangle);
111 kfree(repl);
112 return ret;
113}
114
115static void __net_exit iptable_mangle_net_exit(struct net *net)
116{
117 if (!net->ipv4.iptable_mangle)
118 return;
119 ipt_unregister_table(net, net->ipv4.iptable_mangle, mangle_ops);
120 net->ipv4.iptable_mangle = NULL;
121}
122
123static struct pernet_operations iptable_mangle_net_ops = {
124 .exit = iptable_mangle_net_exit,
125};
126
127static int __init iptable_mangle_init(void)
128{
129 int ret;
130
131 mangle_ops = xt_hook_ops_alloc(&packet_mangler, iptable_mangle_hook);
132 if (IS_ERR(mangle_ops)) {
133 ret = PTR_ERR(mangle_ops);
134 return ret;
135 }
136
137 ret = register_pernet_subsys(&iptable_mangle_net_ops);
138 if (ret < 0) {
139 kfree(mangle_ops);
140 return ret;
141 }
142
143 ret = iptable_mangle_table_init(&init_net);
144 if (ret) {
145 unregister_pernet_subsys(&iptable_mangle_net_ops);
146 kfree(mangle_ops);
147 }
148
149 return ret;
150}
151
152static void __exit iptable_mangle_fini(void)
153{
154 unregister_pernet_subsys(&iptable_mangle_net_ops);
155 kfree(mangle_ops);
156}
157
158module_init(iptable_mangle_init);
159module_exit(iptable_mangle_fini);
160