Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[pandora-kernel.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 = { { "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},
48       0, NULL, { } },
49     {
50             /* PRE_ROUTING */
51             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
52                 0,
53                 sizeof(struct ip6t_entry),
54                 sizeof(struct ip6t_standard),
55                 0, { 0, 0 }, { } },
56               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
57                 -NF_ACCEPT - 1 } },
58             /* LOCAL_IN */
59             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
60                 0,
61                 sizeof(struct ip6t_entry),
62                 sizeof(struct ip6t_standard),
63                 0, { 0, 0 }, { } },
64               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
65                 -NF_ACCEPT - 1 } },
66             /* FORWARD */
67             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
68                 0,
69                 sizeof(struct ip6t_entry),
70                 sizeof(struct ip6t_standard),
71                 0, { 0, 0 }, { } },
72               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
73                 -NF_ACCEPT - 1 } },
74             /* LOCAL_OUT */
75             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
76                 0,
77                 sizeof(struct ip6t_entry),
78                 sizeof(struct ip6t_standard),
79                 0, { 0, 0 }, { } },
80               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
81                 -NF_ACCEPT - 1 } },
82             /* POST_ROUTING */
83             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
84                 0,
85                 sizeof(struct ip6t_entry),
86                 sizeof(struct ip6t_standard),
87                 0, { 0, 0 }, { } },
88               { { { { IP6T_ALIGN(sizeof(struct ip6t_standard_target)), "" } }, { } },
89                 -NF_ACCEPT - 1 } }
90     },
91     /* ERROR */
92     { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
93         0,
94         sizeof(struct ip6t_entry),
95         sizeof(struct ip6t_error),
96         0, { 0, 0 }, { } },
97       { { { { IP6T_ALIGN(sizeof(struct ip6t_error_target)), IP6T_ERROR_TARGET } },
98           { } },
99         "ERROR"
100       }
101     }
102 };
103
104 static struct xt_table packet_mangler = {
105         .name           = "mangle",
106         .valid_hooks    = MANGLE_VALID_HOOKS,
107         .lock           = RW_LOCK_UNLOCKED,
108         .me             = THIS_MODULE,
109         .af             = AF_INET6,
110 };
111
112 /* The work comes in here from netfilter.c. */
113 static unsigned int
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 *))
119 {
120         return ip6t_do_table(pskb, hook, in, out, &packet_mangler);
121 }
122
123 static unsigned int
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 *))
129 {
130
131         unsigned int ret;
132         struct in6_addr saddr, daddr;
133         u_int8_t hop_limit;
134         u_int32_t flowlabel, mark;
135
136 #if 0
137         /* root is playing with raw sockets. */
138         if ((*pskb)->len < sizeof(struct iphdr)
139             || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
140                 if (net_ratelimit())
141                         printk("ip6t_hook: happy cracking.\n");
142                 return NF_ACCEPT;
143         }
144 #endif
145
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;
151
152         /* flowlabel and prio (includes version, which shouldn't change either */
153         flowlabel = *((u_int32_t *)ipv6_hdr(*pskb));
154
155         ret = ip6t_do_table(pskb, hook, in, out, &packet_mangler);
156
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;
163
164         return ret;
165 }
166
167 static struct nf_hook_ops ip6t_ops[] = {
168         {
169                 .hook           = ip6t_route_hook,
170                 .owner          = THIS_MODULE,
171                 .pf             = PF_INET6,
172                 .hooknum        = NF_IP6_PRE_ROUTING,
173                 .priority       = NF_IP6_PRI_MANGLE,
174         },
175         {
176                 .hook           = ip6t_local_hook,
177                 .owner          = THIS_MODULE,
178                 .pf             = PF_INET6,
179                 .hooknum        = NF_IP6_LOCAL_IN,
180                 .priority       = NF_IP6_PRI_MANGLE,
181         },
182         {
183                 .hook           = ip6t_route_hook,
184                 .owner          = THIS_MODULE,
185                 .pf             = PF_INET6,
186                 .hooknum        = NF_IP6_FORWARD,
187                 .priority       = NF_IP6_PRI_MANGLE,
188         },
189         {
190                 .hook           = ip6t_local_hook,
191                 .owner          = THIS_MODULE,
192                 .pf             = PF_INET6,
193                 .hooknum        = NF_IP6_LOCAL_OUT,
194                 .priority       = NF_IP6_PRI_MANGLE,
195         },
196         {
197                 .hook           = ip6t_route_hook,
198                 .owner          = THIS_MODULE,
199                 .pf             = PF_INET6,
200                 .hooknum        = NF_IP6_POST_ROUTING,
201                 .priority       = NF_IP6_PRI_MANGLE,
202         },
203 };
204
205 static int __init ip6table_mangle_init(void)
206 {
207         int ret;
208
209         /* Register table */
210         ret = ip6t_register_table(&packet_mangler, &initial_table.repl);
211         if (ret < 0)
212                 return ret;
213
214         /* Register hooks */
215         ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
216         if (ret < 0)
217                 goto cleanup_table;
218
219         return ret;
220
221  cleanup_table:
222         ip6t_unregister_table(&packet_mangler);
223         return ret;
224 }
225
226 static void __exit ip6table_mangle_fini(void)
227 {
228         nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
229         ip6t_unregister_table(&packet_mangler);
230 }
231
232 module_init(ip6table_mangle_init);
233 module_exit(ip6table_mangle_fini);