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