Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-block.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>
5a0e3ad6 13#include <linux/slab.h>
1da177e4
LT
14
15MODULE_LICENSE("GPL");
16MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
17MODULE_DESCRIPTION("ip6tables mangle table");
18
6e23ae2a
PM
19#define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
20 (1 << NF_INET_LOCAL_IN) | \
21 (1 << NF_INET_FORWARD) | \
22 (1 << NF_INET_LOCAL_OUT) | \
23 (1 << NF_INET_POST_ROUTING))
1da177e4 24
35aad0ff 25static const struct xt_table packet_mangler = {
1da177e4
LT
26 .name = "mangle",
27 .valid_hooks = MANGLE_VALID_HOOKS,
1da177e4 28 .me = THIS_MODULE,
f88e6a8a 29 .af = NFPROTO_IPV6,
2b95efe7 30 .priority = NF_IP6_PRI_MANGLE,
1da177e4
LT
31};
32
7dd1b8da 33static unsigned int
fa96a0e2 34ip6t_mangle_out(struct sk_buff *skb, const struct net_device *out)
1da177e4 35{
1da177e4
LT
36 unsigned int ret;
37 struct in6_addr saddr, daddr;
38 u_int8_t hop_limit;
82e91ffe 39 u_int32_t flowlabel, mark;
1da177e4
LT
40
41#if 0
42 /* root is playing with raw sockets. */
3666ed1c
JP
43 if (skb->len < sizeof(struct iphdr) ||
44 ip_hdrlen(skb) < sizeof(struct iphdr)) {
1da177e4 45 if (net_ratelimit())
654d0fbd 46 pr_warning("ip6t_hook: happy cracking.\n");
1da177e4
LT
47 return NF_ACCEPT;
48 }
49#endif
50
82e91ffe 51 /* save source/dest address, mark, hoplimit, flowlabel, priority, */
3db05fea
HX
52 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
53 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
54 mark = skb->mark;
55 hop_limit = ipv6_hdr(skb)->hop_limit;
1da177e4
LT
56
57 /* flowlabel and prio (includes version, which shouldn't change either */
3db05fea 58 flowlabel = *((u_int32_t *)ipv6_hdr(skb));
1da177e4 59
fa96a0e2 60 ret = ip6t_do_table(skb, NF_INET_LOCAL_OUT, NULL, out,
7dd1b8da 61 dev_net(out)->ipv6.ip6table_mangle);
1da177e4 62
3666ed1c
JP
63 if (ret != NF_DROP && ret != NF_STOLEN &&
64 (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) ||
65 memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) ||
66 skb->mark != mark ||
b169f6db
DM
67 ipv6_hdr(skb)->hop_limit != hop_limit ||
68 flowlabel != *((u_int32_t *)ipv6_hdr(skb))))
3db05fea 69 return ip6_route_me_harder(skb) == 0 ? ret : NF_DROP;
1da177e4
LT
70
71 return ret;
72}
73
737535c5
JE
74/* The work comes in here from netfilter.c. */
75static unsigned int
76ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb,
77 const struct net_device *in, const struct net_device *out,
78 int (*okfn)(struct sk_buff *))
79{
80 if (hook == NF_INET_LOCAL_OUT)
fa96a0e2 81 return ip6t_mangle_out(skb, out);
b2907e50
AD
82 if (hook == NF_INET_POST_ROUTING)
83 return ip6t_do_table(skb, hook, in, out,
84 dev_net(out)->ipv6.ip6table_mangle);
737535c5
JE
85 /* INPUT/FORWARD */
86 return ip6t_do_table(skb, hook, in, out,
87 dev_net(in)->ipv6.ip6table_mangle);
88}
89
2b95efe7 90static struct nf_hook_ops *mangle_ops __read_mostly;
8280aa61
AD
91static int __net_init ip6table_mangle_net_init(struct net *net)
92{
e3eaa991
JE
93 struct ip6t_replace *repl;
94
95 repl = ip6t_alloc_initial_table(&packet_mangler);
96 if (repl == NULL)
97 return -ENOMEM;
8280aa61 98 net->ipv6.ip6table_mangle =
e3eaa991
JE
99 ip6t_register_table(net, &packet_mangler, repl);
100 kfree(repl);
8280aa61
AD
101 if (IS_ERR(net->ipv6.ip6table_mangle))
102 return PTR_ERR(net->ipv6.ip6table_mangle);
103 return 0;
104}
105
106static void __net_exit ip6table_mangle_net_exit(struct net *net)
107{
f54e9367 108 ip6t_unregister_table(net, net->ipv6.ip6table_mangle);
8280aa61
AD
109}
110
111static struct pernet_operations ip6table_mangle_net_ops = {
112 .init = ip6table_mangle_net_init,
113 .exit = ip6table_mangle_net_exit,
114};
115
65b4b4e8 116static int __init ip6table_mangle_init(void)
1da177e4
LT
117{
118 int ret;
119
8280aa61
AD
120 ret = register_pernet_subsys(&ip6table_mangle_net_ops);
121 if (ret < 0)
122 return ret;
1da177e4
LT
123
124 /* Register hooks */
2b95efe7
JE
125 mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook);
126 if (IS_ERR(mangle_ops)) {
127 ret = PTR_ERR(mangle_ops);
1da177e4 128 goto cleanup_table;
2b95efe7 129 }
1da177e4 130
1da177e4
LT
131 return ret;
132
1da177e4 133 cleanup_table:
8280aa61 134 unregister_pernet_subsys(&ip6table_mangle_net_ops);
1da177e4
LT
135 return ret;
136}
137
65b4b4e8 138static void __exit ip6table_mangle_fini(void)
1da177e4 139{
2b95efe7 140 xt_hook_unlink(&packet_mangler, mangle_ops);
8280aa61 141 unregister_pernet_subsys(&ip6table_mangle_net_ops);
1da177e4
LT
142}
143
65b4b4e8
AM
144module_init(ip6table_mangle_init);
145module_exit(ip6table_mangle_fini);