2 * IPv6 packet mangling table, a port of the IPv4 mangle table to IPv6
4 * Copyright (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
5 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
11 #include <linux/module.h>
12 #include <linux/netfilter_ipv6/ip6_tables.h>
14 MODULE_LICENSE("GPL");
15 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
16 MODULE_DESCRIPTION("ip6tables mangle table");
18 #define MANGLE_VALID_HOOKS ((1 << NF_IP6_PRE_ROUTING) | \
19 (1 << NF_IP6_LOCAL_IN) | \
20 (1 << NF_IP6_FORWARD) | \
21 (1 << NF_IP6_LOCAL_OUT) | \
22 (1 << NF_IP6_POST_ROUTING))
25 #define DEBUGP(x, args...) printk(KERN_DEBUG x, ## args)
27 #define DEBUGP(x, args...)
32 struct ip6t_replace repl;
33 struct ip6t_standard entries[5];
34 struct ip6t_error term;
35 } initial_table __initdata
36 = { { "mangle", MANGLE_VALID_HOOKS, 6,
37 sizeof(struct ip6t_standard) * 5 + sizeof(struct ip6t_error),
38 { [NF_IP6_PRE_ROUTING] = 0,
39 [NF_IP6_LOCAL_IN] = sizeof(struct ip6t_standard),
40 [NF_IP6_FORWARD] = sizeof(struct ip6t_standard) * 2,
41 [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 3,
42 [NF_IP6_POST_ROUTING] = sizeof(struct ip6t_standard) * 4},
43 { [NF_IP6_PRE_ROUTING] = 0,
44 [NF_IP6_LOCAL_IN] = sizeof(struct ip6t_standard),
45 [NF_IP6_FORWARD] = sizeof(struct ip6t_standard) * 2,
46 [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 3,
47 [NF_IP6_POST_ROUTING] = sizeof(struct ip6t_standard) * 4},
51 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
53 sizeof(struct ip6t_entry),
54 sizeof(struct ip6t_standard),
56 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
59 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
61 sizeof(struct ip6t_entry),
62 sizeof(struct ip6t_standard),
64 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
67 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
69 sizeof(struct ip6t_entry),
70 sizeof(struct ip6t_standard),
72 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
75 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
77 sizeof(struct ip6t_entry),
78 sizeof(struct ip6t_standard),
80 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
83 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
85 sizeof(struct ip6t_entry),
86 sizeof(struct ip6t_standard),
88 { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
92 { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
94 sizeof(struct ip6t_entry),
95 sizeof(struct ip6t_error),
97 { { { { IP6T_ALIGN(sizeof(struct ip6t_error_target)), IP6T_ERROR_TARGET } },
104 static struct xt_table packet_mangler = {
106 .valid_hooks = MANGLE_VALID_HOOKS,
107 .lock = RW_LOCK_UNLOCKED,
112 /* The work comes in here from netfilter.c. */
114 ip6t_route_hook(unsigned int hook,
115 struct sk_buff **pskb,
116 const struct net_device *in,
117 const struct net_device *out,
118 int (*okfn)(struct sk_buff *))
120 return ip6t_do_table(pskb, hook, in, out, &packet_mangler);
124 ip6t_local_hook(unsigned int hook,
125 struct sk_buff **pskb,
126 const struct net_device *in,
127 const struct net_device *out,
128 int (*okfn)(struct sk_buff *))
132 struct in6_addr saddr, daddr;
134 u_int32_t flowlabel, mark;
137 /* root is playing with raw sockets. */
138 if ((*pskb)->len < sizeof(struct iphdr)
139 || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
141 printk("ip6t_hook: happy cracking.\n");
146 /* save source/dest address, mark, hoplimit, flowlabel, priority, */
147 memcpy(&saddr, &ipv6_hdr(*pskb)->saddr, sizeof(saddr));
148 memcpy(&daddr, &ipv6_hdr(*pskb)->daddr, sizeof(daddr));
149 mark = (*pskb)->mark;
150 hop_limit = ipv6_hdr(*pskb)->hop_limit;
152 /* flowlabel and prio (includes version, which shouldn't change either */
153 flowlabel = *((u_int32_t *)ipv6_hdr(*pskb));
155 ret = ip6t_do_table(pskb, hook, in, out, &packet_mangler);
157 if (ret != NF_DROP && ret != NF_STOLEN
158 && (memcmp(&ipv6_hdr(*pskb)->saddr, &saddr, sizeof(saddr))
159 || memcmp(&ipv6_hdr(*pskb)->daddr, &daddr, sizeof(daddr))
160 || (*pskb)->mark != mark
161 || ipv6_hdr(*pskb)->hop_limit != hop_limit))
162 return ip6_route_me_harder(*pskb) == 0 ? ret : NF_DROP;
167 static struct nf_hook_ops ip6t_ops[] = {
169 .hook = ip6t_route_hook,
170 .owner = THIS_MODULE,
172 .hooknum = NF_IP6_PRE_ROUTING,
173 .priority = NF_IP6_PRI_MANGLE,
176 .hook = ip6t_local_hook,
177 .owner = THIS_MODULE,
179 .hooknum = NF_IP6_LOCAL_IN,
180 .priority = NF_IP6_PRI_MANGLE,
183 .hook = ip6t_route_hook,
184 .owner = THIS_MODULE,
186 .hooknum = NF_IP6_FORWARD,
187 .priority = NF_IP6_PRI_MANGLE,
190 .hook = ip6t_local_hook,
191 .owner = THIS_MODULE,
193 .hooknum = NF_IP6_LOCAL_OUT,
194 .priority = NF_IP6_PRI_MANGLE,
197 .hook = ip6t_route_hook,
198 .owner = THIS_MODULE,
200 .hooknum = NF_IP6_POST_ROUTING,
201 .priority = NF_IP6_PRI_MANGLE,
205 static int __init ip6table_mangle_init(void)
210 ret = ip6t_register_table(&packet_mangler, &initial_table.repl);
215 ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
222 ip6t_unregister_table(&packet_mangler);
226 static void __exit ip6table_mangle_fini(void)
228 nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
229 ip6t_unregister_table(&packet_mangler);
232 module_init(ip6table_mangle_init);
233 module_exit(ip6table_mangle_fini);