Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[deliverable/linux.git] / net / ipv6 / netfilter / ip6table_mangle.c
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 #include <linux/module.h>
12 #include <linux/netfilter_ipv6/ip6_tables.h>
13
14 MODULE_LICENSE("GPL");
15 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
16 MODULE_DESCRIPTION("ip6tables mangle table");
17
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))
23
24 #if 0
25 #define DEBUGP(x, args...) printk(KERN_DEBUG x, ## args)
26 #else
27 #define DEBUGP(x, args...)
28 #endif
29
30 static struct
31 {
32 struct ip6t_replace repl;
33 struct ip6t_standard entries[5];
34 struct ip6t_error term;
35 } initial_table __initdata = {
36 .repl = {
37 .name = "mangle",
38 .valid_hooks = MANGLE_VALID_HOOKS,
39 .num_entries = 6,
40 .size = sizeof(struct ip6t_standard) * 5 + sizeof(struct ip6t_error),
41 .hook_entry = {
42 [NF_IP6_PRE_ROUTING] = 0,
43 [NF_IP6_LOCAL_IN] = sizeof(struct ip6t_standard),
44 [NF_IP6_FORWARD] = sizeof(struct ip6t_standard) * 2,
45 [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 3,
46 [NF_IP6_POST_ROUTING] = sizeof(struct ip6t_standard) * 4,
47 },
48 .underflow = {
49 [NF_IP6_PRE_ROUTING] = 0,
50 [NF_IP6_LOCAL_IN] = sizeof(struct ip6t_standard),
51 [NF_IP6_FORWARD] = sizeof(struct ip6t_standard) * 2,
52 [NF_IP6_LOCAL_OUT] = sizeof(struct ip6t_standard) * 3,
53 [NF_IP6_POST_ROUTING] = sizeof(struct ip6t_standard) * 4,
54 },
55 },
56 .entries = {
57 IP6T_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
58 IP6T_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */
59 IP6T_STANDARD_INIT(NF_ACCEPT), /* FORWARD */
60 IP6T_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
61 IP6T_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */
62 },
63 .term = IP6T_ERROR_INIT, /* ERROR */
64 };
65
66 static struct xt_table packet_mangler = {
67 .name = "mangle",
68 .valid_hooks = MANGLE_VALID_HOOKS,
69 .lock = RW_LOCK_UNLOCKED,
70 .me = THIS_MODULE,
71 .af = AF_INET6,
72 };
73
74 /* The work comes in here from netfilter.c. */
75 static unsigned int
76 ip6t_route_hook(unsigned int hook,
77 struct sk_buff **pskb,
78 const struct net_device *in,
79 const struct net_device *out,
80 int (*okfn)(struct sk_buff *))
81 {
82 return ip6t_do_table(pskb, hook, in, out, &packet_mangler);
83 }
84
85 static unsigned int
86 ip6t_local_hook(unsigned int hook,
87 struct sk_buff **pskb,
88 const struct net_device *in,
89 const struct net_device *out,
90 int (*okfn)(struct sk_buff *))
91 {
92
93 unsigned int ret;
94 struct in6_addr saddr, daddr;
95 u_int8_t hop_limit;
96 u_int32_t flowlabel, mark;
97
98 #if 0
99 /* root is playing with raw sockets. */
100 if ((*pskb)->len < sizeof(struct iphdr)
101 || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
102 if (net_ratelimit())
103 printk("ip6t_hook: happy cracking.\n");
104 return NF_ACCEPT;
105 }
106 #endif
107
108 /* save source/dest address, mark, hoplimit, flowlabel, priority, */
109 memcpy(&saddr, &ipv6_hdr(*pskb)->saddr, sizeof(saddr));
110 memcpy(&daddr, &ipv6_hdr(*pskb)->daddr, sizeof(daddr));
111 mark = (*pskb)->mark;
112 hop_limit = ipv6_hdr(*pskb)->hop_limit;
113
114 /* flowlabel and prio (includes version, which shouldn't change either */
115 flowlabel = *((u_int32_t *)ipv6_hdr(*pskb));
116
117 ret = ip6t_do_table(pskb, hook, in, out, &packet_mangler);
118
119 if (ret != NF_DROP && ret != NF_STOLEN
120 && (memcmp(&ipv6_hdr(*pskb)->saddr, &saddr, sizeof(saddr))
121 || memcmp(&ipv6_hdr(*pskb)->daddr, &daddr, sizeof(daddr))
122 || (*pskb)->mark != mark
123 || ipv6_hdr(*pskb)->hop_limit != hop_limit))
124 return ip6_route_me_harder(*pskb) == 0 ? ret : NF_DROP;
125
126 return ret;
127 }
128
129 static struct nf_hook_ops ip6t_ops[] = {
130 {
131 .hook = ip6t_route_hook,
132 .owner = THIS_MODULE,
133 .pf = PF_INET6,
134 .hooknum = NF_IP6_PRE_ROUTING,
135 .priority = NF_IP6_PRI_MANGLE,
136 },
137 {
138 .hook = ip6t_local_hook,
139 .owner = THIS_MODULE,
140 .pf = PF_INET6,
141 .hooknum = NF_IP6_LOCAL_IN,
142 .priority = NF_IP6_PRI_MANGLE,
143 },
144 {
145 .hook = ip6t_route_hook,
146 .owner = THIS_MODULE,
147 .pf = PF_INET6,
148 .hooknum = NF_IP6_FORWARD,
149 .priority = NF_IP6_PRI_MANGLE,
150 },
151 {
152 .hook = ip6t_local_hook,
153 .owner = THIS_MODULE,
154 .pf = PF_INET6,
155 .hooknum = NF_IP6_LOCAL_OUT,
156 .priority = NF_IP6_PRI_MANGLE,
157 },
158 {
159 .hook = ip6t_route_hook,
160 .owner = THIS_MODULE,
161 .pf = PF_INET6,
162 .hooknum = NF_IP6_POST_ROUTING,
163 .priority = NF_IP6_PRI_MANGLE,
164 },
165 };
166
167 static int __init ip6table_mangle_init(void)
168 {
169 int ret;
170
171 /* Register table */
172 ret = ip6t_register_table(&packet_mangler, &initial_table.repl);
173 if (ret < 0)
174 return ret;
175
176 /* Register hooks */
177 ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
178 if (ret < 0)
179 goto cleanup_table;
180
181 return ret;
182
183 cleanup_table:
184 ip6t_unregister_table(&packet_mangler);
185 return ret;
186 }
187
188 static void __exit ip6table_mangle_fini(void)
189 {
190 nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
191 ip6t_unregister_table(&packet_mangler);
192 }
193
194 module_init(ip6table_mangle_init);
195 module_exit(ip6table_mangle_fini);
This page took 0.039417 seconds and 6 git commands to generate.