ip6table_mangle.c 4.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * IPv6 packet mangling table, a port of the IPv4 mangle table to IPv6
 *
 * Copyright (C) 2000-2001 by Harald Welte <laforge@gnumonks.org>
 * Copyright (C) 2000-2004 Netfilter Core Team <coreteam@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/module.h>
#include <linux/netfilter_ipv6/ip6_tables.h>
13
#include <linux/slab.h>
14
#include <net/ipv6.h>
L
Linus Torvalds 已提交
15 16 17 18 19

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
MODULE_DESCRIPTION("ip6tables mangle table");

20 21 22 23 24
#define MANGLE_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | \
			    (1 << NF_INET_LOCAL_IN) | \
			    (1 << NF_INET_FORWARD) | \
			    (1 << NF_INET_LOCAL_OUT) | \
			    (1 << NF_INET_POST_ROUTING))
L
Linus Torvalds 已提交
25

26
static const struct xt_table packet_mangler = {
L
Linus Torvalds 已提交
27 28 29
	.name		= "mangle",
	.valid_hooks	= MANGLE_VALID_HOOKS,
	.me		= THIS_MODULE,
30
	.af		= NFPROTO_IPV6,
31
	.priority	= NF_IP6_PRI_MANGLE,
L
Linus Torvalds 已提交
32 33
};

34
static unsigned int
35
ip6t_mangle_out(struct sk_buff *skb, const struct net_device *out)
L
Linus Torvalds 已提交
36 37 38 39
{
	unsigned int ret;
	struct in6_addr saddr, daddr;
	u_int8_t hop_limit;
T
Thomas Graf 已提交
40
	u_int32_t flowlabel, mark;
41
	int err;
L
Linus Torvalds 已提交
42 43
#if 0
	/* root is playing with raw sockets. */
44 45
	if (skb->len < sizeof(struct iphdr) ||
	    ip_hdrlen(skb) < sizeof(struct iphdr)) {
46
		net_warn_ratelimited("ip6t_hook: happy cracking\n");
L
Linus Torvalds 已提交
47 48 49 50
		return NF_ACCEPT;
	}
#endif

T
Thomas Graf 已提交
51
	/* save source/dest address, mark, hoplimit, flowlabel, priority,  */
52 53 54 55
	memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
	memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
	mark = skb->mark;
	hop_limit = ipv6_hdr(skb)->hop_limit;
L
Linus Torvalds 已提交
56 57

	/* flowlabel and prio (includes version, which shouldn't change either */
58
	flowlabel = *((u_int32_t *)ipv6_hdr(skb));
L
Linus Torvalds 已提交
59

60
	ret = ip6t_do_table(skb, NF_INET_LOCAL_OUT, NULL, out,
61
			    dev_net(out)->ipv6.ip6table_mangle);
L
Linus Torvalds 已提交
62

63
	if (ret != NF_DROP && ret != NF_STOLEN &&
64 65
	    (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
	     !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
66
	     skb->mark != mark ||
67
	     ipv6_hdr(skb)->hop_limit != hop_limit ||
68 69 70 71 72
	     flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
		err = ip6_route_me_harder(skb);
		if (err < 0)
			ret = NF_DROP_ERR(err);
	}
L
Linus Torvalds 已提交
73 74 75 76

	return ret;
}

77 78 79 80 81 82 83
/* The work comes in here from netfilter.c. */
static unsigned int
ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb,
		     const struct net_device *in, const struct net_device *out,
		     int (*okfn)(struct sk_buff *))
{
	if (hook == NF_INET_LOCAL_OUT)
84
		return ip6t_mangle_out(skb, out);
85 86 87
	if (hook == NF_INET_POST_ROUTING)
		return ip6t_do_table(skb, hook, in, out,
				     dev_net(out)->ipv6.ip6table_mangle);
88 89 90 91 92
	/* INPUT/FORWARD */
	return ip6t_do_table(skb, hook, in, out,
			     dev_net(in)->ipv6.ip6table_mangle);
}

93
static struct nf_hook_ops *mangle_ops __read_mostly;
94 95
static int __net_init ip6table_mangle_net_init(struct net *net)
{
96 97 98 99 100
	struct ip6t_replace *repl;

	repl = ip6t_alloc_initial_table(&packet_mangler);
	if (repl == NULL)
		return -ENOMEM;
101
	net->ipv6.ip6table_mangle =
102 103
		ip6t_register_table(net, &packet_mangler, repl);
	kfree(repl);
104
	return PTR_ERR_OR_ZERO(net->ipv6.ip6table_mangle);
105 106 107 108
}

static void __net_exit ip6table_mangle_net_exit(struct net *net)
{
109
	ip6t_unregister_table(net, net->ipv6.ip6table_mangle);
110 111 112 113 114 115 116
}

static struct pernet_operations ip6table_mangle_net_ops = {
	.init = ip6table_mangle_net_init,
	.exit = ip6table_mangle_net_exit,
};

117
static int __init ip6table_mangle_init(void)
L
Linus Torvalds 已提交
118 119 120
{
	int ret;

121 122 123
	ret = register_pernet_subsys(&ip6table_mangle_net_ops);
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
124 125

	/* Register hooks */
126 127 128
	mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook);
	if (IS_ERR(mangle_ops)) {
		ret = PTR_ERR(mangle_ops);
L
Linus Torvalds 已提交
129
		goto cleanup_table;
130
	}
L
Linus Torvalds 已提交
131 132 133 134

	return ret;

 cleanup_table:
135
	unregister_pernet_subsys(&ip6table_mangle_net_ops);
L
Linus Torvalds 已提交
136 137 138
	return ret;
}

139
static void __exit ip6table_mangle_fini(void)
L
Linus Torvalds 已提交
140
{
141
	xt_hook_unlink(&packet_mangler, mangle_ops);
142
	unregister_pernet_subsys(&ip6table_mangle_net_ops);
L
Linus Torvalds 已提交
143 144
}

145 146
module_init(ip6table_mangle_init);
module_exit(ip6table_mangle_fini);