1
2
3
4
5
6
7
8
9
10
11#include <linux/module.h>
12#include <linux/netfilter_ipv6/ip6_tables.h>
13#include <linux/slab.h>
14#include <net/ipv6.h>
15
16MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
18MODULE_DESCRIPTION("ip6tables mangle table");
19
20#define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
21 (1 << NF_INET_LOCAL_IN) | \
22 (1 << NF_INET_FORWARD) | \
23 (1 << NF_INET_LOCAL_OUT) | \
24 (1 << NF_INET_POST_ROUTING))
25
26static int __net_init ip6table_mangle_table_init(struct net *net);
27
28static const struct xt_table packet_mangler = {
29 .name = "mangle",
30 .valid_hooks = MANGLE_VALID_HOOKS,
31 .me = THIS_MODULE,
32 .af = NFPROTO_IPV6,
33 .priority = NF_IP6_PRI_MANGLE,
34 .table_init = ip6table_mangle_table_init,
35};
36
37static unsigned int
38ip6t_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
39{
40 unsigned int ret;
41 struct in6_addr saddr, daddr;
42 u_int8_t hop_limit;
43 u_int32_t flowlabel, mark;
44 int err;
45#if 0
46
47 if (skb->len < sizeof(struct iphdr) ||
48 ip_hdrlen(skb) < sizeof(struct iphdr)) {
49 net_warn_ratelimited("ip6t_hook: happy cracking\n");
50 return NF_ACCEPT;
51 }
52#endif
53
54
55 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
56 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
57 mark = skb->mark;
58 hop_limit = ipv6_hdr(skb)->hop_limit;
59
60
61 flowlabel = *((u_int32_t *)ipv6_hdr(skb));
62
63 ret = ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
64
65 if (ret != NF_DROP && ret != NF_STOLEN &&
66 (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
67 !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
68 skb->mark != mark ||
69 ipv6_hdr(skb)->hop_limit != hop_limit ||
70 flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
71 err = ip6_route_me_harder(state->net, skb);
72 if (err < 0)
73 ret = NF_DROP_ERR(err);
74 }
75
76 return ret;
77}
78
79
80static unsigned int
81ip6table_mangle_hook(void *priv, struct sk_buff *skb,
82 const struct nf_hook_state *state)
83{
84 if (state->hook == NF_INET_LOCAL_OUT)
85 return ip6t_mangle_out(skb, state);
86 return ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
87}
88
89static struct nf_hook_ops *mangle_ops __read_mostly;
90static int __net_init ip6table_mangle_table_init(struct net *net)
91{
92 struct ip6t_replace *repl;
93 int ret;
94
95 if (net->ipv6.ip6table_mangle)
96 return 0;
97
98 repl = ip6t_alloc_initial_table(&packet_mangler);
99 if (repl == NULL)
100 return -ENOMEM;
101 ret = ip6t_register_table(net, &packet_mangler, repl, mangle_ops,
102 &net->ipv6.ip6table_mangle);
103 kfree(repl);
104 return ret;
105}
106
107static void __net_exit ip6table_mangle_net_exit(struct net *net)
108{
109 if (!net->ipv6.ip6table_mangle)
110 return;
111
112 ip6t_unregister_table(net, net->ipv6.ip6table_mangle, mangle_ops);
113 net->ipv6.ip6table_mangle = NULL;
114}
115
116static struct pernet_operations ip6table_mangle_net_ops = {
117 .exit = ip6table_mangle_net_exit,
118};
119
120static int __init ip6table_mangle_init(void)
121{
122 int ret;
123
124 mangle_ops = xt_hook_ops_alloc(&packet_mangler, ip6table_mangle_hook);
125 if (IS_ERR(mangle_ops))
126 return PTR_ERR(mangle_ops);
127
128 ret = register_pernet_subsys(&ip6table_mangle_net_ops);
129 if (ret < 0) {
130 kfree(mangle_ops);
131 return ret;
132 }
133
134 ret = ip6table_mangle_table_init(&init_net);
135 if (ret) {
136 unregister_pernet_subsys(&ip6table_mangle_net_ops);
137 kfree(mangle_ops);
138 }
139 return ret;
140}
141
142static void __exit ip6table_mangle_fini(void)
143{
144 unregister_pernet_subsys(&ip6table_mangle_net_ops);
145 kfree(mangle_ops);
146}
147
148module_init(ip6table_mangle_init);
149module_exit(ip6table_mangle_fini);
150