Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/netfilter_ipv6/ip6_tables.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
YOSHIFUJI Hideaki / 吉藤英明 | d9e8565 | 2013-01-29 12:48:58 +0000 | [diff] [blame] | 14 | #include <net/ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | MODULE_LICENSE("GPL"); |
| 17 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 18 | MODULE_DESCRIPTION("ip6tables mangle table"); |
| 19 | |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 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)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 26 | static int __net_init ip6table_mangle_table_init(struct net *net); |
| 27 | |
Jan Engelhardt | 35aad0f | 2009-08-24 14:56:30 +0200 | [diff] [blame] | 28 | static const struct xt_table packet_mangler = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | .name = "mangle", |
| 30 | .valid_hooks = MANGLE_VALID_HOOKS, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | .me = THIS_MODULE, |
Jan Engelhardt | f88e6a8 | 2009-06-13 06:25:44 +0200 | [diff] [blame] | 32 | .af = NFPROTO_IPV6, |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 33 | .priority = NF_IP6_PRI_MANGLE, |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 34 | .table_init = ip6table_mangle_table_init, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Alexey Dobriyan | 7dd1b8d | 2008-10-08 11:35:02 +0200 | [diff] [blame] | 37 | static unsigned int |
David S. Miller | 8f8a371 | 2015-04-03 21:09:51 -0400 | [diff] [blame] | 38 | ip6t_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | unsigned int ret; |
| 41 | struct in6_addr saddr, daddr; |
| 42 | u_int8_t hop_limit; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 43 | u_int32_t flowlabel, mark; |
Patrick McHardy | 58e35d1 | 2013-04-05 06:41:11 +0000 | [diff] [blame] | 44 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #if 0 |
| 46 | /* root is playing with raw sockets. */ |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 47 | if (skb->len < sizeof(struct iphdr) || |
| 48 | ip_hdrlen(skb) < sizeof(struct iphdr)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 49 | net_warn_ratelimited("ip6t_hook: happy cracking\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return NF_ACCEPT; |
| 51 | } |
| 52 | #endif |
| 53 | |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 54 | /* save source/dest address, mark, hoplimit, flowlabel, priority, */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 55 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | /* flowlabel and prio (includes version, which shouldn't change either */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 61 | flowlabel = *((u_int32_t *)ipv6_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Eric W. Biederman | 6cb8ff3f1 | 2015-09-18 14:32:55 -0500 | [diff] [blame] | 63 | ret = ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 65 | if (ret != NF_DROP && ret != NF_STOLEN && |
YOSHIFUJI Hideaki / 吉藤英明 | d9e8565 | 2013-01-29 12:48:58 +0000 | [diff] [blame] | 66 | (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) || |
| 67 | !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) || |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 68 | skb->mark != mark || |
David S. Miller | b169f6d | 2011-04-17 17:06:15 -0700 | [diff] [blame] | 69 | ipv6_hdr(skb)->hop_limit != hop_limit || |
Patrick McHardy | 58e35d1 | 2013-04-05 06:41:11 +0000 | [diff] [blame] | 70 | flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) { |
Eric W. Biederman | 5f5d74d | 2015-09-25 15:07:31 -0500 | [diff] [blame] | 71 | err = ip6_route_me_harder(state->net, skb); |
Patrick McHardy | 58e35d1 | 2013-04-05 06:41:11 +0000 | [diff] [blame] | 72 | if (err < 0) |
| 73 | ret = NF_DROP_ERR(err); |
| 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 79 | /* The work comes in here from netfilter.c. */ |
| 80 | static unsigned int |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 81 | ip6table_mangle_hook(void *priv, struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 82 | const struct nf_hook_state *state) |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 83 | { |
Eric W. Biederman | 6cb8ff3f1 | 2015-09-18 14:32:55 -0500 | [diff] [blame] | 84 | if (state->hook == NF_INET_LOCAL_OUT) |
David S. Miller | 8f8a371 | 2015-04-03 21:09:51 -0400 | [diff] [blame] | 85 | return ip6t_mangle_out(skb, state); |
Eric W. Biederman | 6cb8ff3f1 | 2015-09-18 14:32:55 -0500 | [diff] [blame] | 86 | return ip6t_do_table(skb, state, state->net->ipv6.ip6table_mangle); |
Jan Engelhardt | 737535c | 2009-06-13 06:46:36 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 89 | static struct nf_hook_ops *mangle_ops __read_mostly; |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 90 | static int __net_init ip6table_mangle_table_init(struct net *net) |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 91 | { |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 92 | struct ip6t_replace *repl; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 93 | int ret; |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 94 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 95 | if (net->ipv6.ip6table_mangle) |
| 96 | return 0; |
| 97 | |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 98 | repl = ip6t_alloc_initial_table(&packet_mangler); |
| 99 | if (repl == NULL) |
| 100 | return -ENOMEM; |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 101 | ret = ip6t_register_table(net, &packet_mangler, repl, mangle_ops, |
| 102 | &net->ipv6.ip6table_mangle); |
Jan Engelhardt | e3eaa99 | 2009-06-17 22:14:54 +0200 | [diff] [blame] | 103 | kfree(repl); |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 104 | return ret; |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void __net_exit ip6table_mangle_net_exit(struct net *net) |
| 108 | { |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 109 | if (!net->ipv6.ip6table_mangle) |
| 110 | return; |
| 111 | |
Florian Westphal | a67dd26 | 2016-02-25 10:08:35 +0100 | [diff] [blame] | 112 | ip6t_unregister_table(net, net->ipv6.ip6table_mangle, mangle_ops); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 113 | net->ipv6.ip6table_mangle = NULL; |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static struct pernet_operations ip6table_mangle_net_ops = { |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 117 | .exit = ip6table_mangle_net_exit, |
| 118 | }; |
| 119 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 120 | static int __init ip6table_mangle_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
| 122 | int ret; |
| 123 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 124 | mangle_ops = xt_hook_ops_alloc(&packet_mangler, ip6table_mangle_hook); |
| 125 | if (IS_ERR(mangle_ops)) |
| 126 | return PTR_ERR(mangle_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 128 | ret = register_pernet_subsys(&ip6table_mangle_net_ops); |
| 129 | if (ret < 0) { |
| 130 | kfree(mangle_ops); |
| 131 | return ret; |
Jan Engelhardt | 2b95efe | 2009-06-17 13:57:48 +0200 | [diff] [blame] | 132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 134 | ret = ip6table_mangle_table_init(&init_net); |
| 135 | if (ret) { |
| 136 | unregister_pernet_subsys(&ip6table_mangle_net_ops); |
| 137 | kfree(mangle_ops); |
| 138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 142 | static void __exit ip6table_mangle_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Alexey Dobriyan | 8280aa6 | 2008-01-31 04:04:13 -0800 | [diff] [blame] | 144 | unregister_pernet_subsys(&ip6table_mangle_net_ops); |
Florian Westphal | b9e69e1 | 2016-02-25 10:08:36 +0100 | [diff] [blame] | 145 | kfree(mangle_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 148 | module_init(ip6table_mangle_init); |
| 149 | module_exit(ip6table_mangle_fini); |