Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IPv6 packet mangling table, a port of the IPv4 mangle table to IPv6 | |
3 | * | |
4 | * Copyright (C) 2000-2001 by Harald Welte <laforge@gnumonks.org> | |
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. | |
10 | * | |
11 | * Extended to all five netfilter hooks by Brad Chapman & Harald Welte | |
12 | */ | |
13 | #include <linux/module.h> | |
14 | #include <linux/netfilter_ipv6/ip6_tables.h> | |
15 | ||
16 | MODULE_LICENSE("GPL"); | |
17 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
18 | MODULE_DESCRIPTION("ip6tables mangle table"); | |
19 | ||
20 | #define MANGLE_VALID_HOOKS ((1 << NF_IP6_PRE_ROUTING) | \ | |
21 | (1 << NF_IP6_LOCAL_IN) | \ | |
22 | (1 << NF_IP6_FORWARD) | \ | |
23 | (1 << NF_IP6_LOCAL_OUT) | \ | |
24 | (1 << NF_IP6_POST_ROUTING)) | |
25 | ||
26 | #if 0 | |
27 | #define DEBUGP(x, args...) printk(KERN_DEBUG x, ## args) | |
28 | #else | |
29 | #define DEBUGP(x, args...) | |
30 | #endif | |
31 | ||
1da177e4 LT |
32 | static struct |
33 | { | |
34 | struct ip6t_replace repl; | |
35 | struct ip6t_standard entries[5]; | |
36 | struct ip6t_error term; | |
37 | } initial_table __initdata | |
38 | = { { "mangle", MANGLE_VALID_HOOKS, 6, | |
39 | sizeof(struct ip6t_standard) * 5 + sizeof(struct ip6t_error), | |
40 | { [NF_IP6_PRE_ROUTING] = 0, | |
41 | [NF_IP6_LOCAL_IN] = sizeof(struct ip6t_standard), | |
42 | [NF_IP6_FORWARD] = sizeof(struct ip6t_standard) * 2, | |
43 | [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 3, | |
44 | [NF_IP6_POST_ROUTING] = sizeof(struct ip6t_standard) * 4}, | |
45 | { [NF_IP6_PRE_ROUTING] = 0, | |
46 | [NF_IP6_LOCAL_IN] = sizeof(struct ip6t_standard), | |
47 | [NF_IP6_FORWARD] = sizeof(struct ip6t_standard) * 2, | |
48 | [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 3, | |
49 | [NF_IP6_POST_ROUTING] = sizeof(struct ip6t_standard) * 4}, | |
50 | 0, NULL, { } }, | |
51 | { | |
52 | /* PRE_ROUTING */ | |
1ab1457c | 53 | { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 }, |
1da177e4 LT |
54 | 0, |
55 | sizeof(struct ip6t_entry), | |
56 | sizeof(struct ip6t_standard), | |
57 | 0, { 0, 0 }, { } }, | |
58 | { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } }, | |
59 | -NF_ACCEPT - 1 } }, | |
60 | /* LOCAL_IN */ | |
1ab1457c | 61 | { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 }, |
1da177e4 LT |
62 | 0, |
63 | sizeof(struct ip6t_entry), | |
64 | sizeof(struct ip6t_standard), | |
65 | 0, { 0, 0 }, { } }, | |
66 | { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } }, | |
67 | -NF_ACCEPT - 1 } }, | |
68 | /* FORWARD */ | |
1ab1457c | 69 | { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 }, |
1da177e4 LT |
70 | 0, |
71 | sizeof(struct ip6t_entry), | |
72 | sizeof(struct ip6t_standard), | |
73 | 0, { 0, 0 }, { } }, | |
74 | { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } }, | |
75 | -NF_ACCEPT - 1 } }, | |
76 | /* LOCAL_OUT */ | |
1ab1457c | 77 | { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 }, |
1da177e4 LT |
78 | 0, |
79 | sizeof(struct ip6t_entry), | |
80 | sizeof(struct ip6t_standard), | |
81 | 0, { 0, 0 }, { } }, | |
82 | { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } }, | |
83 | -NF_ACCEPT - 1 } }, | |
84 | /* POST_ROUTING */ | |
85 | { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 }, | |
86 | 0, | |
87 | sizeof(struct ip6t_entry), | |
88 | sizeof(struct ip6t_standard), | |
89 | 0, { 0, 0 }, { } }, | |
90 | { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } }, | |
91 | -NF_ACCEPT - 1 } } | |
92 | }, | |
93 | /* ERROR */ | |
94 | { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 }, | |
95 | 0, | |
96 | sizeof(struct ip6t_entry), | |
97 | sizeof(struct ip6t_error), | |
98 | 0, { 0, 0 }, { } }, | |
99 | { { { { IP6T_ALIGN(sizeof(struct ip6t_error_target)), IP6T_ERROR_TARGET } }, | |
100 | { } }, | |
101 | "ERROR" | |
102 | } | |
103 | } | |
104 | }; | |
105 | ||
e60a13e0 | 106 | static struct xt_table packet_mangler = { |
1da177e4 LT |
107 | .name = "mangle", |
108 | .valid_hooks = MANGLE_VALID_HOOKS, | |
109 | .lock = RW_LOCK_UNLOCKED, | |
110 | .me = THIS_MODULE, | |
2e4e6a17 | 111 | .af = AF_INET6, |
1da177e4 LT |
112 | }; |
113 | ||
114 | /* The work comes in here from netfilter.c. */ | |
115 | static unsigned int | |
116 | ip6t_route_hook(unsigned int hook, | |
117 | struct sk_buff **pskb, | |
118 | const struct net_device *in, | |
119 | const struct net_device *out, | |
120 | int (*okfn)(struct sk_buff *)) | |
121 | { | |
fe1cb108 | 122 | return ip6t_do_table(pskb, hook, in, out, &packet_mangler); |
1da177e4 LT |
123 | } |
124 | ||
125 | static unsigned int | |
126 | ip6t_local_hook(unsigned int hook, | |
127 | struct sk_buff **pskb, | |
128 | const struct net_device *in, | |
129 | const struct net_device *out, | |
130 | int (*okfn)(struct sk_buff *)) | |
131 | { | |
132 | ||
1da177e4 LT |
133 | unsigned int ret; |
134 | struct in6_addr saddr, daddr; | |
135 | u_int8_t hop_limit; | |
82e91ffe | 136 | u_int32_t flowlabel, mark; |
1da177e4 LT |
137 | |
138 | #if 0 | |
139 | /* root is playing with raw sockets. */ | |
140 | if ((*pskb)->len < sizeof(struct iphdr) | |
c9bdd4b5 | 141 | || ip_hdrlen(*pskb) < sizeof(struct iphdr)) { |
1da177e4 LT |
142 | if (net_ratelimit()) |
143 | printk("ip6t_hook: happy cracking.\n"); | |
144 | return NF_ACCEPT; | |
145 | } | |
146 | #endif | |
147 | ||
82e91ffe | 148 | /* save source/dest address, mark, hoplimit, flowlabel, priority, */ |
0660e03f ACM |
149 | memcpy(&saddr, &ipv6_hdr(*pskb)->saddr, sizeof(saddr)); |
150 | memcpy(&daddr, &ipv6_hdr(*pskb)->daddr, sizeof(daddr)); | |
82e91ffe | 151 | mark = (*pskb)->mark; |
0660e03f | 152 | hop_limit = ipv6_hdr(*pskb)->hop_limit; |
1da177e4 LT |
153 | |
154 | /* flowlabel and prio (includes version, which shouldn't change either */ | |
0660e03f | 155 | flowlabel = *((u_int32_t *)ipv6_hdr(*pskb)); |
1da177e4 | 156 | |
fe1cb108 | 157 | ret = ip6t_do_table(pskb, hook, in, out, &packet_mangler); |
1da177e4 | 158 | |
1ab1457c | 159 | if (ret != NF_DROP && ret != NF_STOLEN |
0660e03f ACM |
160 | && (memcmp(&ipv6_hdr(*pskb)->saddr, &saddr, sizeof(saddr)) |
161 | || memcmp(&ipv6_hdr(*pskb)->daddr, &daddr, sizeof(daddr)) | |
82e91ffe | 162 | || (*pskb)->mark != mark |
0660e03f | 163 | || ipv6_hdr(*pskb)->hop_limit != hop_limit)) |
9123de2c | 164 | return ip6_route_me_harder(*pskb) == 0 ? ret : NF_DROP; |
1da177e4 LT |
165 | |
166 | return ret; | |
167 | } | |
168 | ||
169 | static struct nf_hook_ops ip6t_ops[] = { | |
170 | { | |
171 | .hook = ip6t_route_hook, | |
172 | .owner = THIS_MODULE, | |
173 | .pf = PF_INET6, | |
174 | .hooknum = NF_IP6_PRE_ROUTING, | |
175 | .priority = NF_IP6_PRI_MANGLE, | |
176 | }, | |
177 | { | |
178 | .hook = ip6t_local_hook, | |
179 | .owner = THIS_MODULE, | |
180 | .pf = PF_INET6, | |
181 | .hooknum = NF_IP6_LOCAL_IN, | |
182 | .priority = NF_IP6_PRI_MANGLE, | |
183 | }, | |
184 | { | |
185 | .hook = ip6t_route_hook, | |
186 | .owner = THIS_MODULE, | |
187 | .pf = PF_INET6, | |
188 | .hooknum = NF_IP6_FORWARD, | |
189 | .priority = NF_IP6_PRI_MANGLE, | |
190 | }, | |
191 | { | |
192 | .hook = ip6t_local_hook, | |
193 | .owner = THIS_MODULE, | |
194 | .pf = PF_INET6, | |
195 | .hooknum = NF_IP6_LOCAL_OUT, | |
196 | .priority = NF_IP6_PRI_MANGLE, | |
197 | }, | |
198 | { | |
199 | .hook = ip6t_route_hook, | |
200 | .owner = THIS_MODULE, | |
201 | .pf = PF_INET6, | |
202 | .hooknum = NF_IP6_POST_ROUTING, | |
203 | .priority = NF_IP6_PRI_MANGLE, | |
204 | }, | |
205 | }; | |
206 | ||
65b4b4e8 | 207 | static int __init ip6table_mangle_init(void) |
1da177e4 LT |
208 | { |
209 | int ret; | |
210 | ||
211 | /* Register table */ | |
212 | ret = ip6t_register_table(&packet_mangler, &initial_table.repl); | |
213 | if (ret < 0) | |
214 | return ret; | |
215 | ||
216 | /* Register hooks */ | |
964ddaa1 | 217 | ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops)); |
1da177e4 LT |
218 | if (ret < 0) |
219 | goto cleanup_table; | |
220 | ||
1da177e4 LT |
221 | return ret; |
222 | ||
1da177e4 LT |
223 | cleanup_table: |
224 | ip6t_unregister_table(&packet_mangler); | |
1da177e4 LT |
225 | return ret; |
226 | } | |
227 | ||
65b4b4e8 | 228 | static void __exit ip6table_mangle_fini(void) |
1da177e4 | 229 | { |
964ddaa1 | 230 | nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops)); |
1da177e4 LT |
231 | ip6t_unregister_table(&packet_mangler); |
232 | } | |
233 | ||
65b4b4e8 AM |
234 | module_init(ip6table_mangle_init); |
235 | module_exit(ip6table_mangle_fini); |