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