Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 2 | * IPv6 tunneling device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Linux INET6 implementation |
| 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Ville Nuorvala <vnuorval@tcs.hut.fi> |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 7 | * Yasuyuki Kozakai <kozakai@linux-ipv6.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Based on: |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 10 | * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | * RFC 2473 |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * as published by the Free Software Foundation; either version |
| 17 | * 2 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | */ |
| 20 | |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 24 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/errno.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/sockios.h> |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 28 | #include <linux/icmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/if.h> |
| 30 | #include <linux/in.h> |
| 31 | #include <linux/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/net.h> |
| 33 | #include <linux/in6.h> |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/if_arp.h> |
| 36 | #include <linux/icmpv6.h> |
| 37 | #include <linux/init.h> |
| 38 | #include <linux/route.h> |
| 39 | #include <linux/rtnetlink.h> |
| 40 | #include <linux/netfilter_ipv6.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 41 | #include <linux/slab.h> |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 42 | #include <linux/hash.h> |
Nicolas Dichtel | e837735 | 2013-08-20 12:16:06 +0200 | [diff] [blame] | 43 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #include <asm/uaccess.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 46 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 48 | #include <net/icmp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <net/ip.h> |
Pravin B Shelar | c544193 | 2013-03-25 14:49:35 +0000 | [diff] [blame] | 50 | #include <net/ip_tunnels.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <net/ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <net/ip6_route.h> |
| 53 | #include <net/addrconf.h> |
| 54 | #include <net/ip6_tunnel.h> |
| 55 | #include <net/xfrm.h> |
| 56 | #include <net/dsfield.h> |
| 57 | #include <net/inet_ecn.h> |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 58 | #include <net/net_namespace.h> |
| 59 | #include <net/netns/generic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | MODULE_AUTHOR("Ville Nuorvala"); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 62 | MODULE_DESCRIPTION("IPv6 tunneling device"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | MODULE_LICENSE("GPL"); |
Tom Gundersen | f98f89a | 2014-05-15 23:21:30 +0200 | [diff] [blame] | 64 | MODULE_ALIAS_RTNL_LINK("ip6tnl"); |
stephen hemminger | 6dfbd87 | 2011-03-10 11:43:19 +0000 | [diff] [blame] | 65 | MODULE_ALIAS_NETDEV("ip6tnl0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 67 | #define HASH_SIZE_SHIFT 5 |
| 68 | #define HASH_SIZE (1 << HASH_SIZE_SHIFT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Nicolas Dichtel | f4e0b4c | 2012-11-27 03:07:11 +0000 | [diff] [blame] | 70 | static bool log_ecn_error = true; |
| 71 | module_param(log_ecn_error, bool, 0644); |
| 72 | MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN"); |
| 73 | |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 74 | static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2) |
| 75 | { |
| 76 | u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2); |
| 77 | |
| 78 | return hash_32(hash, HASH_SIZE_SHIFT); |
| 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 81 | static int ip6_tnl_dev_init(struct net_device *dev); |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 82 | static void ip6_tnl_dev_setup(struct net_device *dev); |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 83 | static struct rtnl_link_ops ip6_link_ops __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Eric Dumazet | f99189b | 2009-11-17 10:42:49 +0000 | [diff] [blame] | 85 | static int ip6_tnl_net_id __read_mostly; |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 86 | struct ip6_tnl_net { |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 87 | /* the IPv6 tunnel fallback device */ |
| 88 | struct net_device *fb_tnl_dev; |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 89 | /* lists for storing tunnels in use */ |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 90 | struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE]; |
| 91 | struct ip6_tnl __rcu *tnls_wc[1]; |
| 92 | struct ip6_tnl __rcu **tnls[2]; |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 95 | static struct net_device_stats *ip6_get_stats(struct net_device *dev) |
| 96 | { |
David S. Miller | 56a4342 | 2014-01-06 17:37:45 -0500 | [diff] [blame] | 97 | struct pcpu_sw_netstats tmp, sum = { 0 }; |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 98 | int i; |
| 99 | |
| 100 | for_each_possible_cpu(i) { |
Li RongQing | abb6013 | 2014-01-02 13:20:12 +0800 | [diff] [blame] | 101 | unsigned int start; |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 102 | const struct pcpu_sw_netstats *tstats = |
| 103 | per_cpu_ptr(dev->tstats, i); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 104 | |
Li RongQing | abb6013 | 2014-01-02 13:20:12 +0800 | [diff] [blame] | 105 | do { |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 106 | start = u64_stats_fetch_begin_irq(&tstats->syncp); |
Li RongQing | abb6013 | 2014-01-02 13:20:12 +0800 | [diff] [blame] | 107 | tmp.rx_packets = tstats->rx_packets; |
| 108 | tmp.rx_bytes = tstats->rx_bytes; |
| 109 | tmp.tx_packets = tstats->tx_packets; |
| 110 | tmp.tx_bytes = tstats->tx_bytes; |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 111 | } while (u64_stats_fetch_retry_irq(&tstats->syncp, start)); |
Li RongQing | abb6013 | 2014-01-02 13:20:12 +0800 | [diff] [blame] | 112 | |
| 113 | sum.rx_packets += tmp.rx_packets; |
| 114 | sum.rx_bytes += tmp.rx_bytes; |
| 115 | sum.tx_packets += tmp.tx_packets; |
| 116 | sum.tx_bytes += tmp.tx_bytes; |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 117 | } |
| 118 | dev->stats.rx_packets = sum.rx_packets; |
| 119 | dev->stats.rx_bytes = sum.rx_bytes; |
| 120 | dev->stats.tx_packets = sum.tx_packets; |
| 121 | dev->stats.tx_bytes = sum.tx_bytes; |
| 122 | return &dev->stats; |
| 123 | } |
| 124 | |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 125 | /* |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 126 | * Locking : hash tables are protected by RCU and RTNL |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 127 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 129 | static void __ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst *idst, |
| 130 | struct dst_entry *dst) |
| 131 | { |
| 132 | dst_release(idst->dst); |
| 133 | if (dst) { |
| 134 | dst_hold(dst); |
| 135 | idst->cookie = rt6_get_cookie((struct rt6_info *)dst); |
| 136 | } else { |
| 137 | idst->cookie = 0; |
| 138 | } |
| 139 | idst->dst = dst; |
| 140 | } |
| 141 | |
| 142 | static void ip6_tnl_per_cpu_dst_set(struct ip6_tnl_dst *idst, |
| 143 | struct dst_entry *dst) |
| 144 | { |
| 145 | |
| 146 | spin_lock_bh(&idst->lock); |
| 147 | __ip6_tnl_per_cpu_dst_set(idst, dst); |
| 148 | spin_unlock_bh(&idst->lock); |
| 149 | } |
| 150 | |
Martin KaFai Lau | f230d1e | 2015-09-15 14:30:06 -0700 | [diff] [blame] | 151 | struct dst_entry *ip6_tnl_dst_get(struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 153 | struct ip6_tnl_dst *idst; |
| 154 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 156 | idst = raw_cpu_ptr(t->dst_cache); |
| 157 | spin_lock_bh(&idst->lock); |
| 158 | dst = idst->dst; |
| 159 | if (dst) { |
| 160 | if (!dst->obsolete || dst->ops->check(dst, idst->cookie)) { |
| 161 | dst_hold(idst->dst); |
| 162 | } else { |
| 163 | __ip6_tnl_per_cpu_dst_set(idst, NULL); |
| 164 | dst = NULL; |
| 165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 167 | spin_unlock_bh(&idst->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return dst; |
| 169 | } |
Martin KaFai Lau | f230d1e | 2015-09-15 14:30:06 -0700 | [diff] [blame] | 170 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 172 | void ip6_tnl_dst_reset(struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 174 | int i; |
| 175 | |
| 176 | for_each_possible_cpu(i) |
| 177 | ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 179 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Martin KaFai Lau | f230d1e | 2015-09-15 14:30:06 -0700 | [diff] [blame] | 181 | void ip6_tnl_dst_set(struct ip6_tnl *t, struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 183 | ip6_tnl_per_cpu_dst_set(raw_cpu_ptr(t->dst_cache), dst); |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
Martin KaFai Lau | f230d1e | 2015-09-15 14:30:06 -0700 | [diff] [blame] | 186 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 188 | void ip6_tnl_dst_destroy(struct ip6_tnl *t) |
| 189 | { |
| 190 | if (!t->dst_cache) |
| 191 | return; |
| 192 | |
| 193 | ip6_tnl_dst_reset(t); |
| 194 | free_percpu(t->dst_cache); |
| 195 | } |
| 196 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_destroy); |
| 197 | |
| 198 | int ip6_tnl_dst_init(struct ip6_tnl *t) |
| 199 | { |
| 200 | int i; |
| 201 | |
| 202 | t->dst_cache = alloc_percpu(struct ip6_tnl_dst); |
| 203 | if (!t->dst_cache) |
| 204 | return -ENOMEM; |
| 205 | |
| 206 | for_each_possible_cpu(i) |
| 207 | spin_lock_init(&per_cpu_ptr(t->dst_cache, i)->lock); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | EXPORT_SYMBOL_GPL(ip6_tnl_dst_init); |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 214 | * ip6_tnl_lookup - fetch tunnel matching the end-point addresses |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 215 | * @remote: the address of the tunnel exit-point |
| 216 | * @local: the address of the tunnel entry-point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 218 | * Return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | * tunnel matching given end-points if found, |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 220 | * else fallback tunnel if its device is up, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | * else %NULL |
| 222 | **/ |
| 223 | |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 224 | #define for_each_ip6_tunnel_rcu(start) \ |
| 225 | for (t = rcu_dereference(start); t; t = rcu_dereference(t->next)) |
| 226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | static struct ip6_tnl * |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 228 | ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 230 | unsigned int hash = HASH(remote, local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | struct ip6_tnl *t; |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 232 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
Steffen Klassert | ea3dc96 | 2014-11-05 08:03:50 +0100 | [diff] [blame] | 233 | struct in6_addr any; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 235 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
| 237 | ipv6_addr_equal(remote, &t->parms.raddr) && |
| 238 | (t->dev->flags & IFF_UP)) |
| 239 | return t; |
| 240 | } |
Steffen Klassert | ea3dc96 | 2014-11-05 08:03:50 +0100 | [diff] [blame] | 241 | |
| 242 | memset(&any, 0, sizeof(any)); |
| 243 | hash = HASH(&any, local); |
| 244 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
| 245 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
| 246 | (t->dev->flags & IFF_UP)) |
| 247 | return t; |
| 248 | } |
| 249 | |
| 250 | hash = HASH(remote, &any); |
| 251 | for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) { |
| 252 | if (ipv6_addr_equal(remote, &t->parms.raddr) && |
| 253 | (t->dev->flags & IFF_UP)) |
| 254 | return t; |
| 255 | } |
| 256 | |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 257 | t = rcu_dereference(ip6n->tnls_wc[0]); |
| 258 | if (t && (t->dev->flags & IFF_UP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return t; |
| 260 | |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 265 | * ip6_tnl_bucket - get head of list matching given tunnel parameters |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 266 | * @p: parameters containing tunnel end-points |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | * |
| 268 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 269 | * ip6_tnl_bucket() returns the head of the list matching the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * &struct in6_addr entries laddr and raddr in @p. |
| 271 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 272 | * Return: head of IPv6 tunnel list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | **/ |
| 274 | |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 275 | static struct ip6_tnl __rcu ** |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 276 | ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 278 | const struct in6_addr *remote = &p->raddr; |
| 279 | const struct in6_addr *local = &p->laddr; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 280 | unsigned int h = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | int prio = 0; |
| 282 | |
| 283 | if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { |
| 284 | prio = 1; |
Eric Dumazet | ddbe503 | 2012-07-18 08:11:12 +0000 | [diff] [blame] | 285 | h = HASH(remote, local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 287 | return &ip6n->tnls[prio][h]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 291 | * ip6_tnl_link - add tunnel to hash table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | * @t: tunnel to be added |
| 293 | **/ |
| 294 | |
| 295 | static void |
Pavel Emelyanov | 2dd02c8 | 2008-04-16 01:22:23 -0700 | [diff] [blame] | 296 | ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 298 | struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 300 | rcu_assign_pointer(t->next , rtnl_dereference(*tp)); |
| 301 | rcu_assign_pointer(*tp, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 305 | * ip6_tnl_unlink - remove tunnel from hash table |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | * @t: tunnel to be removed |
| 307 | **/ |
| 308 | |
| 309 | static void |
Pavel Emelyanov | 2dd02c8 | 2008-04-16 01:22:23 -0700 | [diff] [blame] | 310 | ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 312 | struct ip6_tnl __rcu **tp; |
| 313 | struct ip6_tnl *iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 315 | for (tp = ip6_tnl_bucket(ip6n, &t->parms); |
| 316 | (iter = rtnl_dereference(*tp)) != NULL; |
| 317 | tp = &iter->next) { |
| 318 | if (t == iter) { |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 319 | rcu_assign_pointer(*tp, t->next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 325 | static void ip6_dev_free(struct net_device *dev) |
| 326 | { |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 327 | struct ip6_tnl *t = netdev_priv(dev); |
| 328 | |
| 329 | ip6_tnl_dst_destroy(t); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 330 | free_percpu(dev->tstats); |
| 331 | free_netdev(dev); |
| 332 | } |
| 333 | |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 334 | static int ip6_tnl_create2(struct net_device *dev) |
| 335 | { |
| 336 | struct ip6_tnl *t = netdev_priv(dev); |
| 337 | struct net *net = dev_net(dev); |
| 338 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 339 | int err; |
| 340 | |
| 341 | t = netdev_priv(dev); |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 342 | |
| 343 | err = register_netdevice(dev); |
| 344 | if (err < 0) |
| 345 | goto out; |
| 346 | |
| 347 | strcpy(t->parms.name, dev->name); |
| 348 | dev->rtnl_link_ops = &ip6_link_ops; |
| 349 | |
| 350 | dev_hold(dev); |
| 351 | ip6_tnl_link(ip6n, t); |
| 352 | return 0; |
| 353 | |
| 354 | out: |
| 355 | return err; |
| 356 | } |
| 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | /** |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 359 | * ip6_tnl_create - create a new tunnel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | * @p: tunnel parameters |
| 361 | * @pt: pointer to new tunnel |
| 362 | * |
| 363 | * Description: |
| 364 | * Create tunnel matching given parameters. |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 365 | * |
| 366 | * Return: |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 367 | * created tunnel or error pointer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | **/ |
| 369 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 370 | static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | { |
| 372 | struct net_device *dev; |
| 373 | struct ip6_tnl *t; |
| 374 | char name[IFNAMSIZ]; |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 375 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Pavel Emelyanov | 34cc7ba | 2008-02-23 20:19:20 -0800 | [diff] [blame] | 377 | if (p->name[0]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | strlcpy(name, p->name, IFNAMSIZ); |
Pavel Emelyanov | 34cc7ba | 2008-02-23 20:19:20 -0800 | [diff] [blame] | 379 | else |
| 380 | sprintf(name, "ip6tnl%%d"); |
| 381 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 382 | dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, |
| 383 | ip6_tnl_dev_setup); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 384 | if (!dev) |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 385 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Pavel Emelyanov | 554eb27 | 2008-04-16 01:24:13 -0700 | [diff] [blame] | 387 | dev_net_set(dev, net); |
| 388 | |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 389 | t = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | t->parms = *p; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 391 | t->net = dev_net(dev); |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 392 | err = ip6_tnl_create2(dev); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 393 | if (err < 0) |
| 394 | goto failed_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 396 | return t; |
Pavel Emelyanov | b37d428b | 2008-02-26 23:51:04 -0800 | [diff] [blame] | 397 | |
| 398 | failed_free: |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 399 | ip6_dev_free(dev); |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 400 | failed: |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 401 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 405 | * ip6_tnl_locate - find or create tunnel matching given parameters |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 406 | * @p: tunnel parameters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | * @create: != 0 if allowed to create new tunnel if no match found |
| 408 | * |
| 409 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 410 | * ip6_tnl_locate() first tries to locate an existing tunnel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * based on @parms. If this is unsuccessful, but @create is set a new |
| 412 | * tunnel device is created and registered for use. |
| 413 | * |
| 414 | * Return: |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 415 | * matching tunnel or error pointer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | **/ |
| 417 | |
Pavel Emelyanov | 2dd02c8 | 2008-04-16 01:22:23 -0700 | [diff] [blame] | 418 | static struct ip6_tnl *ip6_tnl_locate(struct net *net, |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 419 | struct __ip6_tnl_parm *p, int create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 421 | const struct in6_addr *remote = &p->raddr; |
| 422 | const struct in6_addr *local = &p->laddr; |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 423 | struct ip6_tnl __rcu **tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | struct ip6_tnl *t; |
Pavel Emelyanov | 2dd02c8 | 2008-04-16 01:22:23 -0700 | [diff] [blame] | 425 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 427 | for (tp = ip6_tnl_bucket(ip6n, p); |
| 428 | (t = rtnl_dereference(*tp)) != NULL; |
| 429 | tp = &t->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | if (ipv6_addr_equal(local, &t->parms.laddr) && |
Steffen Klassert | 2b0bb01 | 2014-09-22 10:07:24 +0200 | [diff] [blame] | 431 | ipv6_addr_equal(remote, &t->parms.raddr)) { |
| 432 | if (create) |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 433 | return ERR_PTR(-EEXIST); |
Steffen Klassert | 2b0bb01 | 2014-09-22 10:07:24 +0200 | [diff] [blame] | 434 | |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 435 | return t; |
Steffen Klassert | 2b0bb01 | 2014-09-22 10:07:24 +0200 | [diff] [blame] | 436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | if (!create) |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 439 | return ERR_PTR(-ENODEV); |
Pavel Emelyanov | 2dd02c8 | 2008-04-16 01:22:23 -0700 | [diff] [blame] | 440 | return ip6_tnl_create(net, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 444 | * ip6_tnl_dev_uninit - tunnel device uninitializer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | * @dev: the device to be destroyed |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 446 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 448 | * ip6_tnl_dev_uninit() removes tunnel from its list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | **/ |
| 450 | |
| 451 | static void |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 452 | ip6_tnl_dev_uninit(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 454 | struct ip6_tnl *t = netdev_priv(dev); |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 455 | struct net *net = t->net; |
Pavel Emelyanov | 2dd02c8 | 2008-04-16 01:22:23 -0700 | [diff] [blame] | 456 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 458 | if (dev == ip6n->fb_tnl_dev) |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 459 | RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL); |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 460 | else |
Pavel Emelyanov | 2dd02c8 | 2008-04-16 01:22:23 -0700 | [diff] [blame] | 461 | ip6_tnl_unlink(ip6n, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | ip6_tnl_dst_reset(t); |
| 463 | dev_put(dev); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * parse_tvl_tnl_enc_lim - handle encapsulation limit option |
| 468 | * @skb: received socket buffer |
| 469 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 470 | * Return: |
| 471 | * 0 if none was found, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | * else index to encapsulation limit |
| 473 | **/ |
| 474 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 475 | __u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 477 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | __u8 nexthdr = ipv6h->nexthdr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 479 | __u16 off = sizeof(*ipv6h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { |
| 482 | __u16 optlen = 0; |
| 483 | struct ipv6_opt_hdr *hdr; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 484 | if (raw + off + sizeof(*hdr) > skb->data && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr))) |
| 486 | break; |
| 487 | |
| 488 | hdr = (struct ipv6_opt_hdr *) (raw + off); |
| 489 | if (nexthdr == NEXTHDR_FRAGMENT) { |
| 490 | struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; |
| 491 | if (frag_hdr->frag_off) |
| 492 | break; |
| 493 | optlen = 8; |
| 494 | } else if (nexthdr == NEXTHDR_AUTH) { |
| 495 | optlen = (hdr->hdrlen + 2) << 2; |
| 496 | } else { |
| 497 | optlen = ipv6_optlen(hdr); |
| 498 | } |
| 499 | if (nexthdr == NEXTHDR_DEST) { |
| 500 | __u16 i = off + 2; |
| 501 | while (1) { |
| 502 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 503 | |
| 504 | /* No more room for encapsulation limit */ |
| 505 | if (i + sizeof (*tel) > off + optlen) |
| 506 | break; |
| 507 | |
| 508 | tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i]; |
| 509 | /* return index of option if found and valid */ |
| 510 | if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && |
| 511 | tel->length == 1) |
| 512 | return i; |
| 513 | /* else jump to next option */ |
| 514 | if (tel->type) |
| 515 | i += tel->length + 2; |
| 516 | else |
| 517 | i++; |
| 518 | } |
| 519 | } |
| 520 | nexthdr = hdr->nexthdr; |
| 521 | off += optlen; |
| 522 | } |
| 523 | return 0; |
| 524 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 525 | EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | /** |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 528 | * ip6_tnl_err - tunnel error handler |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | * |
| 530 | * Description: |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 531 | * ip6_tnl_err() should handle errors in the tunnel according |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | * to the specifications in RFC 2473. |
| 533 | **/ |
| 534 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 535 | static int |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 536 | ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt, |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 537 | u8 *type, u8 *code, int *msg, __u32 *info, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 539 | const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | struct ip6_tnl *t; |
| 541 | int rel_msg = 0; |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 542 | u8 rel_type = ICMPV6_DEST_UNREACH; |
| 543 | u8 rel_code = ICMPV6_ADDR_UNREACH; |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 544 | u8 tproto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | __u32 rel_info = 0; |
| 546 | __u16 len; |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 547 | int err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 549 | /* If the packet doesn't contain the original IPv6 header we are |
| 550 | in trouble since we might need the source address for further |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | processing of the error. */ |
| 552 | |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 553 | rcu_read_lock(); |
Ian Morris | e5d08d7 | 2014-11-23 21:28:43 +0000 | [diff] [blame] | 554 | t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 555 | if (!t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | goto out; |
| 557 | |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 558 | tproto = ACCESS_ONCE(t->parms.proto); |
| 559 | if (tproto != ipproto && tproto != 0) |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 560 | goto out; |
| 561 | |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 562 | err = 0; |
| 563 | |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 564 | switch (*type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | __u32 teli; |
| 566 | struct ipv6_tlv_tnl_enc_lim *tel; |
| 567 | __u32 mtu; |
| 568 | case ICMPV6_DEST_UNREACH: |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 569 | net_warn_ratelimited("%s: Path to destination invalid or inactive!\n", |
| 570 | t->parms.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | rel_msg = 1; |
| 572 | break; |
| 573 | case ICMPV6_TIME_EXCEED: |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 574 | if ((*code) == ICMPV6_EXC_HOPLIMIT) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 575 | net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n", |
| 576 | t->parms.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | rel_msg = 1; |
| 578 | } |
| 579 | break; |
| 580 | case ICMPV6_PARAMPROB: |
Ville Nuorvala | 107a5fe | 2006-11-24 17:08:58 -0800 | [diff] [blame] | 581 | teli = 0; |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 582 | if ((*code) == ICMPV6_HDR_FIELD) |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 583 | teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 585 | if (teli && teli == *info - 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; |
| 587 | if (tel->encap_limit == 0) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 588 | net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n", |
| 589 | t->parms.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | rel_msg = 1; |
| 591 | } |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 592 | } else { |
| 593 | net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n", |
| 594 | t->parms.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | break; |
| 597 | case ICMPV6_PKT_TOOBIG: |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 598 | mtu = *info - offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | if (mtu < IPV6_MIN_MTU) |
| 600 | mtu = IPV6_MIN_MTU; |
| 601 | t->dev->mtu = mtu; |
| 602 | |
Ian Morris | e5d08d7 | 2014-11-23 21:28:43 +0000 | [diff] [blame] | 603 | len = sizeof(*ipv6h) + ntohs(ipv6h->payload_len); |
| 604 | if (len > mtu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | rel_type = ICMPV6_PKT_TOOBIG; |
| 606 | rel_code = 0; |
| 607 | rel_info = mtu; |
| 608 | rel_msg = 1; |
| 609 | } |
| 610 | break; |
| 611 | } |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 612 | |
| 613 | *type = rel_type; |
| 614 | *code = rel_code; |
| 615 | *info = rel_info; |
| 616 | *msg = rel_msg; |
| 617 | |
| 618 | out: |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 619 | rcu_read_unlock(); |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 620 | return err; |
| 621 | } |
| 622 | |
| 623 | static int |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 624 | ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 625 | u8 type, u8 code, int offset, __be32 info) |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 626 | { |
| 627 | int rel_msg = 0; |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 628 | u8 rel_type = type; |
| 629 | u8 rel_code = code; |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 630 | __u32 rel_info = ntohl(info); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 631 | int err; |
| 632 | struct sk_buff *skb2; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 633 | const struct iphdr *eiph; |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 634 | struct rtable *rt; |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 635 | struct flowi4 fl4; |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 636 | |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 637 | err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code, |
| 638 | &rel_msg, &rel_info, offset); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 639 | if (err < 0) |
| 640 | return err; |
| 641 | |
| 642 | if (rel_msg == 0) |
| 643 | return 0; |
| 644 | |
| 645 | switch (rel_type) { |
| 646 | case ICMPV6_DEST_UNREACH: |
| 647 | if (rel_code != ICMPV6_ADDR_UNREACH) |
| 648 | return 0; |
| 649 | rel_type = ICMP_DEST_UNREACH; |
| 650 | rel_code = ICMP_HOST_UNREACH; |
| 651 | break; |
| 652 | case ICMPV6_PKT_TOOBIG: |
| 653 | if (rel_code != 0) |
| 654 | return 0; |
| 655 | rel_type = ICMP_DEST_UNREACH; |
| 656 | rel_code = ICMP_FRAG_NEEDED; |
| 657 | break; |
David S. Miller | ec18d9a | 2012-07-12 00:25:15 -0700 | [diff] [blame] | 658 | case NDISC_REDIRECT: |
| 659 | rel_type = ICMP_REDIRECT; |
| 660 | rel_code = ICMP_REDIR_HOST; |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 661 | default: |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | if (!pskb_may_pull(skb, offset + sizeof(struct iphdr))) |
| 666 | return 0; |
| 667 | |
| 668 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 669 | if (!skb2) |
| 670 | return 0; |
| 671 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 672 | skb_dst_drop(skb2); |
| 673 | |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 674 | skb_pull(skb2, offset); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 675 | skb_reset_network_header(skb2); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 676 | eiph = ip_hdr(skb2); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 677 | |
| 678 | /* Try to guess incoming interface */ |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 679 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 680 | eiph->saddr, 0, |
| 681 | 0, 0, |
| 682 | IPPROTO_IPIP, RT_TOS(eiph->tos), 0); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 683 | if (IS_ERR(rt)) |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 684 | goto out; |
| 685 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 686 | skb2->dev = rt->dst.dev; |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 687 | |
| 688 | /* route "incoming" packet */ |
| 689 | if (rt->rt_flags & RTCF_LOCAL) { |
| 690 | ip_rt_put(rt); |
| 691 | rt = NULL; |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 692 | rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 693 | eiph->daddr, eiph->saddr, |
| 694 | 0, 0, |
| 695 | IPPROTO_IPIP, |
| 696 | RT_TOS(eiph->tos), 0); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 697 | if (IS_ERR(rt) || |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 698 | rt->dst.dev->type != ARPHRD_TUNNEL) { |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 699 | if (!IS_ERR(rt)) |
| 700 | ip_rt_put(rt); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 701 | goto out; |
| 702 | } |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 703 | skb_dst_set(skb2, &rt->dst); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 704 | } else { |
| 705 | ip_rt_put(rt); |
| 706 | if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos, |
| 707 | skb2->dev) || |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 708 | skb_dst(skb2)->dev->type != ARPHRD_TUNNEL) |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 709 | goto out; |
| 710 | } |
| 711 | |
| 712 | /* change mtu on this route */ |
| 713 | if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) { |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 714 | if (rel_info > dst_mtu(skb_dst(skb2))) |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 715 | goto out; |
| 716 | |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 717 | skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 718 | } |
David S. Miller | 1ed5c48 | 2012-07-12 00:41:25 -0700 | [diff] [blame] | 719 | if (rel_type == ICMP_REDIRECT) |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 720 | skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 721 | |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 722 | icmp_send(skb2, rel_type, rel_code, htonl(rel_info)); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 723 | |
| 724 | out: |
| 725 | kfree_skb(skb2); |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | static int |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 730 | ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 731 | u8 type, u8 code, int offset, __be32 info) |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 732 | { |
| 733 | int rel_msg = 0; |
Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 734 | u8 rel_type = type; |
| 735 | u8 rel_code = code; |
Al Viro | 704eae1 | 2007-07-26 17:33:29 +0100 | [diff] [blame] | 736 | __u32 rel_info = ntohl(info); |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 737 | int err; |
| 738 | |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 739 | err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code, |
| 740 | &rel_msg, &rel_info, offset); |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 741 | if (err < 0) |
| 742 | return err; |
| 743 | |
| 744 | if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | struct rt6_info *rt; |
| 746 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); |
Ville Nuorvala | 305d4b3 | 2006-11-24 17:06:53 -0800 | [diff] [blame] | 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | if (!skb2) |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 749 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 751 | skb_dst_drop(skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | skb_pull(skb2, offset); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 753 | skb_reset_network_header(skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
| 755 | /* Try to guess incoming interface */ |
Pavel Emelyanov | 2f7f54b | 2008-04-16 01:23:44 -0700 | [diff] [blame] | 756 | rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, |
| 757 | NULL, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 759 | if (rt && rt->dst.dev) |
| 760 | skb2->dev = rt->dst.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 762 | icmpv6_send(skb2, rel_type, rel_code, rel_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 764 | ip6_rt_put(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | kfree_skb(skb2); |
| 767 | } |
Yasuyuki Kozakai | e490d1d | 2006-10-31 23:11:25 +0900 | [diff] [blame] | 768 | |
| 769 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Nicolas Dichtel | f4e0b4c | 2012-11-27 03:07:11 +0000 | [diff] [blame] | 772 | static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
| 773 | const struct ipv6hdr *ipv6h, |
| 774 | struct sk_buff *skb) |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 775 | { |
| 776 | __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK; |
| 777 | |
| 778 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 779 | ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 780 | |
Nicolas Dichtel | f4e0b4c | 2012-11-27 03:07:11 +0000 | [diff] [blame] | 781 | return IP6_ECN_decapsulate(ipv6h, skb); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 782 | } |
| 783 | |
Nicolas Dichtel | f4e0b4c | 2012-11-27 03:07:11 +0000 | [diff] [blame] | 784 | static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t, |
| 785 | const struct ipv6hdr *ipv6h, |
| 786 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | { |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 788 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) |
Herbert Xu | 29bb43b4 | 2007-11-13 21:40:13 -0800 | [diff] [blame] | 789 | ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
Nicolas Dichtel | f4e0b4c | 2012-11-27 03:07:11 +0000 | [diff] [blame] | 791 | return IP6_ECN_decapsulate(ipv6h, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 793 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 794 | __u32 ip6_tnl_get_cap(struct ip6_tnl *t, |
Ville Nuorvala | d0087b2 | 2012-06-28 18:15:52 +0000 | [diff] [blame] | 795 | const struct in6_addr *laddr, |
| 796 | const struct in6_addr *raddr) |
| 797 | { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 798 | struct __ip6_tnl_parm *p = &t->parms; |
Ville Nuorvala | d0087b2 | 2012-06-28 18:15:52 +0000 | [diff] [blame] | 799 | int ltype = ipv6_addr_type(laddr); |
| 800 | int rtype = ipv6_addr_type(raddr); |
| 801 | __u32 flags = 0; |
| 802 | |
| 803 | if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) { |
| 804 | flags = IP6_TNL_F_CAP_PER_PACKET; |
| 805 | } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && |
| 806 | rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) && |
| 807 | !((ltype|rtype) & IPV6_ADDR_LOOPBACK) && |
| 808 | (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) { |
| 809 | if (ltype&IPV6_ADDR_UNICAST) |
| 810 | flags |= IP6_TNL_F_CAP_XMIT; |
| 811 | if (rtype&IPV6_ADDR_UNICAST) |
| 812 | flags |= IP6_TNL_F_CAP_RCV; |
| 813 | } |
| 814 | return flags; |
| 815 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 816 | EXPORT_SYMBOL(ip6_tnl_get_cap); |
Ville Nuorvala | d0087b2 | 2012-06-28 18:15:52 +0000 | [diff] [blame] | 817 | |
Eric Dumazet | f1a28ea | 2009-11-02 11:21:37 +0100 | [diff] [blame] | 818 | /* called with rcu_read_lock() */ |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 819 | int ip6_tnl_rcv_ctl(struct ip6_tnl *t, |
Ville Nuorvala | d0087b2 | 2012-06-28 18:15:52 +0000 | [diff] [blame] | 820 | const struct in6_addr *laddr, |
| 821 | const struct in6_addr *raddr) |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 822 | { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 823 | struct __ip6_tnl_parm *p = &t->parms; |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 824 | int ret = 0; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 825 | struct net *net = t->net; |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 826 | |
Ville Nuorvala | d0087b2 | 2012-06-28 18:15:52 +0000 | [diff] [blame] | 827 | if ((p->flags & IP6_TNL_F_CAP_RCV) || |
| 828 | ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && |
| 829 | (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 830 | struct net_device *ldev = NULL; |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 831 | |
| 832 | if (p->link) |
Eric Dumazet | f1a28ea | 2009-11-02 11:21:37 +0100 | [diff] [blame] | 833 | ldev = dev_get_by_index_rcu(net, p->link); |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 834 | |
Ville Nuorvala | d0087b2 | 2012-06-28 18:15:52 +0000 | [diff] [blame] | 835 | if ((ipv6_addr_is_multicast(laddr) || |
| 836 | likely(ipv6_chk_addr(net, laddr, ldev, 0))) && |
| 837 | likely(!ipv6_chk_addr(net, raddr, NULL, 0))) |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 838 | ret = 1; |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 839 | } |
| 840 | return ret; |
| 841 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 842 | EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
| 844 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 845 | * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | * @skb: received socket buffer |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 847 | * @protocol: ethernet protocol ID |
| 848 | * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | * |
| 850 | * Return: 0 |
| 851 | **/ |
| 852 | |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 853 | static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol, |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 854 | __u8 ipproto, |
Nicolas Dichtel | f4e0b4c | 2012-11-27 03:07:11 +0000 | [diff] [blame] | 855 | int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t, |
| 856 | const struct ipv6hdr *ipv6h, |
| 857 | struct sk_buff *skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | struct ip6_tnl *t; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 860 | const struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 861 | u8 tproto; |
Nicolas Dichtel | f4e0b4c | 2012-11-27 03:07:11 +0000 | [diff] [blame] | 862 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 864 | rcu_read_lock(); |
Ian Morris | e5d08d7 | 2014-11-23 21:28:43 +0000 | [diff] [blame] | 865 | t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr); |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 866 | if (t) { |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 867 | struct pcpu_sw_netstats *tstats; |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 868 | |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 869 | tproto = ACCESS_ONCE(t->parms.proto); |
| 870 | if (tproto != ipproto && tproto != 0) { |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 871 | rcu_read_unlock(); |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 872 | goto discard; |
| 873 | } |
| 874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 876 | rcu_read_unlock(); |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 877 | goto discard; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Ville Nuorvala | d0087b2 | 2012-06-28 18:15:52 +0000 | [diff] [blame] | 880 | if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) { |
Pavel Emelyanov | 3dca02a | 2008-05-21 14:17:05 -0700 | [diff] [blame] | 881 | t->dev->stats.rx_dropped++; |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 882 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | goto discard; |
| 884 | } |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 885 | skb->mac_header = skb->network_header; |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 886 | skb_reset_network_header(skb); |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 887 | skb->protocol = htons(protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); |
Eric Dumazet | d19d56d | 2010-05-17 22:36:55 -0700 | [diff] [blame] | 889 | |
Nicolas Dichtel | ea23192 | 2013-09-02 15:34:58 +0200 | [diff] [blame] | 890 | __skb_tunnel_rx(skb, t->dev, t->net); |
Nicolas Dichtel | f4e0b4c | 2012-11-27 03:07:11 +0000 | [diff] [blame] | 891 | |
| 892 | err = dscp_ecn_decapsulate(t, ipv6h, skb); |
| 893 | if (unlikely(err)) { |
| 894 | if (log_ecn_error) |
| 895 | net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n", |
| 896 | &ipv6h->saddr, |
| 897 | ipv6_get_dsfield(ipv6h)); |
| 898 | if (err > 1) { |
| 899 | ++t->dev->stats.rx_frame_errors; |
| 900 | ++t->dev->stats.rx_errors; |
| 901 | rcu_read_unlock(); |
| 902 | goto discard; |
| 903 | } |
| 904 | } |
| 905 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 906 | tstats = this_cpu_ptr(t->dev->tstats); |
Li RongQing | abb6013 | 2014-01-02 13:20:12 +0800 | [diff] [blame] | 907 | u64_stats_update_begin(&tstats->syncp); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 908 | tstats->rx_packets++; |
| 909 | tstats->rx_bytes += skb->len; |
Li RongQing | abb6013 | 2014-01-02 13:20:12 +0800 | [diff] [blame] | 910 | u64_stats_update_end(&tstats->syncp); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 911 | |
Eric Dumazet | caf586e | 2010-09-30 21:06:55 +0000 | [diff] [blame] | 912 | netif_rx(skb); |
Eric Dumazet | 8990f46 | 2010-09-20 00:12:11 +0000 | [diff] [blame] | 913 | |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 914 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | return 0; |
| 916 | } |
Eric Dumazet | 2922bc8 | 2009-10-23 06:34:34 +0000 | [diff] [blame] | 917 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | return 1; |
Herbert Xu | 50fba2a | 2006-04-04 13:50:45 -0700 | [diff] [blame] | 919 | |
| 920 | discard: |
| 921 | kfree_skb(skb); |
| 922 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 925 | static int ip4ip6_rcv(struct sk_buff *skb) |
| 926 | { |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 927 | return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP, |
| 928 | ip4ip6_dscp_ecn_decapsulate); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 929 | } |
| 930 | |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 931 | static int ip6ip6_rcv(struct sk_buff *skb) |
| 932 | { |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 933 | return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6, |
| 934 | ip6ip6_dscp_ecn_decapsulate); |
Yasuyuki Kozakai | 8359925 | 2006-11-03 09:39:14 +0900 | [diff] [blame] | 935 | } |
| 936 | |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 937 | struct ipv6_tel_txoption { |
| 938 | struct ipv6_txoptions ops; |
| 939 | __u8 dst_opt[8]; |
| 940 | }; |
| 941 | |
| 942 | static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 944 | memset(opt, 0, sizeof(struct ipv6_tel_txoption)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 946 | opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT; |
| 947 | opt->dst_opt[3] = 1; |
| 948 | opt->dst_opt[4] = encap_limit; |
| 949 | opt->dst_opt[5] = IPV6_TLV_PADN; |
| 950 | opt->dst_opt[6] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 952 | opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt; |
| 953 | opt->ops.opt_nflen = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 957 | * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | * @t: the outgoing tunnel device |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 959 | * @hdr: IPv6 header from the incoming packet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | * |
| 961 | * Description: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 962 | * Avoid trivial tunneling loop by checking that tunnel exit-point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | * doesn't match source of incoming packet. |
| 964 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 965 | * Return: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | * 1 if conflict, |
| 967 | * 0 else |
| 968 | **/ |
| 969 | |
Eric Dumazet | 92113bf | 2012-05-18 08:14:11 +0200 | [diff] [blame] | 970 | static inline bool |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 971 | ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | { |
| 973 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); |
| 974 | } |
| 975 | |
Steffen Klassert | d500514 | 2014-11-05 08:02:48 +0100 | [diff] [blame] | 976 | int ip6_tnl_xmit_ctl(struct ip6_tnl *t, |
| 977 | const struct in6_addr *laddr, |
| 978 | const struct in6_addr *raddr) |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 979 | { |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 980 | struct __ip6_tnl_parm *p = &t->parms; |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 981 | int ret = 0; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 982 | struct net *net = t->net; |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 983 | |
Steffen Klassert | d500514 | 2014-11-05 08:02:48 +0100 | [diff] [blame] | 984 | if ((p->flags & IP6_TNL_F_CAP_XMIT) || |
| 985 | ((p->flags & IP6_TNL_F_CAP_PER_PACKET) && |
| 986 | (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_XMIT))) { |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 987 | struct net_device *ldev = NULL; |
| 988 | |
Eric Dumazet | f1a28ea | 2009-11-02 11:21:37 +0100 | [diff] [blame] | 989 | rcu_read_lock(); |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 990 | if (p->link) |
Eric Dumazet | f1a28ea | 2009-11-02 11:21:37 +0100 | [diff] [blame] | 991 | ldev = dev_get_by_index_rcu(net, p->link); |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 992 | |
Steffen Klassert | d500514 | 2014-11-05 08:02:48 +0100 | [diff] [blame] | 993 | if (unlikely(!ipv6_chk_addr(net, laddr, ldev, 0))) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 994 | pr_warn("%s xmit: Local address not yet configured!\n", |
| 995 | p->name); |
Steffen Klassert | d500514 | 2014-11-05 08:02:48 +0100 | [diff] [blame] | 996 | else if (!ipv6_addr_is_multicast(raddr) && |
| 997 | unlikely(ipv6_chk_addr(net, raddr, NULL, 0))) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 998 | pr_warn("%s xmit: Routing loop! Remote address found on this node!\n", |
| 999 | p->name); |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 1000 | else |
| 1001 | ret = 1; |
Eric Dumazet | f1a28ea | 2009-11-02 11:21:37 +0100 | [diff] [blame] | 1002 | rcu_read_unlock(); |
Ville Nuorvala | 09c6bbf | 2006-11-24 17:06:27 -0800 | [diff] [blame] | 1003 | } |
| 1004 | return ret; |
| 1005 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1006 | EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl); |
| 1007 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | /** |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1009 | * ip6_tnl_xmit2 - encapsulate packet and send |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | * @skb: the outgoing socket buffer |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1011 | * @dev: the outgoing tunnel device |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1012 | * @dsfield: dscp code for outer header |
| 1013 | * @fl: flow of tunneled packet |
| 1014 | * @encap_limit: encapsulation limit |
| 1015 | * @pmtu: Path MTU is stored if packet is too big |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | * |
| 1017 | * Description: |
| 1018 | * Build new header and do some sanity checks on the packet before sending |
| 1019 | * it. |
| 1020 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1021 | * Return: |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1022 | * 0 on success |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1023 | * -1 fail |
| 1024 | * %-EMSGSIZE message too big. return mtu in this case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | **/ |
| 1026 | |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1027 | static int ip6_tnl_xmit2(struct sk_buff *skb, |
| 1028 | struct net_device *dev, |
| 1029 | __u8 dsfield, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1030 | struct flowi6 *fl6, |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1031 | int encap_limit, |
| 1032 | __u32 *pmtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1034 | struct ip6_tnl *t = netdev_priv(dev); |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1035 | struct net *net = t->net; |
Pavel Emelyanov | 3dca02a | 2008-05-21 14:17:05 -0700 | [diff] [blame] | 1036 | struct net_device_stats *stats = &t->dev->stats; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1037 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 1038 | struct ipv6_tel_txoption opt; |
Eric Dumazet | d24f22f | 2011-09-20 14:50:00 -0400 | [diff] [blame] | 1039 | struct dst_entry *dst = NULL, *ndst = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | struct net_device *tdev; |
| 1041 | int mtu; |
Chuck Lever | c2636b4 | 2007-10-23 21:07:32 -0700 | [diff] [blame] | 1042 | unsigned int max_headroom = sizeof(struct ipv6hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | u8 proto; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1044 | int err = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
Steffen Klassert | ea3dc96 | 2014-11-05 08:03:50 +0100 | [diff] [blame] | 1046 | /* NBMA tunnel */ |
| 1047 | if (ipv6_addr_any(&t->parms.raddr)) { |
| 1048 | struct in6_addr *addr6; |
| 1049 | struct neighbour *neigh; |
| 1050 | int addr_type; |
| 1051 | |
| 1052 | if (!skb_dst(skb)) |
| 1053 | goto tx_err_link_failure; |
| 1054 | |
| 1055 | neigh = dst_neigh_lookup(skb_dst(skb), |
| 1056 | &ipv6_hdr(skb)->daddr); |
| 1057 | if (!neigh) |
| 1058 | goto tx_err_link_failure; |
| 1059 | |
| 1060 | addr6 = (struct in6_addr *)&neigh->primary_key; |
| 1061 | addr_type = ipv6_addr_type(addr6); |
| 1062 | |
| 1063 | if (addr_type == IPV6_ADDR_ANY) |
| 1064 | addr6 = &ipv6_hdr(skb)->daddr; |
| 1065 | |
| 1066 | memcpy(&fl6->daddr, addr6, sizeof(fl6->daddr)); |
| 1067 | neigh_release(neigh); |
| 1068 | } else if (!fl6->flowi6_mark) |
Martin KaFai Lau | f230d1e | 2015-09-15 14:30:06 -0700 | [diff] [blame] | 1069 | dst = ip6_tnl_dst_get(t); |
Steffen Klassert | d500514 | 2014-11-05 08:02:48 +0100 | [diff] [blame] | 1070 | |
| 1071 | if (!ip6_tnl_xmit_ctl(t, &fl6->saddr, &fl6->daddr)) |
| 1072 | goto tx_err_link_failure; |
| 1073 | |
Eric Dumazet | 89b0212 | 2011-07-28 04:32:25 +0000 | [diff] [blame] | 1074 | if (!dst) { |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 1075 | dst = ip6_route_output(net, NULL, fl6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 1077 | if (dst->error) |
Patrick McHardy | a57ebc9 | 2005-09-08 14:27:47 -0700 | [diff] [blame] | 1078 | goto tx_err_link_failure; |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 1079 | dst = xfrm_lookup(net, dst, flowi6_to_flowi(fl6), NULL, 0); |
| 1080 | if (IS_ERR(dst)) { |
| 1081 | err = PTR_ERR(dst); |
| 1082 | dst = NULL; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 1083 | goto tx_err_link_failure; |
| 1084 | } |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 1085 | ndst = dst; |
Patrick McHardy | a57ebc9 | 2005-09-08 14:27:47 -0700 | [diff] [blame] | 1086 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
| 1088 | tdev = dst->dev; |
| 1089 | |
| 1090 | if (tdev == dev) { |
| 1091 | stats->collisions++; |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 1092 | net_warn_ratelimited("%s: Local routing loop detected!\n", |
| 1093 | t->parms.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | goto tx_err_dst_release; |
| 1095 | } |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1096 | mtu = dst_mtu(dst) - sizeof(*ipv6h); |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 1097 | if (encap_limit >= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | max_headroom += 8; |
| 1099 | mtu -= 8; |
| 1100 | } |
| 1101 | if (mtu < IPV6_MIN_MTU) |
| 1102 | mtu = IPV6_MIN_MTU; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1103 | if (skb_dst(skb)) |
David S. Miller | 6700c27 | 2012-07-17 03:29:28 -0700 | [diff] [blame] | 1104 | skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | if (skb->len > mtu) { |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1106 | *pmtu = mtu; |
| 1107 | err = -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | goto tx_err_dst_release; |
| 1109 | } |
| 1110 | |
Nicolas Dichtel | 963a88b | 2013-09-02 15:34:57 +0200 | [diff] [blame] | 1111 | skb_scrub_packet(skb, !net_eq(t->net, dev_net(dev))); |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | /* |
| 1114 | * Okay, now see if we can stuff it in the buffer as-is. |
| 1115 | */ |
| 1116 | max_headroom += LL_RESERVED_SPACE(tdev); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1117 | |
Patrick McHardy | cfbba49 | 2007-07-09 15:33:40 -0700 | [diff] [blame] | 1118 | if (skb_headroom(skb) < max_headroom || skb_shared(skb) || |
| 1119 | (skb_cloned(skb) && !skb_clone_writable(skb, 0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | struct sk_buff *new_skb; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1121 | |
Ian Morris | e5d08d7 | 2014-11-23 21:28:43 +0000 | [diff] [blame] | 1122 | new_skb = skb_realloc_headroom(skb, max_headroom); |
| 1123 | if (!new_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | goto tx_err_dst_release; |
| 1125 | |
| 1126 | if (skb->sk) |
| 1127 | skb_set_owner_w(new_skb, skb->sk); |
Eric Dumazet | 9ff2644 | 2012-04-19 02:24:17 +0000 | [diff] [blame] | 1128 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | skb = new_skb; |
| 1130 | } |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 1131 | |
| 1132 | if (!fl6->flowi6_mark && ndst) |
| 1133 | ip6_tnl_dst_set(t, ndst); |
| 1134 | skb_dst_set(skb, dst); |
| 1135 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1136 | skb->transport_header = skb->network_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1138 | proto = fl6->flowi6_proto; |
Ville Nuorvala | 6fb32dd | 2006-11-24 17:08:32 -0800 | [diff] [blame] | 1139 | if (encap_limit >= 0) { |
| 1140 | init_tel_txopt(&opt, encap_limit); |
| 1141 | ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL); |
| 1142 | } |
Hannes Frederic Sowa | 3d48305 | 2013-08-18 13:46:52 +0200 | [diff] [blame] | 1143 | |
| 1144 | if (likely(!skb->encapsulation)) { |
| 1145 | skb_reset_inner_headers(skb); |
| 1146 | skb->encapsulation = 1; |
| 1147 | } |
| 1148 | |
Arnaldo Carvalho de Melo | e2d1bca | 2007-04-10 20:46:21 -0700 | [diff] [blame] | 1149 | skb_push(skb, sizeof(struct ipv6hdr)); |
| 1150 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1151 | ipv6h = ipv6_hdr(skb); |
Tom Herbert | cb1ce2e | 2014-07-01 21:33:10 -0700 | [diff] [blame] | 1152 | ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), |
Tom Herbert | 4224090 | 2015-07-31 16:52:12 -0700 | [diff] [blame] | 1153 | ip6_make_flowlabel(net, skb, fl6->flowlabel, true, fl6)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | ipv6h->hop_limit = t->parms.hop_limit; |
| 1155 | ipv6h->nexthdr = proto; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1156 | ipv6h->saddr = fl6->saddr; |
| 1157 | ipv6h->daddr = fl6->daddr; |
David Miller | 79b16aa | 2015-04-05 22:19:09 -0400 | [diff] [blame] | 1158 | ip6tunnel_xmit(NULL, skb, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | return 0; |
| 1160 | tx_err_link_failure: |
| 1161 | stats->tx_carrier_errors++; |
| 1162 | dst_link_failure(skb); |
| 1163 | tx_err_dst_release: |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 1164 | dst_release(dst); |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1165 | return err; |
| 1166 | } |
| 1167 | |
| 1168 | static inline int |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1169 | ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1170 | { |
| 1171 | struct ip6_tnl *t = netdev_priv(dev); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1172 | const struct iphdr *iph = ip_hdr(skb); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1173 | int encap_limit = -1; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1174 | struct flowi6 fl6; |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1175 | __u8 dsfield; |
| 1176 | __u32 mtu; |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 1177 | u8 tproto; |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1178 | int err; |
| 1179 | |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 1180 | tproto = ACCESS_ONCE(t->parms.proto); |
Steffen Klassert | d500514 | 2014-11-05 08:02:48 +0100 | [diff] [blame] | 1181 | if (tproto != IPPROTO_IPIP && tproto != 0) |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1182 | return -1; |
| 1183 | |
| 1184 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1185 | encap_limit = t->parms.encap_limit; |
| 1186 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1187 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1188 | fl6.flowi6_proto = IPPROTO_IPIP; |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1189 | |
| 1190 | dsfield = ipv4_get_dsfield(iph); |
| 1191 | |
Eric Dumazet | d24f22f | 2011-09-20 14:50:00 -0400 | [diff] [blame] | 1192 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1193 | fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT) |
Al Viro | b77f2fa | 2007-07-21 19:09:41 -0700 | [diff] [blame] | 1194 | & IPV6_TCLASS_MASK; |
Eric Dumazet | d24f22f | 2011-09-20 14:50:00 -0400 | [diff] [blame] | 1195 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 1196 | fl6.flowi6_mark = skb->mark; |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1197 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1198 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1199 | if (err != 0) { |
| 1200 | /* XXX: send ICMP error even if DF is not set. */ |
| 1201 | if (err == -EMSGSIZE) |
| 1202 | icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, |
| 1203 | htonl(mtu)); |
| 1204 | return -1; |
| 1205 | } |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static inline int |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1211 | ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1212 | { |
| 1213 | struct ip6_tnl *t = netdev_priv(dev); |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1214 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1215 | int encap_limit = -1; |
| 1216 | __u16 offset; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1217 | struct flowi6 fl6; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1218 | __u8 dsfield; |
| 1219 | __u32 mtu; |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 1220 | u8 tproto; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1221 | int err; |
| 1222 | |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 1223 | tproto = ACCESS_ONCE(t->parms.proto); |
| 1224 | if ((tproto != IPPROTO_IPV6 && tproto != 0) || |
Steffen Klassert | d500514 | 2014-11-05 08:02:48 +0100 | [diff] [blame] | 1225 | ip6_tnl_addr_conflict(t, ipv6h)) |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1226 | return -1; |
| 1227 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1228 | offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb)); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1229 | if (offset > 0) { |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1230 | struct ipv6_tlv_tnl_enc_lim *tel; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1231 | tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset]; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1232 | if (tel->encap_limit == 0) { |
| 1233 | icmpv6_send(skb, ICMPV6_PARAMPROB, |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 1234 | ICMPV6_HDR_FIELD, offset + 2); |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1235 | return -1; |
| 1236 | } |
| 1237 | encap_limit = tel->encap_limit - 1; |
| 1238 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
| 1239 | encap_limit = t->parms.encap_limit; |
| 1240 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1241 | memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6)); |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1242 | fl6.flowi6_proto = IPPROTO_IPV6; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1243 | |
| 1244 | dsfield = ipv6_get_dsfield(ipv6h); |
Eric Dumazet | d24f22f | 2011-09-20 14:50:00 -0400 | [diff] [blame] | 1245 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS) |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1246 | fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK); |
Eric Dumazet | d24f22f | 2011-09-20 14:50:00 -0400 | [diff] [blame] | 1247 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL) |
Florent Fourcot | 3308de2 | 2013-12-08 15:47:00 +0100 | [diff] [blame] | 1248 | fl6.flowlabel |= ip6_flowlabel(ipv6h); |
Eric Dumazet | d24f22f | 2011-09-20 14:50:00 -0400 | [diff] [blame] | 1249 | if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK) |
| 1250 | fl6.flowi6_mark = skb->mark; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1251 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1252 | err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu); |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1253 | if (err != 0) { |
| 1254 | if (err == -EMSGSIZE) |
Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 1255 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1256 | return -1; |
| 1257 | } |
| 1258 | |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
Stephen Hemminger | 6fef4c0 | 2009-08-31 19:50:41 +0000 | [diff] [blame] | 1262 | static netdev_tx_t |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1263 | ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1264 | { |
| 1265 | struct ip6_tnl *t = netdev_priv(dev); |
Pavel Emelyanov | 3dca02a | 2008-05-21 14:17:05 -0700 | [diff] [blame] | 1266 | struct net_device_stats *stats = &t->dev->stats; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1267 | int ret; |
| 1268 | |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1269 | switch (skb->protocol) { |
Arnaldo Carvalho de Melo | 6067804 | 2008-09-20 22:20:49 -0700 | [diff] [blame] | 1270 | case htons(ETH_P_IP): |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1271 | ret = ip4ip6_tnl_xmit(skb, dev); |
| 1272 | break; |
Arnaldo Carvalho de Melo | 6067804 | 2008-09-20 22:20:49 -0700 | [diff] [blame] | 1273 | case htons(ETH_P_IPV6): |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1274 | ret = ip6ip6_tnl_xmit(skb, dev); |
| 1275 | break; |
| 1276 | default: |
| 1277 | goto tx_err; |
| 1278 | } |
| 1279 | |
| 1280 | if (ret < 0) |
| 1281 | goto tx_err; |
| 1282 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 1283 | return NETDEV_TX_OK; |
Yasuyuki Kozakai | 61ec2ae | 2006-11-05 22:56:45 +0900 | [diff] [blame] | 1284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | tx_err: |
| 1286 | stats->tx_errors++; |
| 1287 | stats->tx_dropped++; |
| 1288 | kfree_skb(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 1289 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1292 | static void ip6_tnl_link_config(struct ip6_tnl *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | { |
| 1294 | struct net_device *dev = t->dev; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1295 | struct __ip6_tnl_parm *p = &t->parms; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1296 | struct flowi6 *fl6 = &t->fl.u.ip6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | |
Jiri Pirko | 3a6d54c | 2009-05-11 23:37:15 +0000 | [diff] [blame] | 1298 | memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); |
| 1299 | memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
| 1301 | /* Set up flowi template */ |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1302 | fl6->saddr = p->laddr; |
| 1303 | fl6->daddr = p->raddr; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1304 | fl6->flowi6_oif = p->link; |
| 1305 | fl6->flowlabel = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
| 1307 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1308 | fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1310 | fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
Ville Nuorvala | d0087b2 | 2012-06-28 18:15:52 +0000 | [diff] [blame] | 1312 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET); |
| 1313 | p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
| 1315 | if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) |
| 1316 | dev->flags |= IFF_POINTOPOINT; |
| 1317 | else |
| 1318 | dev->flags &= ~IFF_POINTOPOINT; |
| 1319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | if (p->flags & IP6_TNL_F_CAP_XMIT) { |
Ville Nuorvala | 305d4b3 | 2006-11-24 17:06:53 -0800 | [diff] [blame] | 1321 | int strict = (ipv6_addr_type(&p->raddr) & |
| 1322 | (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); |
| 1323 | |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1324 | struct rt6_info *rt = rt6_lookup(t->net, |
Pavel Emelyanov | 2f7f54b | 2008-04-16 01:23:44 -0700 | [diff] [blame] | 1325 | &p->raddr, &p->laddr, |
Ville Nuorvala | 305d4b3 | 2006-11-24 17:06:53 -0800 | [diff] [blame] | 1326 | p->link, strict); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1328 | if (!rt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | return; |
| 1330 | |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 1331 | if (rt->dst.dev) { |
| 1332 | dev->hard_header_len = rt->dst.dev->hard_header_len + |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1333 | sizeof(struct ipv6hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1335 | dev->mtu = rt->dst.dev->mtu - sizeof(struct ipv6hdr); |
Anders Franzen | 381601e | 2010-11-24 05:47:18 +0000 | [diff] [blame] | 1336 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1337 | dev->mtu -= 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | |
| 1339 | if (dev->mtu < IPV6_MIN_MTU) |
| 1340 | dev->mtu = IPV6_MIN_MTU; |
| 1341 | } |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 1342 | ip6_rt_put(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1347 | * ip6_tnl_change - update the tunnel parameters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | * @t: tunnel to be changed |
| 1349 | * @p: tunnel configuration parameters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | * |
| 1351 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1352 | * ip6_tnl_change() updates the tunnel parameters |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | **/ |
| 1354 | |
| 1355 | static int |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1356 | ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | { |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1358 | t->parms.laddr = p->laddr; |
| 1359 | t->parms.raddr = p->raddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | t->parms.flags = p->flags; |
| 1361 | t->parms.hop_limit = p->hop_limit; |
| 1362 | t->parms.encap_limit = p->encap_limit; |
| 1363 | t->parms.flowinfo = p->flowinfo; |
Gabor Fekete | 8181b8c | 2005-06-08 14:54:38 -0700 | [diff] [blame] | 1364 | t->parms.link = p->link; |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 1365 | t->parms.proto = p->proto; |
Hugo Santos | 0c08889 | 2006-02-24 13:16:25 -0800 | [diff] [blame] | 1366 | ip6_tnl_dst_reset(t); |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1367 | ip6_tnl_link_config(t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | return 0; |
| 1369 | } |
| 1370 | |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1371 | static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) |
| 1372 | { |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1373 | struct net *net = t->net; |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1374 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1375 | int err; |
| 1376 | |
| 1377 | ip6_tnl_unlink(ip6n, t); |
| 1378 | synchronize_net(); |
| 1379 | err = ip6_tnl_change(t, p); |
| 1380 | ip6_tnl_link(ip6n, t); |
| 1381 | netdev_state_change(t->dev); |
| 1382 | return err; |
| 1383 | } |
| 1384 | |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 1385 | static int ip6_tnl0_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p) |
| 1386 | { |
| 1387 | /* for default tnl0 device allow to change only the proto */ |
| 1388 | t->parms.proto = p->proto; |
| 1389 | netdev_state_change(t->dev); |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1393 | static void |
| 1394 | ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u) |
| 1395 | { |
| 1396 | p->laddr = u->laddr; |
| 1397 | p->raddr = u->raddr; |
| 1398 | p->flags = u->flags; |
| 1399 | p->hop_limit = u->hop_limit; |
| 1400 | p->encap_limit = u->encap_limit; |
| 1401 | p->flowinfo = u->flowinfo; |
| 1402 | p->link = u->link; |
| 1403 | p->proto = u->proto; |
| 1404 | memcpy(p->name, u->name, sizeof(u->name)); |
| 1405 | } |
| 1406 | |
| 1407 | static void |
| 1408 | ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p) |
| 1409 | { |
| 1410 | u->laddr = p->laddr; |
| 1411 | u->raddr = p->raddr; |
| 1412 | u->flags = p->flags; |
| 1413 | u->hop_limit = p->hop_limit; |
| 1414 | u->encap_limit = p->encap_limit; |
| 1415 | u->flowinfo = p->flowinfo; |
| 1416 | u->link = p->link; |
| 1417 | u->proto = p->proto; |
| 1418 | memcpy(u->name, p->name, sizeof(u->name)); |
| 1419 | } |
| 1420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1422 | * ip6_tnl_ioctl - configure ipv6 tunnels from userspace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | * @dev: virtual device associated with tunnel |
| 1424 | * @ifr: parameters passed from userspace |
| 1425 | * @cmd: command to be performed |
| 1426 | * |
| 1427 | * Description: |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1428 | * ip6_tnl_ioctl() is used for managing IPv6 tunnels |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1429 | * from userspace. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | * |
| 1431 | * The possible commands are the following: |
| 1432 | * %SIOCGETTUNNEL: get tunnel parameters for device |
| 1433 | * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters |
| 1434 | * %SIOCCHGTUNNEL: change tunnel parameters to those given |
| 1435 | * %SIOCDELTUNNEL: delete tunnel |
| 1436 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1437 | * The fallback device "ip6tnl0", created during module |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | * initialization, can be used for creating other tunnel devices. |
| 1439 | * |
| 1440 | * Return: |
| 1441 | * 0 on success, |
| 1442 | * %-EFAULT if unable to copy data to or from userspace, |
| 1443 | * %-EPERM if current process hasn't %CAP_NET_ADMIN set |
| 1444 | * %-EINVAL if passed tunnel parameters are invalid, |
| 1445 | * %-EEXIST if changing a tunnel's parameters would cause a conflict |
| 1446 | * %-ENODEV if attempting to change or delete a nonexisting device |
| 1447 | **/ |
| 1448 | |
| 1449 | static int |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1450 | ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | { |
| 1452 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | struct ip6_tnl_parm p; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1454 | struct __ip6_tnl_parm p1; |
Nicolas Dichtel | 74462f0 | 2014-04-16 11:19:34 +0200 | [diff] [blame] | 1455 | struct ip6_tnl *t = netdev_priv(dev); |
| 1456 | struct net *net = t->net; |
Pavel Emelyanov | 2dd02c8 | 2008-04-16 01:22:23 -0700 | [diff] [blame] | 1457 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
| 1459 | switch (cmd) { |
| 1460 | case SIOCGETTUNNEL: |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 1461 | if (dev == ip6n->fb_tnl_dev) { |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1462 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | err = -EFAULT; |
| 1464 | break; |
| 1465 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1466 | ip6_tnl_parm_from_user(&p1, &p); |
| 1467 | t = ip6_tnl_locate(net, &p1, 0); |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 1468 | if (IS_ERR(t)) |
Nicolas Dichtel | 74462f0 | 2014-04-16 11:19:34 +0200 | [diff] [blame] | 1469 | t = netdev_priv(dev); |
Dan Carpenter | 5ef5d6c | 2012-08-16 03:14:04 +0000 | [diff] [blame] | 1470 | } else { |
| 1471 | memset(&p, 0, sizeof(p)); |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1472 | } |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1473 | ip6_tnl_parm_to_user(&p, &t->parms); |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1474 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | err = -EFAULT; |
| 1476 | } |
| 1477 | break; |
| 1478 | case SIOCADDTUNNEL: |
| 1479 | case SIOCCHGTUNNEL: |
| 1480 | err = -EPERM; |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 1481 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1483 | err = -EFAULT; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1484 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1486 | err = -EINVAL; |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 1487 | if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP && |
| 1488 | p.proto != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | break; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1490 | ip6_tnl_parm_from_user(&p1, &p); |
| 1491 | t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL); |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 1492 | if (cmd == SIOCCHGTUNNEL) { |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 1493 | if (!IS_ERR(t)) { |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1494 | if (t->dev != dev) { |
| 1495 | err = -EEXIST; |
| 1496 | break; |
| 1497 | } |
| 1498 | } else |
| 1499 | t = netdev_priv(dev); |
Alexey Andriyanov | acf722f | 2014-10-29 10:54:52 +0300 | [diff] [blame] | 1500 | if (dev == ip6n->fb_tnl_dev) |
| 1501 | err = ip6_tnl0_update(t, &p1); |
| 1502 | else |
| 1503 | err = ip6_tnl_update(t, &p1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | } |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 1505 | if (!IS_ERR(t)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | err = 0; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1507 | ip6_tnl_parm_to_user(&p, &t->parms); |
| 1508 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1509 | err = -EFAULT; |
| 1510 | |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 1511 | } else { |
| 1512 | err = PTR_ERR(t); |
| 1513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | break; |
| 1515 | case SIOCDELTUNNEL: |
| 1516 | err = -EPERM; |
Eric W. Biederman | af31f41 | 2012-11-16 03:03:06 +0000 | [diff] [blame] | 1517 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | break; |
| 1519 | |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 1520 | if (dev == ip6n->fb_tnl_dev) { |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1521 | err = -EFAULT; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1522 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1524 | err = -ENOENT; |
xeb@mail.ru | c12b395 | 2012-08-10 00:51:50 +0000 | [diff] [blame] | 1525 | ip6_tnl_parm_from_user(&p1, &p); |
| 1526 | t = ip6_tnl_locate(net, &p1, 0); |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 1527 | if (IS_ERR(t)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1529 | err = -EPERM; |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 1530 | if (t->dev == ip6n->fb_tnl_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | break; |
Ville Nuorvala | 567131a | 2006-11-24 17:05:41 -0800 | [diff] [blame] | 1532 | dev = t->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } |
Stephen Hemminger | 22f8cde | 2007-02-07 00:09:58 -0800 | [diff] [blame] | 1534 | err = 0; |
| 1535 | unregister_netdevice(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | break; |
| 1537 | default: |
| 1538 | err = -EINVAL; |
| 1539 | } |
| 1540 | return err; |
| 1541 | } |
| 1542 | |
| 1543 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1544 | * ip6_tnl_change_mtu - change mtu manually for tunnel device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | * @dev: virtual device associated with tunnel |
| 1546 | * @new_mtu: the new mtu |
| 1547 | * |
| 1548 | * Return: |
| 1549 | * 0 on success, |
| 1550 | * %-EINVAL if mtu too small |
| 1551 | **/ |
| 1552 | |
| 1553 | static int |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1554 | ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | { |
Oussama Ghorbel | 582442d | 2013-10-03 14:49:26 +0100 | [diff] [blame] | 1556 | struct ip6_tnl *tnl = netdev_priv(dev); |
| 1557 | |
| 1558 | if (tnl->parms.proto == IPPROTO_IPIP) { |
| 1559 | if (new_mtu < 68) |
| 1560 | return -EINVAL; |
| 1561 | } else { |
| 1562 | if (new_mtu < IPV6_MIN_MTU) |
| 1563 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } |
Oussama Ghorbel | 582442d | 2013-10-03 14:49:26 +0100 | [diff] [blame] | 1565 | if (new_mtu > 0xFFF8 - dev->hard_header_len) |
| 1566 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | dev->mtu = new_mtu; |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
Nicolas Dichtel | ecf2c06 | 2015-04-02 17:07:01 +0200 | [diff] [blame] | 1571 | int ip6_tnl_get_iflink(const struct net_device *dev) |
| 1572 | { |
| 1573 | struct ip6_tnl *t = netdev_priv(dev); |
| 1574 | |
| 1575 | return t->parms.link; |
| 1576 | } |
| 1577 | EXPORT_SYMBOL(ip6_tnl_get_iflink); |
Stephen Hemminger | 1326c3d | 2008-11-20 20:33:56 -0800 | [diff] [blame] | 1578 | |
| 1579 | static const struct net_device_ops ip6_tnl_netdev_ops = { |
Steffen Klassert | 6c6151d | 2014-11-03 09:19:27 +0100 | [diff] [blame] | 1580 | .ndo_init = ip6_tnl_dev_init, |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1581 | .ndo_uninit = ip6_tnl_dev_uninit, |
Stephen Hemminger | 1326c3d | 2008-11-20 20:33:56 -0800 | [diff] [blame] | 1582 | .ndo_start_xmit = ip6_tnl_xmit, |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1583 | .ndo_do_ioctl = ip6_tnl_ioctl, |
Stephen Hemminger | 1326c3d | 2008-11-20 20:33:56 -0800 | [diff] [blame] | 1584 | .ndo_change_mtu = ip6_tnl_change_mtu, |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1585 | .ndo_get_stats = ip6_get_stats, |
Nicolas Dichtel | ecf2c06 | 2015-04-02 17:07:01 +0200 | [diff] [blame] | 1586 | .ndo_get_iflink = ip6_tnl_get_iflink, |
Stephen Hemminger | 1326c3d | 2008-11-20 20:33:56 -0800 | [diff] [blame] | 1587 | }; |
| 1588 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1591 | * ip6_tnl_dev_setup - setup virtual tunnel device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | * @dev: virtual device associated with tunnel |
| 1593 | * |
| 1594 | * Description: |
| 1595 | * Initialize function pointers and device parameters |
| 1596 | **/ |
| 1597 | |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1598 | static void ip6_tnl_dev_setup(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | { |
Anders Franzen | 381601e | 2010-11-24 05:47:18 +0000 | [diff] [blame] | 1600 | struct ip6_tnl *t; |
| 1601 | |
Stephen Hemminger | 1326c3d | 2008-11-20 20:33:56 -0800 | [diff] [blame] | 1602 | dev->netdev_ops = &ip6_tnl_netdev_ops; |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1603 | dev->destructor = ip6_dev_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
| 1605 | dev->type = ARPHRD_TUNNEL6; |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1606 | dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr); |
| 1607 | dev->mtu = ETH_DATA_LEN - sizeof(struct ipv6hdr); |
Anders Franzen | 381601e | 2010-11-24 05:47:18 +0000 | [diff] [blame] | 1608 | t = netdev_priv(dev); |
| 1609 | if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) |
Ian Morris | 67ba415 | 2014-08-24 21:53:10 +0100 | [diff] [blame] | 1610 | dev->mtu -= 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | dev->flags |= IFF_NOARP; |
| 1612 | dev->addr_len = sizeof(struct in6_addr); |
Eric Dumazet | 0287587 | 2014-10-05 18:38:35 -0700 | [diff] [blame] | 1613 | netif_keep_dst(dev); |
Nicolas Dichtel | e837735 | 2013-08-20 12:16:06 +0200 | [diff] [blame] | 1614 | /* This perm addr will be used as interface identifier by IPv6 */ |
| 1615 | dev->addr_assign_type = NET_ADDR_RANDOM; |
| 1616 | eth_random_addr(dev->perm_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1621 | * ip6_tnl_dev_init_gen - general initializer for all tunnel devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | * @dev: virtual device associated with tunnel |
| 1623 | **/ |
| 1624 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1625 | static inline int |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1626 | ip6_tnl_dev_init_gen(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1628 | struct ip6_tnl *t = netdev_priv(dev); |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 1629 | int ret; |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | t->dev = dev; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1632 | t->net = dev_net(dev); |
WANG Cong | 1c213bd | 2014-02-13 11:46:28 -0800 | [diff] [blame] | 1633 | dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1634 | if (!dev->tstats) |
| 1635 | return -ENOMEM; |
Martin KaFai Lau | cdf3464 | 2015-09-15 14:30:07 -0700 | [diff] [blame^] | 1636 | |
| 1637 | ret = ip6_tnl_dst_init(t); |
| 1638 | if (ret) { |
| 1639 | free_percpu(dev->tstats); |
| 1640 | dev->tstats = NULL; |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1644 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1648 | * ip6_tnl_dev_init - initializer for all non fallback tunnel devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | * @dev: virtual device associated with tunnel |
| 1650 | **/ |
| 1651 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1652 | static int ip6_tnl_dev_init(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1654 | struct ip6_tnl *t = netdev_priv(dev); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1655 | int err = ip6_tnl_dev_init_gen(dev); |
| 1656 | |
| 1657 | if (err) |
| 1658 | return err; |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1659 | ip6_tnl_link_config(t); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1660 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /** |
Yasuyuki Kozakai | 3144581 | 2007-02-10 00:30:33 +0900 | [diff] [blame] | 1664 | * ip6_fb_tnl_dev_init - initializer for fallback tunnel device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | * @dev: fallback device |
| 1666 | * |
| 1667 | * Return: 0 |
| 1668 | **/ |
| 1669 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1670 | static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | { |
Patrick McHardy | 2941a48 | 2006-01-08 22:05:26 -0800 | [diff] [blame] | 1672 | struct ip6_tnl *t = netdev_priv(dev); |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1673 | struct net *net = dev_net(dev); |
| 1674 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1675 | |
Yasuyuki Kozakai | 502b093 | 2006-11-30 14:43:28 +0900 | [diff] [blame] | 1676 | t->parms.proto = IPPROTO_IPV6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | dev_hold(dev); |
Ville Nuorvala | d0087b2 | 2012-06-28 18:15:52 +0000 | [diff] [blame] | 1678 | |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 1679 | rcu_assign_pointer(ip6n->tnls_wc[0], t); |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1680 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1683 | static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1684 | { |
| 1685 | u8 proto; |
| 1686 | |
Susant Sahani | c896593 | 2014-05-10 00:11:32 +0530 | [diff] [blame] | 1687 | if (!data || !data[IFLA_IPTUN_PROTO]) |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1688 | return 0; |
| 1689 | |
| 1690 | proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); |
| 1691 | if (proto != IPPROTO_IPV6 && |
| 1692 | proto != IPPROTO_IPIP && |
| 1693 | proto != 0) |
| 1694 | return -EINVAL; |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | static void ip6_tnl_netlink_parms(struct nlattr *data[], |
| 1700 | struct __ip6_tnl_parm *parms) |
| 1701 | { |
| 1702 | memset(parms, 0, sizeof(*parms)); |
| 1703 | |
| 1704 | if (!data) |
| 1705 | return; |
| 1706 | |
| 1707 | if (data[IFLA_IPTUN_LINK]) |
| 1708 | parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); |
| 1709 | |
| 1710 | if (data[IFLA_IPTUN_LOCAL]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 1711 | parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]); |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1712 | |
| 1713 | if (data[IFLA_IPTUN_REMOTE]) |
Jiri Benc | 67b61f6 | 2015-03-29 16:59:26 +0200 | [diff] [blame] | 1714 | parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]); |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1715 | |
| 1716 | if (data[IFLA_IPTUN_TTL]) |
| 1717 | parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]); |
| 1718 | |
| 1719 | if (data[IFLA_IPTUN_ENCAP_LIMIT]) |
| 1720 | parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]); |
| 1721 | |
| 1722 | if (data[IFLA_IPTUN_FLOWINFO]) |
Nicolas Dichtel | 1ff05fb | 2012-11-15 04:06:42 +0000 | [diff] [blame] | 1723 | parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]); |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1724 | |
| 1725 | if (data[IFLA_IPTUN_FLAGS]) |
| 1726 | parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]); |
| 1727 | |
| 1728 | if (data[IFLA_IPTUN_PROTO]) |
| 1729 | parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]); |
| 1730 | } |
| 1731 | |
| 1732 | static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev, |
| 1733 | struct nlattr *tb[], struct nlattr *data[]) |
| 1734 | { |
| 1735 | struct net *net = dev_net(dev); |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 1736 | struct ip6_tnl *nt, *t; |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1737 | |
| 1738 | nt = netdev_priv(dev); |
| 1739 | ip6_tnl_netlink_parms(data, &nt->parms); |
| 1740 | |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 1741 | t = ip6_tnl_locate(net, &nt->parms, 0); |
| 1742 | if (!IS_ERR(t)) |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1743 | return -EEXIST; |
| 1744 | |
| 1745 | return ip6_tnl_create2(dev); |
| 1746 | } |
| 1747 | |
| 1748 | static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[], |
| 1749 | struct nlattr *data[]) |
| 1750 | { |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1751 | struct ip6_tnl *t = netdev_priv(dev); |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1752 | struct __ip6_tnl_parm p; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1753 | struct net *net = t->net; |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1754 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1755 | |
| 1756 | if (dev == ip6n->fb_tnl_dev) |
| 1757 | return -EINVAL; |
| 1758 | |
| 1759 | ip6_tnl_netlink_parms(data, &p); |
| 1760 | |
| 1761 | t = ip6_tnl_locate(net, &p, 0); |
Nicolas Dichtel | 3735556 | 2015-03-16 15:56:05 +0100 | [diff] [blame] | 1762 | if (!IS_ERR(t)) { |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1763 | if (t->dev != dev) |
| 1764 | return -EEXIST; |
| 1765 | } else |
| 1766 | t = netdev_priv(dev); |
| 1767 | |
| 1768 | return ip6_tnl_update(t, &p); |
| 1769 | } |
| 1770 | |
Nicolas Dichtel | 1e9f3d6 | 2013-11-14 15:47:03 +0100 | [diff] [blame] | 1771 | static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head) |
| 1772 | { |
| 1773 | struct net *net = dev_net(dev); |
| 1774 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
| 1775 | |
| 1776 | if (dev != ip6n->fb_tnl_dev) |
| 1777 | unregister_netdevice_queue(dev, head); |
| 1778 | } |
| 1779 | |
Nicolas Dichtel | b58d731 | 2012-11-14 05:13:59 +0000 | [diff] [blame] | 1780 | static size_t ip6_tnl_get_size(const struct net_device *dev) |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1781 | { |
| 1782 | return |
| 1783 | /* IFLA_IPTUN_LINK */ |
| 1784 | nla_total_size(4) + |
| 1785 | /* IFLA_IPTUN_LOCAL */ |
| 1786 | nla_total_size(sizeof(struct in6_addr)) + |
| 1787 | /* IFLA_IPTUN_REMOTE */ |
| 1788 | nla_total_size(sizeof(struct in6_addr)) + |
| 1789 | /* IFLA_IPTUN_TTL */ |
| 1790 | nla_total_size(1) + |
| 1791 | /* IFLA_IPTUN_ENCAP_LIMIT */ |
| 1792 | nla_total_size(1) + |
| 1793 | /* IFLA_IPTUN_FLOWINFO */ |
| 1794 | nla_total_size(4) + |
| 1795 | /* IFLA_IPTUN_FLAGS */ |
| 1796 | nla_total_size(4) + |
Nicolas Dichtel | cfa323b | 2012-11-14 05:13:58 +0000 | [diff] [blame] | 1797 | /* IFLA_IPTUN_PROTO */ |
| 1798 | nla_total_size(1) + |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1799 | 0; |
| 1800 | } |
| 1801 | |
Nicolas Dichtel | b58d731 | 2012-11-14 05:13:59 +0000 | [diff] [blame] | 1802 | static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev) |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1803 | { |
| 1804 | struct ip6_tnl *tunnel = netdev_priv(dev); |
| 1805 | struct __ip6_tnl_parm *parm = &tunnel->parms; |
| 1806 | |
| 1807 | if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 1808 | nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) || |
| 1809 | nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) || |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1810 | nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) || |
| 1811 | nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) || |
| 1812 | nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) || |
Nicolas Dichtel | cfa323b | 2012-11-14 05:13:58 +0000 | [diff] [blame] | 1813 | nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) || |
| 1814 | nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto)) |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1815 | goto nla_put_failure; |
| 1816 | return 0; |
| 1817 | |
| 1818 | nla_put_failure: |
| 1819 | return -EMSGSIZE; |
| 1820 | } |
| 1821 | |
Nicolas Dichtel | 1728d4f | 2015-01-15 15:11:17 +0100 | [diff] [blame] | 1822 | struct net *ip6_tnl_get_link_net(const struct net_device *dev) |
| 1823 | { |
| 1824 | struct ip6_tnl *tunnel = netdev_priv(dev); |
| 1825 | |
| 1826 | return tunnel->net; |
| 1827 | } |
| 1828 | EXPORT_SYMBOL(ip6_tnl_get_link_net); |
| 1829 | |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1830 | static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = { |
| 1831 | [IFLA_IPTUN_LINK] = { .type = NLA_U32 }, |
| 1832 | [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) }, |
| 1833 | [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) }, |
| 1834 | [IFLA_IPTUN_TTL] = { .type = NLA_U8 }, |
| 1835 | [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 }, |
| 1836 | [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 }, |
| 1837 | [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 }, |
| 1838 | [IFLA_IPTUN_PROTO] = { .type = NLA_U8 }, |
| 1839 | }; |
| 1840 | |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1841 | static struct rtnl_link_ops ip6_link_ops __read_mostly = { |
| 1842 | .kind = "ip6tnl", |
| 1843 | .maxtype = IFLA_IPTUN_MAX, |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1844 | .policy = ip6_tnl_policy, |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1845 | .priv_size = sizeof(struct ip6_tnl), |
Nicolas Dichtel | 0b11245 | 2012-11-14 05:14:00 +0000 | [diff] [blame] | 1846 | .setup = ip6_tnl_dev_setup, |
| 1847 | .validate = ip6_tnl_validate, |
| 1848 | .newlink = ip6_tnl_newlink, |
| 1849 | .changelink = ip6_tnl_changelink, |
Nicolas Dichtel | 1e9f3d6 | 2013-11-14 15:47:03 +0100 | [diff] [blame] | 1850 | .dellink = ip6_tnl_dellink, |
Nicolas Dichtel | b58d731 | 2012-11-14 05:13:59 +0000 | [diff] [blame] | 1851 | .get_size = ip6_tnl_get_size, |
| 1852 | .fill_info = ip6_tnl_fill_info, |
Nicolas Dichtel | 1728d4f | 2015-01-15 15:11:17 +0100 | [diff] [blame] | 1853 | .get_link_net = ip6_tnl_get_link_net, |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1854 | }; |
| 1855 | |
Eric Dumazet | 3ff2cfa | 2010-08-30 10:27:10 +0000 | [diff] [blame] | 1856 | static struct xfrm6_tunnel ip4ip6_handler __read_mostly = { |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1857 | .handler = ip4ip6_rcv, |
| 1858 | .err_handler = ip4ip6_err, |
| 1859 | .priority = 1, |
| 1860 | }; |
| 1861 | |
Eric Dumazet | 3ff2cfa | 2010-08-30 10:27:10 +0000 | [diff] [blame] | 1862 | static struct xfrm6_tunnel ip6ip6_handler __read_mostly = { |
Patrick McHardy | 0303770 | 2005-07-19 14:03:34 -0700 | [diff] [blame] | 1863 | .handler = ip6ip6_rcv, |
| 1864 | .err_handler = ip6ip6_err, |
Herbert Xu | d2acc34 | 2006-03-28 01:12:13 -0800 | [diff] [blame] | 1865 | .priority = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | }; |
| 1867 | |
Nicolas Dichtel | 1e9f3d6 | 2013-11-14 15:47:03 +0100 | [diff] [blame] | 1868 | static void __net_exit ip6_tnl_destroy_tunnels(struct net *net) |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1869 | { |
Nicolas Dichtel | 1e9f3d6 | 2013-11-14 15:47:03 +0100 | [diff] [blame] | 1870 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1871 | struct net_device *dev, *aux; |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1872 | int h; |
| 1873 | struct ip6_tnl *t; |
Eric Dumazet | cf4432f | 2009-10-28 05:16:51 +0000 | [diff] [blame] | 1874 | LIST_HEAD(list); |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1875 | |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1876 | for_each_netdev_safe(net, dev, aux) |
| 1877 | if (dev->rtnl_link_ops == &ip6_link_ops) |
| 1878 | unregister_netdevice_queue(dev, &list); |
| 1879 | |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1880 | for (h = 0; h < HASH_SIZE; h++) { |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 1881 | t = rtnl_dereference(ip6n->tnls_r_l[h]); |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 1882 | while (t) { |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1883 | /* If dev is in the same netns, it has already |
| 1884 | * been added to the list by the previous loop. |
| 1885 | */ |
| 1886 | if (!net_eq(dev_net(t->dev), net)) |
| 1887 | unregister_netdevice_queue(t->dev, &list); |
Eric Dumazet | 9476763 | 2010-09-15 20:25:34 +0000 | [diff] [blame] | 1888 | t = rtnl_dereference(t->next); |
Eric Dumazet | cf4432f | 2009-10-28 05:16:51 +0000 | [diff] [blame] | 1889 | } |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
Eric Dumazet | cf4432f | 2009-10-28 05:16:51 +0000 | [diff] [blame] | 1892 | unregister_netdevice_many(&list); |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1895 | static int __net_init ip6_tnl_init_net(struct net *net) |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 1896 | { |
Eric W. Biederman | ac31cd3 | 2009-11-29 15:46:15 +0000 | [diff] [blame] | 1897 | struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id); |
Josh Boyer | 731abb9 | 2011-11-10 15:10:23 +0000 | [diff] [blame] | 1898 | struct ip6_tnl *t = NULL; |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 1899 | int err; |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 1900 | |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1901 | ip6n->tnls[0] = ip6n->tnls_wc; |
| 1902 | ip6n->tnls[1] = ip6n->tnls_r_l; |
| 1903 | |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 1904 | err = -ENOMEM; |
| 1905 | ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 1906 | NET_NAME_UNKNOWN, ip6_tnl_dev_setup); |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 1907 | |
| 1908 | if (!ip6n->fb_tnl_dev) |
| 1909 | goto err_alloc_dev; |
Alexey Dobriyan | be77e59 | 2008-11-23 17:26:26 -0800 | [diff] [blame] | 1910 | dev_net_set(ip6n->fb_tnl_dev, net); |
Nicolas Dichtel | bb81409 | 2013-10-01 18:05:00 +0200 | [diff] [blame] | 1911 | ip6n->fb_tnl_dev->rtnl_link_ops = &ip6_link_ops; |
Nicolas Dichtel | 0bd87628 | 2013-08-13 17:51:12 +0200 | [diff] [blame] | 1912 | /* FB netdevice is special: we have one, and only one per netns. |
| 1913 | * Allowing to move it to another netns is clearly unsafe. |
| 1914 | */ |
| 1915 | ip6n->fb_tnl_dev->features |= NETIF_F_NETNS_LOCAL; |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 1916 | |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1917 | err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev); |
| 1918 | if (err < 0) |
| 1919 | goto err_register; |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 1920 | |
| 1921 | err = register_netdev(ip6n->fb_tnl_dev); |
| 1922 | if (err < 0) |
| 1923 | goto err_register; |
Josh Boyer | 731abb9 | 2011-11-10 15:10:23 +0000 | [diff] [blame] | 1924 | |
| 1925 | t = netdev_priv(ip6n->fb_tnl_dev); |
| 1926 | |
| 1927 | strcpy(t->parms.name, ip6n->fb_tnl_dev->name); |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 1928 | return 0; |
| 1929 | |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 1930 | err_register: |
Eric Dumazet | 8560f22 | 2010-09-28 03:23:34 +0000 | [diff] [blame] | 1931 | ip6_dev_free(ip6n->fb_tnl_dev); |
Pavel Emelyanov | 15820e129 | 2008-04-16 01:23:02 -0700 | [diff] [blame] | 1932 | err_alloc_dev: |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 1933 | return err; |
| 1934 | } |
| 1935 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 1936 | static void __net_exit ip6_tnl_exit_net(struct net *net) |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 1937 | { |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1938 | rtnl_lock(); |
Nicolas Dichtel | 1e9f3d6 | 2013-11-14 15:47:03 +0100 | [diff] [blame] | 1939 | ip6_tnl_destroy_tunnels(net); |
Pavel Emelyanov | 3e6c9fb | 2008-04-16 01:23:22 -0700 | [diff] [blame] | 1940 | rtnl_unlock(); |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | static struct pernet_operations ip6_tnl_net_ops = { |
| 1944 | .init = ip6_tnl_init_net, |
| 1945 | .exit = ip6_tnl_exit_net, |
Eric W. Biederman | ac31cd3 | 2009-11-29 15:46:15 +0000 | [diff] [blame] | 1946 | .id = &ip6_tnl_net_id, |
| 1947 | .size = sizeof(struct ip6_tnl_net), |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 1948 | }; |
| 1949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | /** |
| 1951 | * ip6_tunnel_init - register protocol and reserve needed resources |
| 1952 | * |
| 1953 | * Return: 0 on success |
| 1954 | **/ |
| 1955 | |
| 1956 | static int __init ip6_tunnel_init(void) |
| 1957 | { |
| 1958 | int err; |
| 1959 | |
Eric W. Biederman | ac31cd3 | 2009-11-29 15:46:15 +0000 | [diff] [blame] | 1960 | err = register_pernet_device(&ip6_tnl_net_ops); |
Pavel Emelyanov | 13eeb8e | 2008-04-16 01:22:02 -0700 | [diff] [blame] | 1961 | if (err < 0) |
Alexey Dobriyan | d5aa407 | 2010-02-16 09:05:04 +0000 | [diff] [blame] | 1962 | goto out_pernet; |
| 1963 | |
| 1964 | err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET); |
| 1965 | if (err < 0) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1966 | pr_err("%s: can't register ip4ip6\n", __func__); |
Alexey Dobriyan | d5aa407 | 2010-02-16 09:05:04 +0000 | [diff] [blame] | 1967 | goto out_ip4ip6; |
| 1968 | } |
| 1969 | |
| 1970 | err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6); |
| 1971 | if (err < 0) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1972 | pr_err("%s: can't register ip6ip6\n", __func__); |
Alexey Dobriyan | d5aa407 | 2010-02-16 09:05:04 +0000 | [diff] [blame] | 1973 | goto out_ip6ip6; |
| 1974 | } |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1975 | err = rtnl_link_register(&ip6_link_ops); |
| 1976 | if (err < 0) |
| 1977 | goto rtnl_link_failed; |
Alexey Dobriyan | d5aa407 | 2010-02-16 09:05:04 +0000 | [diff] [blame] | 1978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | return 0; |
Alexey Dobriyan | d5aa407 | 2010-02-16 09:05:04 +0000 | [diff] [blame] | 1980 | |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1981 | rtnl_link_failed: |
| 1982 | xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); |
Alexey Dobriyan | d5aa407 | 2010-02-16 09:05:04 +0000 | [diff] [blame] | 1983 | out_ip6ip6: |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1984 | xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET); |
Alexey Dobriyan | d5aa407 | 2010-02-16 09:05:04 +0000 | [diff] [blame] | 1985 | out_ip4ip6: |
| 1986 | unregister_pernet_device(&ip6_tnl_net_ops); |
| 1987 | out_pernet: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | return err; |
| 1989 | } |
| 1990 | |
| 1991 | /** |
| 1992 | * ip6_tunnel_cleanup - free resources and unregister protocol |
| 1993 | **/ |
| 1994 | |
| 1995 | static void __exit ip6_tunnel_cleanup(void) |
| 1996 | { |
Nicolas Dichtel | c075b13 | 2012-11-09 06:10:01 +0000 | [diff] [blame] | 1997 | rtnl_link_unregister(&ip6_link_ops); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 1998 | if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET)) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1999 | pr_info("%s: can't deregister ip4ip6\n", __func__); |
Yasuyuki Kozakai | c4d3efa | 2007-02-15 00:43:16 +0900 | [diff] [blame] | 2000 | |
Kazunori MIYAZAWA | 73d605d | 2007-02-13 12:55:55 -0800 | [diff] [blame] | 2001 | if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 2002 | pr_info("%s: can't deregister ip6ip6\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | |
Eric W. Biederman | ac31cd3 | 2009-11-29 15:46:15 +0000 | [diff] [blame] | 2004 | unregister_pernet_device(&ip6_tnl_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | module_init(ip6_tunnel_init); |
| 2008 | module_exit(ip6_tunnel_cleanup); |