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))
24 static const struct xt_table packet_mangler = {
26 .valid_hooks = MANGLE_VALID_HOOKS,
29 .priority = NF_IP6_PRI_MANGLE,
33 ip6t_local_out_hook(unsigned int hook,
35 const struct net_device *out,
36 int (*okfn)(struct sk_buff *))
40 struct in6_addr saddr, daddr;
42 u_int32_t flowlabel, mark;
45 /* root is playing with raw sockets. */
46 if (skb->len < sizeof(struct iphdr) ||
47 ip_hdrlen(skb) < sizeof(struct iphdr)) {
49 printk("ip6t_hook: happy cracking.\n");
54 /* save source/dest address, mark, hoplimit, flowlabel, priority, */
55 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
56 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
58 hop_limit = ipv6_hdr(skb)->hop_limit;
60 /* flowlabel and prio (includes version, which shouldn't change either */
61 flowlabel = *((u_int32_t *)ipv6_hdr(skb));
63 ret = ip6t_do_table(skb, hook, NULL, out,
64 dev_net(out)->ipv6.ip6table_mangle);
66 if (ret != NF_DROP && ret != NF_STOLEN &&
67 (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) ||
68 memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) ||
70 ipv6_hdr(skb)->hop_limit != hop_limit))
71 return ip6_route_me_harder(skb) == 0 ? ret : NF_DROP;
76 /* The work comes in here from netfilter.c. */
78 ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb,
79 const struct net_device *in, const struct net_device *out,
80 int (*okfn)(struct sk_buff *))
82 if (hook == NF_INET_LOCAL_OUT)
83 return ip6t_local_out_hook(hook, skb, out, okfn);
86 return ip6t_do_table(skb, hook, in, out,
87 dev_net(in)->ipv6.ip6table_mangle);
90 static struct nf_hook_ops *mangle_ops __read_mostly;
91 static int __net_init ip6table_mangle_net_init(struct net *net)
93 struct ip6t_replace *repl;
95 repl = ip6t_alloc_initial_table(&packet_mangler);
98 net->ipv6.ip6table_mangle =
99 ip6t_register_table(net, &packet_mangler, repl);
101 if (IS_ERR(net->ipv6.ip6table_mangle))
102 return PTR_ERR(net->ipv6.ip6table_mangle);
106 static void __net_exit ip6table_mangle_net_exit(struct net *net)
108 ip6t_unregister_table(net, net->ipv6.ip6table_mangle);
111 static struct pernet_operations ip6table_mangle_net_ops = {
112 .init = ip6table_mangle_net_init,
113 .exit = ip6table_mangle_net_exit,
116 static int __init ip6table_mangle_init(void)
120 ret = register_pernet_subsys(&ip6table_mangle_net_ops);
125 mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook);
126 if (IS_ERR(mangle_ops)) {
127 ret = PTR_ERR(mangle_ops);
134 unregister_pernet_subsys(&ip6table_mangle_net_ops);
138 static void __exit ip6table_mangle_fini(void)
140 xt_hook_unlink(&packet_mangler, mangle_ops);
141 unregister_pernet_subsys(&ip6table_mangle_net_ops);
144 module_init(ip6table_mangle_init);
145 module_exit(ip6table_mangle_fini);