Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-drm-fsl-dcu.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  * 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
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 */
53             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
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 */
61             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
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 */
69             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
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 */
77             { { { { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, { { { 0 } } }, "", "", { 0 }, { 0 }, 0, 0, 0 },
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
106 static struct xt_table packet_mangler = {
107         .name           = "mangle",
108         .valid_hooks    = MANGLE_VALID_HOOKS,
109         .lock           = RW_LOCK_UNLOCKED,
110         .me             = THIS_MODULE,
111         .af             = AF_INET6,
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 {
122         return ip6t_do_table(pskb, hook, in, out, &packet_mangler);
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
133         unsigned int ret;
134         struct in6_addr saddr, daddr;
135         u_int8_t hop_limit;
136         u_int32_t flowlabel, mark;
137
138 #if 0
139         /* root is playing with raw sockets. */
140         if ((*pskb)->len < sizeof(struct iphdr)
141             || (*pskb)->nh.iph->ihl * 4 < sizeof(struct iphdr)) {
142                 if (net_ratelimit())
143                         printk("ip6t_hook: happy cracking.\n");
144                 return NF_ACCEPT;
145         }
146 #endif
147
148         /* save source/dest address, mark, hoplimit, flowlabel, priority,  */
149         memcpy(&saddr, &(*pskb)->nh.ipv6h->saddr, sizeof(saddr));
150         memcpy(&daddr, &(*pskb)->nh.ipv6h->daddr, sizeof(daddr));
151         mark = (*pskb)->mark;
152         hop_limit = (*pskb)->nh.ipv6h->hop_limit;
153
154         /* flowlabel and prio (includes version, which shouldn't change either */
155         flowlabel = *((u_int32_t *) (*pskb)->nh.ipv6h);
156
157         ret = ip6t_do_table(pskb, hook, in, out, &packet_mangler);
158
159         if (ret != NF_DROP && ret != NF_STOLEN
160                 && (memcmp(&(*pskb)->nh.ipv6h->saddr, &saddr, sizeof(saddr))
161                     || memcmp(&(*pskb)->nh.ipv6h->daddr, &daddr, sizeof(daddr))
162                     || (*pskb)->mark != mark
163                     || (*pskb)->nh.ipv6h->hop_limit != hop_limit))
164                 return ip6_route_me_harder(*pskb) == 0 ? ret : NF_DROP;
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
207 static int __init ip6table_mangle_init(void)
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 */
217         ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
218         if (ret < 0)
219                 goto cleanup_table;
220
221         return ret;
222
223  cleanup_table:
224         ip6t_unregister_table(&packet_mangler);
225         return ret;
226 }
227
228 static void __exit ip6table_mangle_fini(void)
229 {
230         nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
231         ip6t_unregister_table(&packet_mangler);
232 }
233
234 module_init(ip6table_mangle_init);
235 module_exit(ip6table_mangle_fini);