405ac1f76390a07c028d6dbdc2ae715bd6848e08
[linux-2.6.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_INET_PRE_ROUTING) | \
19                             (1 << NF_INET_LOCAL_IN) | \
20                             (1 << NF_INET_FORWARD) | \
21                             (1 << NF_INET_LOCAL_OUT) | \
22                             (1 << NF_INET_POST_ROUTING))
23
24 static const struct
25 {
26         struct ip6t_replace repl;
27         struct ip6t_standard entries[5];
28         struct ip6t_error term;
29 } initial_table __net_initdata = {
30         .repl = {
31                 .name = "mangle",
32                 .valid_hooks = MANGLE_VALID_HOOKS,
33                 .num_entries = 6,
34                 .size = sizeof(struct ip6t_standard) * 5 + sizeof(struct ip6t_error),
35                 .hook_entry = {
36                         [NF_INET_PRE_ROUTING]   = 0,
37                         [NF_INET_LOCAL_IN]      = sizeof(struct ip6t_standard),
38                         [NF_INET_FORWARD]       = sizeof(struct ip6t_standard) * 2,
39                         [NF_INET_LOCAL_OUT]     = sizeof(struct ip6t_standard) * 3,
40                         [NF_INET_POST_ROUTING]  = sizeof(struct ip6t_standard) * 4,
41                 },
42                 .underflow = {
43                         [NF_INET_PRE_ROUTING]   = 0,
44                         [NF_INET_LOCAL_IN]      = sizeof(struct ip6t_standard),
45                         [NF_INET_FORWARD]       = sizeof(struct ip6t_standard) * 2,
46                         [NF_INET_LOCAL_OUT]     = sizeof(struct ip6t_standard) * 3,
47                         [NF_INET_POST_ROUTING]  = sizeof(struct ip6t_standard) * 4,
48                 },
49         },
50         .entries = {
51                 IP6T_STANDARD_INIT(NF_ACCEPT),  /* PRE_ROUTING */
52                 IP6T_STANDARD_INIT(NF_ACCEPT),  /* LOCAL_IN */
53                 IP6T_STANDARD_INIT(NF_ACCEPT),  /* FORWARD */
54                 IP6T_STANDARD_INIT(NF_ACCEPT),  /* LOCAL_OUT */
55                 IP6T_STANDARD_INIT(NF_ACCEPT),  /* POST_ROUTING */
56         },
57         .term = IP6T_ERROR_INIT,                /* ERROR */
58 };
59
60 static const struct xt_table packet_mangler = {
61         .name           = "mangle",
62         .valid_hooks    = MANGLE_VALID_HOOKS,
63         .me             = THIS_MODULE,
64         .af             = NFPROTO_IPV6,
65 };
66
67 static unsigned int
68 ip6t_local_out_hook(unsigned int hook,
69                    struct sk_buff *skb,
70                    const struct net_device *out,
71                    int (*okfn)(struct sk_buff *))
72 {
73
74         unsigned int ret;
75         struct in6_addr saddr, daddr;
76         u_int8_t hop_limit;
77         u_int32_t flowlabel, mark;
78
79 #if 0
80         /* root is playing with raw sockets. */
81         if (skb->len < sizeof(struct iphdr) ||
82             ip_hdrlen(skb) < sizeof(struct iphdr)) {
83                 if (net_ratelimit())
84                         printk("ip6t_hook: happy cracking.\n");
85                 return NF_ACCEPT;
86         }
87 #endif
88
89         /* save source/dest address, mark, hoplimit, flowlabel, priority,  */
90         memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
91         memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
92         mark = skb->mark;
93         hop_limit = ipv6_hdr(skb)->hop_limit;
94
95         /* flowlabel and prio (includes version, which shouldn't change either */
96         flowlabel = *((u_int32_t *)ipv6_hdr(skb));
97
98         ret = ip6t_do_table(skb, hook, NULL, out,
99                             dev_net(out)->ipv6.ip6table_mangle);
100
101         if (ret != NF_DROP && ret != NF_STOLEN &&
102             (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) ||
103              memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) ||
104              skb->mark != mark ||
105              ipv6_hdr(skb)->hop_limit != hop_limit))
106                 return ip6_route_me_harder(skb) == 0 ? ret : NF_DROP;
107
108         return ret;
109 }
110
111 /* The work comes in here from netfilter.c. */
112 static unsigned int
113 ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb,
114                      const struct net_device *in, const struct net_device *out,
115                      int (*okfn)(struct sk_buff *))
116 {
117         if (hook == NF_INET_LOCAL_OUT)
118                 return ip6t_local_out_hook(hook, skb, out, okfn);
119
120         /* INPUT/FORWARD */
121         return ip6t_do_table(skb, hook, in, out,
122                              dev_net(in)->ipv6.ip6table_mangle);
123 }
124
125 static struct nf_hook_ops ip6t_ops[] __read_mostly = {
126         {
127                 .hook           = ip6table_mangle_hook,
128                 .owner          = THIS_MODULE,
129                 .pf             = NFPROTO_IPV6,
130                 .hooknum        = NF_INET_PRE_ROUTING,
131                 .priority       = NF_IP6_PRI_MANGLE,
132         },
133         {
134                 .hook           = ip6table_mangle_hook,
135                 .owner          = THIS_MODULE,
136                 .pf             = NFPROTO_IPV6,
137                 .hooknum        = NF_INET_LOCAL_IN,
138                 .priority       = NF_IP6_PRI_MANGLE,
139         },
140         {
141                 .hook           = ip6table_mangle_hook,
142                 .owner          = THIS_MODULE,
143                 .pf             = NFPROTO_IPV6,
144                 .hooknum        = NF_INET_FORWARD,
145                 .priority       = NF_IP6_PRI_MANGLE,
146         },
147         {
148                 .hook           = ip6table_mangle_hook,
149                 .owner          = THIS_MODULE,
150                 .pf             = NFPROTO_IPV6,
151                 .hooknum        = NF_INET_LOCAL_OUT,
152                 .priority       = NF_IP6_PRI_MANGLE,
153         },
154         {
155                 .hook           = ip6table_mangle_hook,
156                 .owner          = THIS_MODULE,
157                 .pf             = NFPROTO_IPV6,
158                 .hooknum        = NF_INET_POST_ROUTING,
159                 .priority       = NF_IP6_PRI_MANGLE,
160         },
161 };
162
163 static int __net_init ip6table_mangle_net_init(struct net *net)
164 {
165         /* Register table */
166         net->ipv6.ip6table_mangle =
167                 ip6t_register_table(net, &packet_mangler, &initial_table.repl);
168         if (IS_ERR(net->ipv6.ip6table_mangle))
169                 return PTR_ERR(net->ipv6.ip6table_mangle);
170         return 0;
171 }
172
173 static void __net_exit ip6table_mangle_net_exit(struct net *net)
174 {
175         ip6t_unregister_table(net, net->ipv6.ip6table_mangle);
176 }
177
178 static struct pernet_operations ip6table_mangle_net_ops = {
179         .init = ip6table_mangle_net_init,
180         .exit = ip6table_mangle_net_exit,
181 };
182
183 static int __init ip6table_mangle_init(void)
184 {
185         int ret;
186
187         ret = register_pernet_subsys(&ip6table_mangle_net_ops);
188         if (ret < 0)
189                 return ret;
190
191         /* Register hooks */
192         ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
193         if (ret < 0)
194                 goto cleanup_table;
195
196         return ret;
197
198  cleanup_table:
199         unregister_pernet_subsys(&ip6table_mangle_net_ops);
200         return ret;
201 }
202
203 static void __exit ip6table_mangle_fini(void)
204 {
205         nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
206         unregister_pernet_subsys(&ip6table_mangle_net_ops);
207 }
208
209 module_init(ip6table_mangle_init);
210 module_exit(ip6table_mangle_fini);