blob: 2b1a9dcdbcb3d08e0fe681065efc24748a63bd7a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11#include <linux/module.h>
12#include <linux/netfilter_ipv6/ip6_tables.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
YOSHIFUJI Hideaki / 吉藤英明d9e85652013-01-29 12:48:58 +000014#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
18MODULE_DESCRIPTION("ip6tables mangle table");
19
Patrick McHardy6e23ae22007-11-19 18:53:30 -080020#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))
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Florian Westphalb9e69e12016-02-25 10:08:36 +010026static int __net_init ip6table_mangle_table_init(struct net *net);
27
Jan Engelhardt35aad0f2009-08-24 14:56:30 +020028static const struct xt_table packet_mangler = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 .name = "mangle",
30 .valid_hooks = MANGLE_VALID_HOOKS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .me = THIS_MODULE,
Jan Engelhardtf88e6a82009-06-13 06:25:44 +020032 .af = NFPROTO_IPV6,
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020033 .priority = NF_IP6_PRI_MANGLE,
Florian Westphalb9e69e12016-02-25 10:08:36 +010034 .table_init = ip6table_mangle_table_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Alexey Dobriyan7dd1b8d2008-10-08 11:35:02 +020037static unsigned int
David S. Miller8f8a3712015-04-03 21:09:51 -040038ip6t_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 unsigned int ret;
41 struct in6_addr saddr, daddr;
42 u_int8_t hop_limit;
Thomas Graf82e91ff2006-11-09 15:19:14 -080043 u_int32_t flowlabel, mark;
Patrick McHardy58e35d12013-04-05 06:41:11 +000044 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#if 0
46 /* root is playing with raw sockets. */
Joe Perches3666ed12009-11-23 23:17:06 +010047 if (skb->len < sizeof(struct iphdr) ||
48 ip_hdrlen(skb) < sizeof(struct iphdr)) {
Joe Perchese87cc472012-05-13 21:56:26 +000049 net_warn_ratelimited("ip6t_hook: happy cracking\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return NF_ACCEPT;
51 }
52#endif
53
Thomas Graf82e91ff2006-11-09 15:19:14 -080054 /* save source/dest address, mark, hoplimit, flowlabel, priority, */
Herbert Xu3db05fe2007-10-15 00:53:15 -070055 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
56 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
57 mark = skb->mark;
58 hop_limit = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 /* flowlabel and prio (includes version, which shouldn't change either */
Herbert Xu3db05fe2007-10-15 00:53:15 -070061 flowlabel = *((u_int32_t *)ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Eric W. Biederman6cb8ff3f12015-09-18 14:32:55 -050063 ret = ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Joe Perches3666ed12009-11-23 23:17:06 +010065 if (ret != NF_DROP && ret != NF_STOLEN &&
YOSHIFUJI Hideaki / 吉藤英明d9e85652013-01-29 12:48:58 +000066 (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
67 !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
Joe Perches3666ed12009-11-23 23:17:06 +010068 skb->mark != mark ||
David S. Millerb169f6d2011-04-17 17:06:15 -070069 ipv6_hdr(skb)->hop_limit != hop_limit ||
Patrick McHardy58e35d12013-04-05 06:41:11 +000070 flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
Eric W. Biederman5f5d74d2015-09-25 15:07:31 -050071 err = ip6_route_me_harder(state->net, skb);
Patrick McHardy58e35d12013-04-05 06:41:11 +000072 if (err < 0)
73 ret = NF_DROP_ERR(err);
74 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 return ret;
77}
78
Jan Engelhardt737535c2009-06-13 06:46:36 +020079/* The work comes in here from netfilter.c. */
80static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -050081ip6table_mangle_hook(void *priv, struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040082 const struct nf_hook_state *state)
Jan Engelhardt737535c2009-06-13 06:46:36 +020083{
Eric W. Biederman6cb8ff3f12015-09-18 14:32:55 -050084 if (state->hook == NF_INET_LOCAL_OUT)
David S. Miller8f8a3712015-04-03 21:09:51 -040085 return ip6t_mangle_out(skb, state);
Eric W. Biederman6cb8ff3f12015-09-18 14:32:55 -050086 return ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle);
Jan Engelhardt737535c2009-06-13 06:46:36 +020087}
88
Jan Engelhardt2b95efe2009-06-17 13:57:48 +020089static struct nf_hook_ops *mangle_ops __read_mostly;
Florian Westphalb9e69e12016-02-25 10:08:36 +010090static int __net_init ip6table_mangle_table_init(struct net *net)
Alexey Dobriyan8280aa62008-01-31 04:04:13 -080091{
Jan Engelhardte3eaa992009-06-17 22:14:54 +020092 struct ip6t_replace *repl;
Florian Westphala67dd262016-02-25 10:08:35 +010093 int ret;
Jan Engelhardte3eaa992009-06-17 22:14:54 +020094
Florian Westphalb9e69e12016-02-25 10:08:36 +010095 if (net->ipv6.ip6table_mangle)
96 return 0;
97
Jan Engelhardte3eaa992009-06-17 22:14:54 +020098 repl = ip6t_alloc_initial_table(&packet_mangler);
99 if (repl == NULL)
100 return -ENOMEM;
Florian Westphala67dd262016-02-25 10:08:35 +0100101 ret = ip6t_register_table(net, &packet_mangler, repl, mangle_ops,
102 &net->ipv6.ip6table_mangle);
Jan Engelhardte3eaa992009-06-17 22:14:54 +0200103 kfree(repl);
Florian Westphala67dd262016-02-25 10:08:35 +0100104 return ret;
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800105}
106
107static void __net_exit ip6table_mangle_net_exit(struct net *net)
108{
Florian Westphalb9e69e12016-02-25 10:08:36 +0100109 if (!net->ipv6.ip6table_mangle)
110 return;
111
Florian Westphala67dd262016-02-25 10:08:35 +0100112 ip6t_unregister_table(net, net->ipv6.ip6table_mangle, mangle_ops);
Florian Westphalb9e69e12016-02-25 10:08:36 +0100113 net->ipv6.ip6table_mangle = NULL;
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800114}
115
116static struct pernet_operations ip6table_mangle_net_ops = {
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800117 .exit = ip6table_mangle_net_exit,
118};
119
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800120static int __init ip6table_mangle_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 int ret;
123
Florian Westphalb9e69e12016-02-25 10:08:36 +0100124 mangle_ops = xt_hook_ops_alloc(&packet_mangler, ip6table_mangle_hook);
125 if (IS_ERR(mangle_ops))
126 return PTR_ERR(mangle_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Florian Westphalb9e69e12016-02-25 10:08:36 +0100128 ret = register_pernet_subsys(&ip6table_mangle_net_ops);
129 if (ret < 0) {
130 kfree(mangle_ops);
131 return ret;
Jan Engelhardt2b95efe2009-06-17 13:57:48 +0200132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Florian Westphalb9e69e12016-02-25 10:08:36 +0100134 ret = ip6table_mangle_table_init(&init_net);
135 if (ret) {
136 unregister_pernet_subsys(&ip6table_mangle_net_ops);
137 kfree(mangle_ops);
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return ret;
140}
141
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800142static void __exit ip6table_mangle_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Alexey Dobriyan8280aa62008-01-31 04:04:13 -0800144 unregister_pernet_subsys(&ip6table_mangle_net_ops);
Florian Westphalb9e69e12016-02-25 10:08:36 +0100145 kfree(mangle_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800148module_init(ip6table_mangle_init);
149module_exit(ip6table_mangle_fini);