netfilter: Use nf_hook_state in nf_queue_entry.
[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>
5a0e3ad6 13#include <linux/slab.h>
d9e85655 14#include <net/ipv6.h>
1da177e4
LT
15
16MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
18MODULE_DESCRIPTION("ip6tables mangle table");
19
6e23ae2a
PM
20#define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
21 (1 << NF_INET_LOCAL_IN) | \
22 (1 << NF_INET_FORWARD) | \
23 (1 << NF_INET_LOCAL_OUT) | \
24 (1 << NF_INET_POST_ROUTING))
1da177e4 25
35aad0ff 26static const struct xt_table packet_mangler = {
1da177e4
LT
27 .name = "mangle",
28 .valid_hooks = MANGLE_VALID_HOOKS,
1da177e4 29 .me = THIS_MODULE,
f88e6a8a 30 .af = NFPROTO_IPV6,
2b95efe7 31 .priority = NF_IP6_PRI_MANGLE,
1da177e4
LT
32};
33
7dd1b8da 34static unsigned int
fa96a0e2 35ip6t_mangle_out(struct sk_buff *skb, const struct net_device *out)
1da177e4 36{
1da177e4
LT
37 unsigned int ret;
38 struct in6_addr saddr, daddr;
39 u_int8_t hop_limit;
82e91ffe 40 u_int32_t flowlabel, mark;
58e35d14 41 int err;
1da177e4
LT
42#if 0
43 /* root is playing with raw sockets. */
3666ed1c
JP
44 if (skb->len < sizeof(struct iphdr) ||
45 ip_hdrlen(skb) < sizeof(struct iphdr)) {
e87cc472 46 net_warn_ratelimited("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 63 if (ret != NF_DROP && ret != NF_STOLEN &&
d9e85655
YH
64 (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
65 !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
3666ed1c 66 skb->mark != mark ||
b169f6db 67 ipv6_hdr(skb)->hop_limit != hop_limit ||
58e35d14
PM
68 flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
69 err = ip6_route_me_harder(skb);
70 if (err < 0)
71 ret = NF_DROP_ERR(err);
72 }
1da177e4
LT
73
74 return ret;
75}
76
737535c5
JE
77/* The work comes in here from netfilter.c. */
78static unsigned int
795aa6ef 79ip6table_mangle_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
737535c5
JE
80 const struct net_device *in, const struct net_device *out,
81 int (*okfn)(struct sk_buff *))
82{
795aa6ef 83 if (ops->hooknum == NF_INET_LOCAL_OUT)
fa96a0e2 84 return ip6t_mangle_out(skb, out);
795aa6ef
PM
85 if (ops->hooknum == NF_INET_POST_ROUTING)
86 return ip6t_do_table(skb, ops->hooknum, in, out,
b2907e50 87 dev_net(out)->ipv6.ip6table_mangle);
737535c5 88 /* INPUT/FORWARD */
795aa6ef 89 return ip6t_do_table(skb, ops->hooknum, in, out,
737535c5
JE
90 dev_net(in)->ipv6.ip6table_mangle);
91}
92
2b95efe7 93static struct nf_hook_ops *mangle_ops __read_mostly;
8280aa61
AD
94static int __net_init ip6table_mangle_net_init(struct net *net)
95{
e3eaa991
JE
96 struct ip6t_replace *repl;
97
98 repl = ip6t_alloc_initial_table(&packet_mangler);
99 if (repl == NULL)
100 return -ENOMEM;
8280aa61 101 net->ipv6.ip6table_mangle =
e3eaa991
JE
102 ip6t_register_table(net, &packet_mangler, repl);
103 kfree(repl);
8c6ffba0 104 return PTR_ERR_OR_ZERO(net->ipv6.ip6table_mangle);
8280aa61
AD
105}
106
107static void __net_exit ip6table_mangle_net_exit(struct net *net)
108{
f54e9367 109 ip6t_unregister_table(net, net->ipv6.ip6table_mangle);
8280aa61
AD
110}
111
112static struct pernet_operations ip6table_mangle_net_ops = {
113 .init = ip6table_mangle_net_init,
114 .exit = ip6table_mangle_net_exit,
115};
116
65b4b4e8 117static int __init ip6table_mangle_init(void)
1da177e4
LT
118{
119 int ret;
120
8280aa61
AD
121 ret = register_pernet_subsys(&ip6table_mangle_net_ops);
122 if (ret < 0)
123 return ret;
1da177e4
LT
124
125 /* Register hooks */
2b95efe7
JE
126 mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook);
127 if (IS_ERR(mangle_ops)) {
128 ret = PTR_ERR(mangle_ops);
1da177e4 129 goto cleanup_table;
2b95efe7 130 }
1da177e4 131
1da177e4
LT
132 return ret;
133
1da177e4 134 cleanup_table:
8280aa61 135 unregister_pernet_subsys(&ip6table_mangle_net_ops);
1da177e4
LT
136 return ret;
137}
138
65b4b4e8 139static void __exit ip6table_mangle_fini(void)
1da177e4 140{
2b95efe7 141 xt_hook_unlink(&packet_mangler, mangle_ops);
8280aa61 142 unregister_pernet_subsys(&ip6table_mangle_net_ops);
1da177e4
LT
143}
144
65b4b4e8
AM
145module_init(ip6table_mangle_init);
146module_exit(ip6table_mangle_fini);
This page took 0.768634 seconds and 5 git commands to generate.