2 * IPv6 packet mangling table, a port of the IPv4 mangle table to IPv6
4 * Copyright (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
5 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
11 #include <linux/module.h>
12 #include <linux/netfilter_ipv6/ip6_tables.h>
14 MODULE_LICENSE("GPL");
15 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
16 MODULE_DESCRIPTION("ip6tables mangle table");
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))
26 struct ip6t_replace repl;
27 struct ip6t_standard entries[5];
28 struct ip6t_error term;
29 } initial_table __net_initdata = {
32 .valid_hooks = MANGLE_VALID_HOOKS,
34 .size = sizeof(struct ip6t_standard) * 5 + sizeof(struct ip6t_error),
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,
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,
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 */
57 .term = IP6T_ERROR_INIT, /* ERROR */
60 static const struct xt_table packet_mangler = {
62 .valid_hooks = MANGLE_VALID_HOOKS,
65 .priority = NF_IP6_PRI_MANGLE,
69 ip6t_local_out_hook(unsigned int hook,
71 const struct net_device *out,
72 int (*okfn)(struct sk_buff *))
76 struct in6_addr saddr, daddr;
78 u_int32_t flowlabel, mark;
81 /* root is playing with raw sockets. */
82 if (skb->len < sizeof(struct iphdr) ||
83 ip_hdrlen(skb) < sizeof(struct iphdr)) {
85 printk("ip6t_hook: happy cracking.\n");
90 /* save source/dest address, mark, hoplimit, flowlabel, priority, */
91 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
92 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
94 hop_limit = ipv6_hdr(skb)->hop_limit;
96 /* flowlabel and prio (includes version, which shouldn't change either */
97 flowlabel = *((u_int32_t *)ipv6_hdr(skb));
99 ret = ip6t_do_table(skb, hook, NULL, out,
100 dev_net(out)->ipv6.ip6table_mangle);
102 if (ret != NF_DROP && ret != NF_STOLEN &&
103 (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) ||
104 memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) ||
106 ipv6_hdr(skb)->hop_limit != hop_limit))
107 return ip6_route_me_harder(skb) == 0 ? ret : NF_DROP;
112 /* The work comes in here from netfilter.c. */
114 ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb,
115 const struct net_device *in, const struct net_device *out,
116 int (*okfn)(struct sk_buff *))
118 if (hook == NF_INET_LOCAL_OUT)
119 return ip6t_local_out_hook(hook, skb, out, okfn);
122 return ip6t_do_table(skb, hook, in, out,
123 dev_net(in)->ipv6.ip6table_mangle);
126 static struct nf_hook_ops *mangle_ops __read_mostly;
127 static int __net_init ip6table_mangle_net_init(struct net *net)
130 net->ipv6.ip6table_mangle =
131 ip6t_register_table(net, &packet_mangler, &initial_table.repl);
132 if (IS_ERR(net->ipv6.ip6table_mangle))
133 return PTR_ERR(net->ipv6.ip6table_mangle);
137 static void __net_exit ip6table_mangle_net_exit(struct net *net)
139 ip6t_unregister_table(net, net->ipv6.ip6table_mangle);
142 static struct pernet_operations ip6table_mangle_net_ops = {
143 .init = ip6table_mangle_net_init,
144 .exit = ip6table_mangle_net_exit,
147 static int __init ip6table_mangle_init(void)
151 ret = register_pernet_subsys(&ip6table_mangle_net_ops);
156 mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook);
157 if (IS_ERR(mangle_ops)) {
158 ret = PTR_ERR(mangle_ops);
165 unregister_pernet_subsys(&ip6table_mangle_net_ops);
169 static void __exit ip6table_mangle_fini(void)
171 xt_hook_unlink(&packet_mangler, mangle_ops);
172 unregister_pernet_subsys(&ip6table_mangle_net_ops);
175 module_init(ip6table_mangle_init);
176 module_exit(ip6table_mangle_fini);