netfilter: xtables: move extension arguments into compound structure (3/6)
[deliverable/linux.git] / net / ipv6 / netfilter / ip6table_mangle.c
CommitLineData
1da177e4
LT
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.
1da177e4
LT
10 */
11#include <linux/module.h>
12#include <linux/netfilter_ipv6/ip6_tables.h>
13
14MODULE_LICENSE("GPL");
15MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
16MODULE_DESCRIPTION("ip6tables mangle table");
17
6e23ae2a
PM
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))
1da177e4 23
1da177e4
LT
24static struct
25{
26 struct ip6t_replace repl;
27 struct ip6t_standard entries[5];
28 struct ip6t_error term;
8280aa61 29} initial_table __net_initdata = {
3c2ad469
PM
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 = {
6e23ae2a
PM
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,
3c2ad469
PM
41 },
42 .underflow = {
6e23ae2a
PM
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,
3c2ad469
PM
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 */
1da177e4
LT
58};
59
8280aa61 60static struct xt_table packet_mangler = {
1da177e4
LT
61 .name = "mangle",
62 .valid_hooks = MANGLE_VALID_HOOKS,
fdccecd0 63 .lock = __RW_LOCK_UNLOCKED(packet_mangler.lock),
1da177e4 64 .me = THIS_MODULE,
2e4e6a17 65 .af = AF_INET6,
1da177e4
LT
66};
67
68/* The work comes in here from netfilter.c. */
69static unsigned int
7dd1b8da 70ip6t_in_hook(unsigned int hook,
3db05fea 71 struct sk_buff *skb,
1da177e4
LT
72 const struct net_device *in,
73 const struct net_device *out,
74 int (*okfn)(struct sk_buff *))
75{
7dd1b8da
AD
76 return ip6t_do_table(skb, hook, in, out,
77 dev_net(in)->ipv6.ip6table_mangle);
1da177e4
LT
78}
79
80static unsigned int
7dd1b8da
AD
81ip6t_post_routing_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 return ip6t_do_table(skb, hook, in, out,
88 dev_net(out)->ipv6.ip6table_mangle);
89}
90
91static unsigned int
92ip6t_local_out_hook(unsigned int hook,
3db05fea 93 struct sk_buff *skb,
1da177e4
LT
94 const struct net_device *in,
95 const struct net_device *out,
96 int (*okfn)(struct sk_buff *))
97{
98
1da177e4
LT
99 unsigned int ret;
100 struct in6_addr saddr, daddr;
101 u_int8_t hop_limit;
82e91ffe 102 u_int32_t flowlabel, mark;
1da177e4
LT
103
104#if 0
105 /* root is playing with raw sockets. */
3db05fea
HX
106 if (skb->len < sizeof(struct iphdr)
107 || ip_hdrlen(skb) < sizeof(struct iphdr)) {
1da177e4
LT
108 if (net_ratelimit())
109 printk("ip6t_hook: happy cracking.\n");
110 return NF_ACCEPT;
111 }
112#endif
113
82e91ffe 114 /* save source/dest address, mark, hoplimit, flowlabel, priority, */
3db05fea
HX
115 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
116 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
117 mark = skb->mark;
118 hop_limit = ipv6_hdr(skb)->hop_limit;
1da177e4
LT
119
120 /* flowlabel and prio (includes version, which shouldn't change either */
3db05fea 121 flowlabel = *((u_int32_t *)ipv6_hdr(skb));
1da177e4 122
7dd1b8da
AD
123 ret = ip6t_do_table(skb, hook, in, out,
124 dev_net(out)->ipv6.ip6table_mangle);
1da177e4 125
1ab1457c 126 if (ret != NF_DROP && ret != NF_STOLEN
3db05fea
HX
127 && (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr))
128 || memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr))
129 || skb->mark != mark
130 || ipv6_hdr(skb)->hop_limit != hop_limit))
131 return ip6_route_me_harder(skb) == 0 ? ret : NF_DROP;
1da177e4
LT
132
133 return ret;
134}
135
1999414a 136static struct nf_hook_ops ip6t_ops[] __read_mostly = {
1da177e4 137 {
7dd1b8da 138 .hook = ip6t_in_hook,
1da177e4
LT
139 .owner = THIS_MODULE,
140 .pf = PF_INET6,
6e23ae2a 141 .hooknum = NF_INET_PRE_ROUTING,
1da177e4
LT
142 .priority = NF_IP6_PRI_MANGLE,
143 },
144 {
7dd1b8da 145 .hook = ip6t_in_hook,
1da177e4
LT
146 .owner = THIS_MODULE,
147 .pf = PF_INET6,
6e23ae2a 148 .hooknum = NF_INET_LOCAL_IN,
1da177e4
LT
149 .priority = NF_IP6_PRI_MANGLE,
150 },
151 {
7dd1b8da 152 .hook = ip6t_in_hook,
1da177e4
LT
153 .owner = THIS_MODULE,
154 .pf = PF_INET6,
6e23ae2a 155 .hooknum = NF_INET_FORWARD,
1da177e4
LT
156 .priority = NF_IP6_PRI_MANGLE,
157 },
158 {
7dd1b8da 159 .hook = ip6t_local_out_hook,
1da177e4
LT
160 .owner = THIS_MODULE,
161 .pf = PF_INET6,
6e23ae2a 162 .hooknum = NF_INET_LOCAL_OUT,
1da177e4
LT
163 .priority = NF_IP6_PRI_MANGLE,
164 },
165 {
7dd1b8da 166 .hook = ip6t_post_routing_hook,
1da177e4
LT
167 .owner = THIS_MODULE,
168 .pf = PF_INET6,
6e23ae2a 169 .hooknum = NF_INET_POST_ROUTING,
1da177e4
LT
170 .priority = NF_IP6_PRI_MANGLE,
171 },
172};
173
8280aa61
AD
174static int __net_init ip6table_mangle_net_init(struct net *net)
175{
176 /* Register table */
177 net->ipv6.ip6table_mangle =
178 ip6t_register_table(net, &packet_mangler, &initial_table.repl);
179 if (IS_ERR(net->ipv6.ip6table_mangle))
180 return PTR_ERR(net->ipv6.ip6table_mangle);
181 return 0;
182}
183
184static void __net_exit ip6table_mangle_net_exit(struct net *net)
185{
186 ip6t_unregister_table(net->ipv6.ip6table_mangle);
187}
188
189static struct pernet_operations ip6table_mangle_net_ops = {
190 .init = ip6table_mangle_net_init,
191 .exit = ip6table_mangle_net_exit,
192};
193
65b4b4e8 194static int __init ip6table_mangle_init(void)
1da177e4
LT
195{
196 int ret;
197
8280aa61
AD
198 ret = register_pernet_subsys(&ip6table_mangle_net_ops);
199 if (ret < 0)
200 return ret;
1da177e4
LT
201
202 /* Register hooks */
964ddaa1 203 ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
1da177e4
LT
204 if (ret < 0)
205 goto cleanup_table;
206
1da177e4
LT
207 return ret;
208
1da177e4 209 cleanup_table:
8280aa61 210 unregister_pernet_subsys(&ip6table_mangle_net_ops);
1da177e4
LT
211 return ret;
212}
213
65b4b4e8 214static void __exit ip6table_mangle_fini(void)
1da177e4 215{
964ddaa1 216 nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
8280aa61 217 unregister_pernet_subsys(&ip6table_mangle_net_ops);
1da177e4
LT
218}
219
65b4b4e8
AM
220module_init(ip6table_mangle_init);
221module_exit(ip6table_mangle_fini);
This page took 1.140256 seconds and 5 git commands to generate.