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