[NETFILTER]: ip6_tables: netns preparation
[sfrench/cifs-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 struct
25 {
26         struct ip6t_replace repl;
27         struct ip6t_standard entries[5];
28         struct ip6t_error term;
29 } initial_table __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 struct xt_table __packet_mangler = {
61         .name           = "mangle",
62         .valid_hooks    = MANGLE_VALID_HOOKS,
63         .lock           = RW_LOCK_UNLOCKED,
64         .me             = THIS_MODULE,
65         .af             = AF_INET6,
66 };
67 static struct xt_table *packet_mangler;
68
69 /* The work comes in here from netfilter.c. */
70 static unsigned int
71 ip6t_route_hook(unsigned int hook,
72          struct sk_buff *skb,
73          const struct net_device *in,
74          const struct net_device *out,
75          int (*okfn)(struct sk_buff *))
76 {
77         return ip6t_do_table(skb, hook, in, out, packet_mangler);
78 }
79
80 static unsigned int
81 ip6t_local_hook(unsigned int hook,
82                    struct sk_buff *skb,
83                    const struct net_device *in,
84                    const struct net_device *out,
85                    int (*okfn)(struct sk_buff *))
86 {
87
88         unsigned int ret;
89         struct in6_addr saddr, daddr;
90         u_int8_t hop_limit;
91         u_int32_t flowlabel, mark;
92
93 #if 0
94         /* root is playing with raw sockets. */
95         if (skb->len < sizeof(struct iphdr)
96             || ip_hdrlen(skb) < sizeof(struct iphdr)) {
97                 if (net_ratelimit())
98                         printk("ip6t_hook: happy cracking.\n");
99                 return NF_ACCEPT;
100         }
101 #endif
102
103         /* save source/dest address, mark, hoplimit, flowlabel, priority,  */
104         memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
105         memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
106         mark = skb->mark;
107         hop_limit = ipv6_hdr(skb)->hop_limit;
108
109         /* flowlabel and prio (includes version, which shouldn't change either */
110         flowlabel = *((u_int32_t *)ipv6_hdr(skb));
111
112         ret = ip6t_do_table(skb, hook, in, out, packet_mangler);
113
114         if (ret != NF_DROP && ret != NF_STOLEN
115                 && (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr))
116                     || memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr))
117                     || skb->mark != mark
118                     || ipv6_hdr(skb)->hop_limit != hop_limit))
119                 return ip6_route_me_harder(skb) == 0 ? ret : NF_DROP;
120
121         return ret;
122 }
123
124 static struct nf_hook_ops ip6t_ops[] __read_mostly = {
125         {
126                 .hook           = ip6t_route_hook,
127                 .owner          = THIS_MODULE,
128                 .pf             = PF_INET6,
129                 .hooknum        = NF_INET_PRE_ROUTING,
130                 .priority       = NF_IP6_PRI_MANGLE,
131         },
132         {
133                 .hook           = ip6t_local_hook,
134                 .owner          = THIS_MODULE,
135                 .pf             = PF_INET6,
136                 .hooknum        = NF_INET_LOCAL_IN,
137                 .priority       = NF_IP6_PRI_MANGLE,
138         },
139         {
140                 .hook           = ip6t_route_hook,
141                 .owner          = THIS_MODULE,
142                 .pf             = PF_INET6,
143                 .hooknum        = NF_INET_FORWARD,
144                 .priority       = NF_IP6_PRI_MANGLE,
145         },
146         {
147                 .hook           = ip6t_local_hook,
148                 .owner          = THIS_MODULE,
149                 .pf             = PF_INET6,
150                 .hooknum        = NF_INET_LOCAL_OUT,
151                 .priority       = NF_IP6_PRI_MANGLE,
152         },
153         {
154                 .hook           = ip6t_route_hook,
155                 .owner          = THIS_MODULE,
156                 .pf             = PF_INET6,
157                 .hooknum        = NF_INET_POST_ROUTING,
158                 .priority       = NF_IP6_PRI_MANGLE,
159         },
160 };
161
162 static int __init ip6table_mangle_init(void)
163 {
164         int ret;
165
166         /* Register table */
167         packet_mangler = ip6t_register_table(&init_net, &__packet_mangler, &initial_table.repl);
168         if (IS_ERR(packet_mangler))
169                 return PTR_ERR(packet_mangler);
170
171         /* Register hooks */
172         ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
173         if (ret < 0)
174                 goto cleanup_table;
175
176         return ret;
177
178  cleanup_table:
179         ip6t_unregister_table(packet_mangler);
180         return ret;
181 }
182
183 static void __exit ip6table_mangle_fini(void)
184 {
185         nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
186         ip6t_unregister_table(packet_mangler);
187 }
188
189 module_init(ip6table_mangle_init);
190 module_exit(ip6table_mangle_fini);