blob: 1e55866cead7425eefdff36c1ddca1aab9504286 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09002 * IPv6 tunneling device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Ville Nuorvala <vnuorval@tcs.hut.fi>
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09007 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Based on:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090010 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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 Perchesf3213832012-05-15 14:11:53 +000021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080024#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
26#include <linux/types.h>
27#include <linux/sockios.h>
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090028#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/if.h>
30#include <linux/in.h>
31#include <linux/ip.h>
32#include <linux/if_tunnel.h>
33#include <linux/net.h>
34#include <linux/in6.h>
35#include <linux/netdevice.h>
36#include <linux/if_arp.h>
37#include <linux/icmpv6.h>
38#include <linux/init.h>
39#include <linux/route.h>
40#include <linux/rtnetlink.h>
41#include <linux/netfilter_ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000043#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
Arun Sharma600634972011-07-26 16:09:06 -070046#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090048#include <net/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000050#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#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 Emelyanov13eeb8e2008-04-16 01:22:02 -070058#include <net/net_namespace.h>
59#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61MODULE_AUTHOR("Ville Nuorvala");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090062MODULE_DESCRIPTION("IPv6 tunneling device");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063MODULE_LICENSE("GPL");
stephen hemminger6dfbd872011-03-10 11:43:19 +000064MODULE_ALIAS_NETDEV("ip6tnl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#ifdef IP6_TNL_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +000067#define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#else
69#define IP6_TNL_TRACE(x...) do {;} while(0)
70#endif
71
72#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090073#define IPV6_TCLASS_SHIFT 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Eric Dumazetddbe5032012-07-18 08:11:12 +000075#define HASH_SIZE_SHIFT 5
76#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +000078static bool log_ecn_error = true;
79module_param(log_ecn_error, bool, 0644);
80MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
81
Eric Dumazetddbe5032012-07-18 08:11:12 +000082static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
83{
84 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
85
86 return hash_32(hash, HASH_SIZE_SHIFT);
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Eric Dumazet8560f222010-09-28 03:23:34 +000089static int ip6_tnl_dev_init(struct net_device *dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090090static void ip6_tnl_dev_setup(struct net_device *dev);
Nicolas Dichtelc075b132012-11-09 06:10:01 +000091static struct rtnl_link_ops ip6_link_ops __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Eric Dumazetf99189b2009-11-17 10:42:49 +000093static int ip6_tnl_net_id __read_mostly;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070094struct ip6_tnl_net {
Pavel Emelyanov15820e1292008-04-16 01:23:02 -070095 /* the IPv6 tunnel fallback device */
96 struct net_device *fb_tnl_dev;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -070097 /* lists for storing tunnels in use */
Eric Dumazet94767632010-09-15 20:25:34 +000098 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
99 struct ip6_tnl __rcu *tnls_wc[1];
100 struct ip6_tnl __rcu **tnls[2];
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -0700101};
102
Eric Dumazet8560f222010-09-28 03:23:34 +0000103static struct net_device_stats *ip6_get_stats(struct net_device *dev)
104{
105 struct pcpu_tstats sum = { 0 };
106 int i;
107
108 for_each_possible_cpu(i) {
109 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
110
111 sum.rx_packets += tstats->rx_packets;
112 sum.rx_bytes += tstats->rx_bytes;
113 sum.tx_packets += tstats->tx_packets;
114 sum.tx_bytes += tstats->tx_bytes;
115 }
116 dev->stats.rx_packets = sum.rx_packets;
117 dev->stats.rx_bytes = sum.rx_bytes;
118 dev->stats.tx_packets = sum.tx_packets;
119 dev->stats.tx_bytes = sum.tx_bytes;
120 return &dev->stats;
121}
122
Eric Dumazet2922bc82009-10-23 06:34:34 +0000123/*
Eric Dumazet94767632010-09-15 20:25:34 +0000124 * Locking : hash tables are protected by RCU and RTNL
Eric Dumazet2922bc82009-10-23 06:34:34 +0000125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000127struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct dst_entry *dst = t->dst_cache;
130
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900131 if (dst && dst->obsolete &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 dst->ops->check(dst, t->dst_cookie) == NULL) {
133 t->dst_cache = NULL;
134 dst_release(dst);
135 return NULL;
136 }
137
138 return dst;
139}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000140EXPORT_SYMBOL_GPL(ip6_tnl_dst_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000142void ip6_tnl_dst_reset(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 dst_release(t->dst_cache);
145 t->dst_cache = NULL;
146}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000147EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000149void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct rt6_info *rt = (struct rt6_info *) dst;
152 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
153 dst_release(t->dst_cache);
154 t->dst_cache = dst;
155}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000156EXPORT_SYMBOL_GPL(ip6_tnl_dst_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900159 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900160 * @remote: the address of the tunnel exit-point
161 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900163 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900165 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * else %NULL
167 **/
168
Eric Dumazet2922bc82009-10-23 06:34:34 +0000169#define for_each_ip6_tunnel_rcu(start) \
170 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static struct ip6_tnl *
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000173ip6_tnl_lookup(struct net *net, const struct in6_addr *remote, const struct in6_addr *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000175 unsigned int hash = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct ip6_tnl *t;
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700177 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Eric Dumazetddbe5032012-07-18 08:11:12 +0000179 for_each_ip6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (ipv6_addr_equal(local, &t->parms.laddr) &&
181 ipv6_addr_equal(remote, &t->parms.raddr) &&
182 (t->dev->flags & IFF_UP))
183 return t;
184 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000185 t = rcu_dereference(ip6n->tnls_wc[0]);
186 if (t && (t->dev->flags & IFF_UP))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return t;
188
189 return NULL;
190}
191
192/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900193 * ip6_tnl_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900194 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900197 * ip6_tnl_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * &struct in6_addr entries laddr and raddr in @p.
199 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900200 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 **/
202
Eric Dumazet94767632010-09-15 20:25:34 +0000203static struct ip6_tnl __rcu **
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000204ip6_tnl_bucket(struct ip6_tnl_net *ip6n, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000206 const struct in6_addr *remote = &p->raddr;
207 const struct in6_addr *local = &p->laddr;
Eric Dumazet95c96172012-04-15 05:58:06 +0000208 unsigned int h = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int prio = 0;
210
211 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
212 prio = 1;
Eric Dumazetddbe5032012-07-18 08:11:12 +0000213 h = HASH(remote, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -0700215 return &ip6n->tnls[prio][h];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900219 * ip6_tnl_link - add tunnel to hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * @t: tunnel to be added
221 **/
222
223static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700224ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Eric Dumazet94767632010-09-15 20:25:34 +0000226 struct ip6_tnl __rcu **tp = ip6_tnl_bucket(ip6n, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Eric Dumazetcf778b02012-01-12 04:41:32 +0000228 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
229 rcu_assign_pointer(*tp, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900233 * ip6_tnl_unlink - remove tunnel from hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * @t: tunnel to be removed
235 **/
236
237static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700238ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Eric Dumazet94767632010-09-15 20:25:34 +0000240 struct ip6_tnl __rcu **tp;
241 struct ip6_tnl *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Eric Dumazet94767632010-09-15 20:25:34 +0000243 for (tp = ip6_tnl_bucket(ip6n, &t->parms);
244 (iter = rtnl_dereference(*tp)) != NULL;
245 tp = &iter->next) {
246 if (t == iter) {
Eric Dumazetcf778b02012-01-12 04:41:32 +0000247 rcu_assign_pointer(*tp, t->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 break;
249 }
250 }
251}
252
Eric Dumazet8560f222010-09-28 03:23:34 +0000253static void ip6_dev_free(struct net_device *dev)
254{
255 free_percpu(dev->tstats);
256 free_netdev(dev);
257}
258
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000259static int ip6_tnl_create2(struct net_device *dev)
260{
261 struct ip6_tnl *t = netdev_priv(dev);
262 struct net *net = dev_net(dev);
263 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
264 int err;
265
266 t = netdev_priv(dev);
267 err = ip6_tnl_dev_init(dev);
268 if (err < 0)
269 goto out;
270
271 err = register_netdevice(dev);
272 if (err < 0)
273 goto out;
274
275 strcpy(t->parms.name, dev->name);
276 dev->rtnl_link_ops = &ip6_link_ops;
277
278 dev_hold(dev);
279 ip6_tnl_link(ip6n, t);
280 return 0;
281
282out:
283 return err;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/**
Ben Hutchings2c530402012-07-10 10:55:09 +0000287 * ip6_tnl_create - create a new tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * @p: tunnel parameters
289 * @pt: pointer to new tunnel
290 *
291 * Description:
292 * Create tunnel matching given parameters.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900293 *
294 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800295 * created tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 **/
297
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000298static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct net_device *dev;
301 struct ip6_tnl *t;
302 char name[IFNAMSIZ];
303 int err;
304
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800305 if (p->name[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 strlcpy(name, p->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800307 else
308 sprintf(name, "ip6tnl%%d");
309
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900310 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (dev == NULL)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800312 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Pavel Emelyanov554eb272008-04-16 01:24:13 -0700314 dev_net_set(dev, net);
315
Patrick McHardy2941a482006-01-08 22:05:26 -0800316 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 t->parms = *p;
Nicolas Dichtel0b112452012-11-14 05:14:00 +0000318 err = ip6_tnl_create2(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +0000319 if (err < 0)
320 goto failed_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Ville Nuorvala567131a2006-11-24 17:05:41 -0800322 return t;
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800323
324failed_free:
Eric Dumazet8560f222010-09-28 03:23:34 +0000325 ip6_dev_free(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800326failed:
327 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900331 * ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900332 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * @create: != 0 if allowed to create new tunnel if no match found
334 *
335 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900336 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * based on @parms. If this is unsuccessful, but @create is set a new
338 * tunnel device is created and registered for use.
339 *
340 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800341 * matching tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 **/
343
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700344static struct ip6_tnl *ip6_tnl_locate(struct net *net,
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000345 struct __ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000347 const struct in6_addr *remote = &p->raddr;
348 const struct in6_addr *local = &p->laddr;
Eric Dumazet94767632010-09-15 20:25:34 +0000349 struct ip6_tnl __rcu **tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct ip6_tnl *t;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700351 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Eric Dumazet94767632010-09-15 20:25:34 +0000353 for (tp = ip6_tnl_bucket(ip6n, p);
354 (t = rtnl_dereference(*tp)) != NULL;
355 tp = &t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Ville Nuorvala567131a2006-11-24 17:05:41 -0800357 ipv6_addr_equal(remote, &t->parms.raddr))
358 return t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 if (!create)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800361 return NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700362 return ip6_tnl_create(net, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900366 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900368 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900370 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 **/
372
373static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900374ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Patrick McHardy2941a482006-01-08 22:05:26 -0800376 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700377 struct net *net = dev_net(dev);
378 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Eric Dumazet94767632010-09-15 20:25:34 +0000380 if (dev == ip6n->fb_tnl_dev)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000381 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
Eric Dumazet94767632010-09-15 20:25:34 +0000382 else
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700383 ip6_tnl_unlink(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 ip6_tnl_dst_reset(t);
385 dev_put(dev);
386}
387
388/**
389 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
390 * @skb: received socket buffer
391 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900392 * Return:
393 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * else index to encapsulation limit
395 **/
396
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000397__u16 ip6_tnl_parse_tlv_enc_lim(struct sk_buff *skb, __u8 *raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000399 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) raw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 __u8 nexthdr = ipv6h->nexthdr;
401 __u16 off = sizeof (*ipv6h);
402
403 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
404 __u16 optlen = 0;
405 struct ipv6_opt_hdr *hdr;
406 if (raw + off + sizeof (*hdr) > skb->data &&
407 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
408 break;
409
410 hdr = (struct ipv6_opt_hdr *) (raw + off);
411 if (nexthdr == NEXTHDR_FRAGMENT) {
412 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
413 if (frag_hdr->frag_off)
414 break;
415 optlen = 8;
416 } else if (nexthdr == NEXTHDR_AUTH) {
417 optlen = (hdr->hdrlen + 2) << 2;
418 } else {
419 optlen = ipv6_optlen(hdr);
420 }
421 if (nexthdr == NEXTHDR_DEST) {
422 __u16 i = off + 2;
423 while (1) {
424 struct ipv6_tlv_tnl_enc_lim *tel;
425
426 /* No more room for encapsulation limit */
427 if (i + sizeof (*tel) > off + optlen)
428 break;
429
430 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
431 /* return index of option if found and valid */
432 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
433 tel->length == 1)
434 return i;
435 /* else jump to next option */
436 if (tel->type)
437 i += tel->length + 2;
438 else
439 i++;
440 }
441 }
442 nexthdr = hdr->nexthdr;
443 off += optlen;
444 }
445 return 0;
446}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000447EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900450 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
452 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900453 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * to the specifications in RFC 2473.
455 **/
456
Herbert Xud2acc342006-03-28 01:12:13 -0800457static int
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900458ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700459 u8 *type, u8 *code, int *msg, __u32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000461 const struct ipv6hdr *ipv6h = (const struct ipv6hdr *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct ip6_tnl *t;
463 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700464 u8 rel_type = ICMPV6_DEST_UNREACH;
465 u8 rel_code = ICMPV6_ADDR_UNREACH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 __u32 rel_info = 0;
467 __u16 len;
Herbert Xud2acc342006-03-28 01:12:13 -0800468 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900470 /* If the packet doesn't contain the original IPv6 header we are
471 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 processing of the error. */
473
Eric Dumazet2922bc82009-10-23 06:34:34 +0000474 rcu_read_lock();
Pavel Emelyanov8704ca7e2008-04-16 01:22:43 -0700475 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr,
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700476 &ipv6h->saddr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 goto out;
478
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900479 if (t->parms.proto != ipproto && t->parms.proto != 0)
480 goto out;
481
Herbert Xud2acc342006-03-28 01:12:13 -0800482 err = 0;
483
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900484 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 __u32 teli;
486 struct ipv6_tlv_tnl_enc_lim *tel;
487 __u32 mtu;
488 case ICMPV6_DEST_UNREACH:
Joe Perchese87cc472012-05-13 21:56:26 +0000489 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
490 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 rel_msg = 1;
492 break;
493 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900494 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Joe Perchese87cc472012-05-13 21:56:26 +0000495 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
496 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 rel_msg = 1;
498 }
499 break;
500 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800501 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900502 if ((*code) == ICMPV6_HDR_FIELD)
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000503 teli = ip6_tnl_parse_tlv_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Al Viro704eae12007-07-26 17:33:29 +0100505 if (teli && teli == *info - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
507 if (tel->encap_limit == 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000508 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
509 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 rel_msg = 1;
511 }
Joe Perchese87cc472012-05-13 21:56:26 +0000512 } else {
513 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
514 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516 break;
517 case ICMPV6_PKT_TOOBIG:
Al Viro704eae12007-07-26 17:33:29 +0100518 mtu = *info - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (mtu < IPV6_MIN_MTU)
520 mtu = IPV6_MIN_MTU;
521 t->dev->mtu = mtu;
522
Al Virocc6cdac2006-02-18 16:02:18 -0500523 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 rel_type = ICMPV6_PKT_TOOBIG;
525 rel_code = 0;
526 rel_info = mtu;
527 rel_msg = 1;
528 }
529 break;
530 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900531
532 *type = rel_type;
533 *code = rel_code;
534 *info = rel_info;
535 *msg = rel_msg;
536
537out:
Eric Dumazet2922bc82009-10-23 06:34:34 +0000538 rcu_read_unlock();
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900539 return err;
540}
541
542static int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900543ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700544 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900545{
546 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700547 u8 rel_type = type;
548 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100549 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900550 int err;
551 struct sk_buff *skb2;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000552 const struct iphdr *eiph;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900553 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -0700554 struct flowi4 fl4;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900555
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900556 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
557 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900558 if (err < 0)
559 return err;
560
561 if (rel_msg == 0)
562 return 0;
563
564 switch (rel_type) {
565 case ICMPV6_DEST_UNREACH:
566 if (rel_code != ICMPV6_ADDR_UNREACH)
567 return 0;
568 rel_type = ICMP_DEST_UNREACH;
569 rel_code = ICMP_HOST_UNREACH;
570 break;
571 case ICMPV6_PKT_TOOBIG:
572 if (rel_code != 0)
573 return 0;
574 rel_type = ICMP_DEST_UNREACH;
575 rel_code = ICMP_FRAG_NEEDED;
576 break;
David S. Millerec18d9a2012-07-12 00:25:15 -0700577 case NDISC_REDIRECT:
578 rel_type = ICMP_REDIRECT;
579 rel_code = ICMP_REDIR_HOST;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900580 default:
581 return 0;
582 }
583
584 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
585 return 0;
586
587 skb2 = skb_clone(skb, GFP_ATOMIC);
588 if (!skb2)
589 return 0;
590
Eric Dumazetadf30902009-06-02 05:19:30 +0000591 skb_dst_drop(skb2);
592
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900593 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700594 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700595 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900596
597 /* Try to guess incoming interface */
David S. Miller31e45432011-05-03 20:25:42 -0700598 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500599 eiph->saddr, 0,
600 0, 0,
601 IPPROTO_IPIP, RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800602 if (IS_ERR(rt))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900603 goto out;
604
Changli Gaod8d1f302010-06-10 23:31:35 -0700605 skb2->dev = rt->dst.dev;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900606
607 /* route "incoming" packet */
608 if (rt->rt_flags & RTCF_LOCAL) {
609 ip_rt_put(rt);
610 rt = NULL;
David S. Miller31e45432011-05-03 20:25:42 -0700611 rt = ip_route_output_ports(dev_net(skb->dev), &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -0500612 eiph->daddr, eiph->saddr,
613 0, 0,
614 IPPROTO_IPIP,
615 RT_TOS(eiph->tos), 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -0800616 if (IS_ERR(rt) ||
Changli Gaod8d1f302010-06-10 23:31:35 -0700617 rt->dst.dev->type != ARPHRD_TUNNEL) {
David S. Millerb23dd4f2011-03-02 14:31:35 -0800618 if (!IS_ERR(rt))
619 ip_rt_put(rt);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900620 goto out;
621 }
David S. Millerb23dd4f2011-03-02 14:31:35 -0800622 skb_dst_set(skb2, &rt->dst);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900623 } else {
624 ip_rt_put(rt);
625 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
626 skb2->dev) ||
Eric Dumazetadf30902009-06-02 05:19:30 +0000627 skb_dst(skb2)->dev->type != ARPHRD_TUNNEL)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900628 goto out;
629 }
630
631 /* change mtu on this route */
632 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000633 if (rel_info > dst_mtu(skb_dst(skb2)))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900634 goto out;
635
David S. Miller6700c272012-07-17 03:29:28 -0700636 skb_dst(skb2)->ops->update_pmtu(skb_dst(skb2), NULL, skb2, rel_info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900637 }
David S. Miller1ed5c482012-07-12 00:41:25 -0700638 if (rel_type == ICMP_REDIRECT)
David S. Miller6700c272012-07-17 03:29:28 -0700639 skb_dst(skb2)->ops->redirect(skb_dst(skb2), NULL, skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900640
Al Viro704eae12007-07-26 17:33:29 +0100641 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900642
643out:
644 kfree_skb(skb2);
645 return 0;
646}
647
648static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900649ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700650 u8 type, u8 code, int offset, __be32 info)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900651{
652 int rel_msg = 0;
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700653 u8 rel_type = type;
654 u8 rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100655 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900656 int err;
657
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900658 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
659 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900660 if (err < 0)
661 return err;
662
663 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct rt6_info *rt;
665 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Eric Dumazetadf30902009-06-02 05:19:30 +0000670 skb_dst_drop(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700672 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* Try to guess incoming interface */
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700675 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr,
676 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
David S. Millerd1918542011-12-28 20:19:20 -0500678 if (rt && rt->dst.dev)
679 skb2->dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000681 icmpv6_send(skb2, rel_type, rel_code, rel_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Amerigo Wang94e187c2012-10-29 00:13:19 +0000683 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 kfree_skb(skb2);
686 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900687
688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000691static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
692 const struct ipv6hdr *ipv6h,
693 struct sk_buff *skb)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900694{
695 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
696
697 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700698 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900699
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000700 return IP6_ECN_decapsulate(ipv6h, skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900701}
702
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000703static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl *t,
704 const struct ipv6hdr *ipv6h,
705 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900707 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800708 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000710 return IP6_ECN_decapsulate(ipv6h, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900712
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000713__u32 ip6_tnl_get_cap(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000714 const struct in6_addr *laddr,
715 const struct in6_addr *raddr)
716{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000717 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000718 int ltype = ipv6_addr_type(laddr);
719 int rtype = ipv6_addr_type(raddr);
720 __u32 flags = 0;
721
722 if (ltype == IPV6_ADDR_ANY || rtype == IPV6_ADDR_ANY) {
723 flags = IP6_TNL_F_CAP_PER_PACKET;
724 } else if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
725 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
726 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
727 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
728 if (ltype&IPV6_ADDR_UNICAST)
729 flags |= IP6_TNL_F_CAP_XMIT;
730 if (rtype&IPV6_ADDR_UNICAST)
731 flags |= IP6_TNL_F_CAP_RCV;
732 }
733 return flags;
734}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000735EXPORT_SYMBOL(ip6_tnl_get_cap);
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000736
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100737/* called with rcu_read_lock() */
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000738int ip6_tnl_rcv_ctl(struct ip6_tnl *t,
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000739 const struct in6_addr *laddr,
740 const struct in6_addr *raddr)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800741{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000742 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800743 int ret = 0;
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700744 struct net *net = dev_net(t->dev);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800745
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000746 if ((p->flags & IP6_TNL_F_CAP_RCV) ||
747 ((p->flags & IP6_TNL_F_CAP_PER_PACKET) &&
748 (ip6_tnl_get_cap(t, laddr, raddr) & IP6_TNL_F_CAP_RCV))) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900749 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800750
751 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100752 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800753
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000754 if ((ipv6_addr_is_multicast(laddr) ||
755 likely(ipv6_chk_addr(net, laddr, ldev, 0))) &&
756 likely(!ipv6_chk_addr(net, raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800757 ret = 1;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800758 }
759 return ret;
760}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000761EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900764 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900766 * @protocol: ethernet protocol ID
767 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *
769 * Return: 0
770 **/
771
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900772static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900773 __u8 ipproto,
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000774 int (*dscp_ecn_decapsulate)(const struct ip6_tnl *t,
775 const struct ipv6hdr *ipv6h,
776 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct ip6_tnl *t;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000779 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000780 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Eric Dumazet2922bc82009-10-23 06:34:34 +0000782 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Pavel Emelyanov8704ca7e2008-04-16 01:22:43 -0700784 if ((t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700785 &ipv6h->daddr)) != NULL) {
Eric Dumazet8560f222010-09-28 03:23:34 +0000786 struct pcpu_tstats *tstats;
787
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900788 if (t->parms.proto != ipproto && t->parms.proto != 0) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000789 rcu_read_unlock();
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900790 goto discard;
791 }
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Eric Dumazet2922bc82009-10-23 06:34:34 +0000794 rcu_read_unlock();
Herbert Xu50fba2a2006-04-04 13:50:45 -0700795 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
Ville Nuorvalad0087b22012-06-28 18:15:52 +0000798 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700799 t->dev->stats.rx_dropped++;
Eric Dumazet2922bc82009-10-23 06:34:34 +0000800 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto discard;
802 }
803 secpath_reset(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700804 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700805 skb_reset_network_header(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900806 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 skb->pkt_type = PACKET_HOST;
808 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
Eric Dumazetd19d56d2010-05-17 22:36:55 -0700809
Nicolas Dichtelf4e0b4c2012-11-27 03:07:11 +0000810 __skb_tunnel_rx(skb, t->dev);
811
812 err = dscp_ecn_decapsulate(t, ipv6h, skb);
813 if (unlikely(err)) {
814 if (log_ecn_error)
815 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
816 &ipv6h->saddr,
817 ipv6_get_dsfield(ipv6h));
818 if (err > 1) {
819 ++t->dev->stats.rx_frame_errors;
820 ++t->dev->stats.rx_errors;
821 rcu_read_unlock();
822 goto discard;
823 }
824 }
825
Eric Dumazet8560f222010-09-28 03:23:34 +0000826 tstats = this_cpu_ptr(t->dev->tstats);
827 tstats->rx_packets++;
828 tstats->rx_bytes += skb->len;
829
Eric Dumazetcaf586e2010-09-30 21:06:55 +0000830 netif_rx(skb);
Eric Dumazet8990f462010-09-20 00:12:11 +0000831
Eric Dumazet2922bc82009-10-23 06:34:34 +0000832 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return 0;
834 }
Eric Dumazet2922bc82009-10-23 06:34:34 +0000835 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700837
838discard:
839 kfree_skb(skb);
840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900843static int ip4ip6_rcv(struct sk_buff *skb)
844{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900845 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
846 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900847}
848
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900849static int ip6ip6_rcv(struct sk_buff *skb)
850{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900851 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
852 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900853}
854
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800855struct ipv6_tel_txoption {
856 struct ipv6_txoptions ops;
857 __u8 dst_opt[8];
858};
859
860static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800862 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800864 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
865 opt->dst_opt[3] = 1;
866 opt->dst_opt[4] = encap_limit;
867 opt->dst_opt[5] = IPV6_TLV_PADN;
868 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800870 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
871 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900875 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900877 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 *
879 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900880 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * doesn't match source of incoming packet.
882 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900883 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 * 1 if conflict,
885 * 0 else
886 **/
887
Eric Dumazet92113bf2012-05-18 08:14:11 +0200888static inline bool
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000889ip6_tnl_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
892}
893
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000894int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800895{
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000896 struct __ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800897 int ret = 0;
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700898 struct net *net = dev_net(t->dev);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800899
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900900 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800901 struct net_device *ldev = NULL;
902
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100903 rcu_read_lock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800904 if (p->link)
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100905 ldev = dev_get_by_index_rcu(net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800906
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700907 if (unlikely(!ipv6_chk_addr(net, &p->laddr, ldev, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000908 pr_warn("%s xmit: Local address not yet configured!\n",
909 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800910 else if (!ipv6_addr_is_multicast(&p->raddr) &&
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -0700911 unlikely(ipv6_chk_addr(net, &p->raddr, NULL, 0)))
Joe Perchesf3213832012-05-15 14:11:53 +0000912 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
913 p->name);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800914 else
915 ret = 1;
Eric Dumazetf1a28ea2009-11-02 11:21:37 +0100916 rcu_read_unlock();
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800917 }
918 return ret;
919}
xeb@mail.ruc12b3952012-08-10 00:51:50 +0000920EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900923 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900925 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900926 * @dsfield: dscp code for outer header
927 * @fl: flow of tunneled packet
928 * @encap_limit: encapsulation limit
929 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *
931 * Description:
932 * Build new header and do some sanity checks on the packet before sending
933 * it.
934 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900935 * Return:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900936 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900937 * -1 fail
938 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 **/
940
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900941static int ip6_tnl_xmit2(struct sk_buff *skb,
942 struct net_device *dev,
943 __u8 dsfield,
David S. Miller4c9483b2011-03-12 16:22:43 -0500944 struct flowi6 *fl6,
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900945 int encap_limit,
946 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Alexey Dobriyan52479b62008-11-25 17:35:18 -0800948 struct net *net = dev_net(dev);
Patrick McHardy2941a482006-01-08 22:05:26 -0800949 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -0700950 struct net_device_stats *stats = &t->dev->stats;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700951 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800952 struct ipv6_tel_txoption opt;
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400953 struct dst_entry *dst = NULL, *ndst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 struct net_device *tdev;
955 int mtu;
Chuck Leverc2636b42007-10-23 21:07:32 -0700956 unsigned int max_headroom = sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900958 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Eric Dumazetd24f22f2011-09-20 14:50:00 -0400960 if (!fl6->flowi6_mark)
961 dst = ip6_tnl_dst_check(t);
Eric Dumazet89b02122011-07-28 04:32:25 +0000962 if (!dst) {
963 ndst = ip6_route_output(net, NULL, fl6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Eric Dumazet89b02122011-07-28 04:32:25 +0000965 if (ndst->error)
Patrick McHardya57ebc92005-09-08 14:27:47 -0700966 goto tx_err_link_failure;
Eric Dumazet89b02122011-07-28 04:32:25 +0000967 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(fl6), NULL, 0);
968 if (IS_ERR(ndst)) {
969 err = PTR_ERR(ndst);
970 ndst = NULL;
David S. Miller452edd52011-03-02 13:27:41 -0800971 goto tx_err_link_failure;
972 }
Eric Dumazet89b02122011-07-28 04:32:25 +0000973 dst = ndst;
Patrick McHardya57ebc92005-09-08 14:27:47 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 tdev = dst->dev;
977
978 if (tdev == dev) {
979 stats->collisions++;
Joe Perchese87cc472012-05-13 21:56:26 +0000980 net_warn_ratelimited("%s: Local routing loop detected!\n",
981 t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 goto tx_err_dst_release;
983 }
984 mtu = dst_mtu(dst) - sizeof (*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800985 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 max_headroom += 8;
987 mtu -= 8;
988 }
989 if (mtu < IPV6_MIN_MTU)
990 mtu = IPV6_MIN_MTU;
Eric Dumazetadf30902009-06-02 05:19:30 +0000991 if (skb_dst(skb))
David S. Miller6700c272012-07-17 03:29:28 -0700992 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900994 *pmtu = mtu;
995 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 goto tx_err_dst_release;
997 }
998
999 /*
1000 * Okay, now see if we can stuff it in the buffer as-is.
1001 */
1002 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001003
Patrick McHardycfbba492007-07-09 15:33:40 -07001004 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
1005 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
1009 goto tx_err_dst_release;
1010
1011 if (skb->sk)
1012 skb_set_owner_w(new_skb, skb->sk);
Eric Dumazet9ff26442012-04-19 02:24:17 +00001013 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 skb = new_skb;
1015 }
Eric Dumazetadf30902009-06-02 05:19:30 +00001016 skb_dst_drop(skb);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001017 if (fl6->flowi6_mark) {
1018 skb_dst_set(skb, dst);
1019 ndst = NULL;
1020 } else {
1021 skb_dst_set_noref(skb, dst);
1022 }
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001023 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
David S. Miller4c9483b2011-03-12 16:22:43 -05001025 proto = fl6->flowi6_proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -08001026 if (encap_limit >= 0) {
1027 init_tel_txopt(&opt, encap_limit);
1028 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
1029 }
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001030 skb_push(skb, sizeof(struct ipv6hdr));
1031 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001032 ipv6h = ipv6_hdr(skb);
YOSHIFUJI Hideaki / 吉藤英明3e4e4c12013-01-13 05:01:39 +00001033 ip6_flow_hdr(ipv6h, INET_ECN_encapsulate(0, dsfield), fl6->flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 ipv6h->hop_limit = t->parms.hop_limit;
1035 ipv6h->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001036 ipv6h->saddr = fl6->saddr;
1037 ipv6h->daddr = fl6->daddr;
Cong Wange8f72ea2013-03-09 23:00:39 +00001038 ip6tunnel_xmit(skb, dev);
Eric Dumazet89b02122011-07-28 04:32:25 +00001039 if (ndst)
1040 ip6_tnl_dst_store(t, ndst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return 0;
1042tx_err_link_failure:
1043 stats->tx_carrier_errors++;
1044 dst_link_failure(skb);
1045tx_err_dst_release:
Eric Dumazet89b02122011-07-28 04:32:25 +00001046 dst_release(ndst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001047 return err;
1048}
1049
1050static inline int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001051ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1052{
1053 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001054 const struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001055 int encap_limit = -1;
David S. Miller4c9483b2011-03-12 16:22:43 -05001056 struct flowi6 fl6;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001057 __u8 dsfield;
1058 __u32 mtu;
1059 int err;
1060
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001061 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
1062 !ip6_tnl_xmit_ctl(t))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001063 return -1;
1064
1065 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1066 encap_limit = t->parms.encap_limit;
1067
David S. Miller4c9483b2011-03-12 16:22:43 -05001068 memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
1069 fl6.flowi6_proto = IPPROTO_IPIP;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001070
1071 dsfield = ipv4_get_dsfield(iph);
1072
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001073 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001074 fl6.flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
Al Virob77f2fa2007-07-21 19:09:41 -07001075 & IPV6_TCLASS_MASK;
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001076 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1077 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001078
David S. Miller4c9483b2011-03-12 16:22:43 -05001079 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001080 if (err != 0) {
1081 /* XXX: send ICMP error even if DF is not set. */
1082 if (err == -EMSGSIZE)
1083 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
1084 htonl(mtu));
1085 return -1;
1086 }
1087
1088 return 0;
1089}
1090
1091static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001092ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1093{
1094 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001095 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001096 int encap_limit = -1;
1097 __u16 offset;
David S. Miller4c9483b2011-03-12 16:22:43 -05001098 struct flowi6 fl6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001099 __u8 dsfield;
1100 __u32 mtu;
1101 int err;
1102
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001103 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
1104 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001105 return -1;
1106
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001107 offset = ip6_tnl_parse_tlv_enc_lim(skb, skb_network_header(skb));
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001108 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001109 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001110 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001111 if (tel->encap_limit == 0) {
1112 icmpv6_send(skb, ICMPV6_PARAMPROB,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001113 ICMPV6_HDR_FIELD, offset + 2);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001114 return -1;
1115 }
1116 encap_limit = tel->encap_limit - 1;
1117 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1118 encap_limit = t->parms.encap_limit;
1119
David S. Miller4c9483b2011-03-12 16:22:43 -05001120 memcpy(&fl6, &t->fl.u.ip6, sizeof (fl6));
1121 fl6.flowi6_proto = IPPROTO_IPV6;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001122
1123 dsfield = ipv6_get_dsfield(ipv6h);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001124 if (t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)
David S. Miller4c9483b2011-03-12 16:22:43 -05001125 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001126 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)
David S. Miller4c9483b2011-03-12 16:22:43 -05001127 fl6.flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
Eric Dumazetd24f22f2011-09-20 14:50:00 -04001128 if (t->parms.flags & IP6_TNL_F_USE_ORIG_FWMARK)
1129 fl6.flowi6_mark = skb->mark;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001130
David S. Miller4c9483b2011-03-12 16:22:43 -05001131 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl6, encap_limit, &mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001132 if (err != 0) {
1133 if (err == -EMSGSIZE)
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +00001134 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001135 return -1;
1136 }
1137
1138 return 0;
1139}
1140
Stephen Hemminger6fef4c02009-08-31 19:50:41 +00001141static netdev_tx_t
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001142ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1143{
1144 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3dca02a2008-05-21 14:17:05 -07001145 struct net_device_stats *stats = &t->dev->stats;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001146 int ret;
1147
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001148 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001149 case htons(ETH_P_IP):
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001150 ret = ip4ip6_tnl_xmit(skb, dev);
1151 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -07001152 case htons(ETH_P_IPV6):
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001153 ret = ip6ip6_tnl_xmit(skb, dev);
1154 break;
1155 default:
1156 goto tx_err;
1157 }
1158
1159 if (ret < 0)
1160 goto tx_err;
1161
Patrick McHardy6ed10652009-06-23 06:03:08 +00001162 return NETDEV_TX_OK;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164tx_err:
1165 stats->tx_errors++;
1166 stats->tx_dropped++;
1167 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001168 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001171static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 struct net_device *dev = t->dev;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001174 struct __ip6_tnl_parm *p = &t->parms;
David S. Miller4c9483b2011-03-12 16:22:43 -05001175 struct flowi6 *fl6 = &t->fl.u.ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Jiri Pirko3a6d54c2009-05-11 23:37:15 +00001177 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1178 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 /* Set up flowi template */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001181 fl6->saddr = p->laddr;
1182 fl6->daddr = p->raddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001183 fl6->flowi6_oif = p->link;
1184 fl6->flowlabel = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
David S. Miller4c9483b2011-03-12 16:22:43 -05001187 fl6->flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
David S. Miller4c9483b2011-03-12 16:22:43 -05001189 fl6->flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001191 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV|IP6_TNL_F_CAP_PER_PACKET);
1192 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1195 dev->flags |= IFF_POINTOPOINT;
1196 else
1197 dev->flags &= ~IFF_POINTOPOINT;
1198
1199 dev->iflink = p->link;
1200
1201 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001202 int strict = (ipv6_addr_type(&p->raddr) &
1203 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1204
Pavel Emelyanov2f7f54b2008-04-16 01:23:44 -07001205 struct rt6_info *rt = rt6_lookup(dev_net(dev),
1206 &p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001207 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (rt == NULL)
1210 return;
1211
David S. Millerd1918542011-12-28 20:19:20 -05001212 if (rt->dst.dev) {
1213 dev->hard_header_len = rt->dst.dev->hard_header_len +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 sizeof (struct ipv6hdr);
1215
David S. Millerd1918542011-12-28 20:19:20 -05001216 dev->mtu = rt->dst.dev->mtu - sizeof (struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001217 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1218 dev->mtu-=8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 if (dev->mtu < IPV6_MIN_MTU)
1221 dev->mtu = IPV6_MIN_MTU;
1222 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00001223 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225}
1226
1227/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001228 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 * @t: tunnel to be changed
1230 * @p: tunnel configuration parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 *
1232 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001233 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 **/
1235
1236static int
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001237ip6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001239 t->parms.laddr = p->laddr;
1240 t->parms.raddr = p->raddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 t->parms.flags = p->flags;
1242 t->parms.hop_limit = p->hop_limit;
1243 t->parms.encap_limit = p->encap_limit;
1244 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001245 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001246 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001247 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001248 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return 0;
1250}
1251
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001252static int ip6_tnl_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
1253{
1254 struct net *net = dev_net(t->dev);
1255 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1256 int err;
1257
1258 ip6_tnl_unlink(ip6n, t);
1259 synchronize_net();
1260 err = ip6_tnl_change(t, p);
1261 ip6_tnl_link(ip6n, t);
1262 netdev_state_change(t->dev);
1263 return err;
1264}
1265
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001266static void
1267ip6_tnl_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm *u)
1268{
1269 p->laddr = u->laddr;
1270 p->raddr = u->raddr;
1271 p->flags = u->flags;
1272 p->hop_limit = u->hop_limit;
1273 p->encap_limit = u->encap_limit;
1274 p->flowinfo = u->flowinfo;
1275 p->link = u->link;
1276 p->proto = u->proto;
1277 memcpy(p->name, u->name, sizeof(u->name));
1278}
1279
1280static void
1281ip6_tnl_parm_to_user(struct ip6_tnl_parm *u, const struct __ip6_tnl_parm *p)
1282{
1283 u->laddr = p->laddr;
1284 u->raddr = p->raddr;
1285 u->flags = p->flags;
1286 u->hop_limit = p->hop_limit;
1287 u->encap_limit = p->encap_limit;
1288 u->flowinfo = p->flowinfo;
1289 u->link = p->link;
1290 u->proto = p->proto;
1291 memcpy(u->name, p->name, sizeof(u->name));
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001295 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * @dev: virtual device associated with tunnel
1297 * @ifr: parameters passed from userspace
1298 * @cmd: command to be performed
1299 *
1300 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001301 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001302 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 *
1304 * The possible commands are the following:
1305 * %SIOCGETTUNNEL: get tunnel parameters for device
1306 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1307 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1308 * %SIOCDELTUNNEL: delete tunnel
1309 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001310 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * initialization, can be used for creating other tunnel devices.
1312 *
1313 * Return:
1314 * 0 on success,
1315 * %-EFAULT if unable to copy data to or from userspace,
1316 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1317 * %-EINVAL if passed tunnel parameters are invalid,
1318 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1319 * %-ENODEV if attempting to change or delete a nonexisting device
1320 **/
1321
1322static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001323ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct ip6_tnl_parm p;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001327 struct __ip6_tnl_parm p1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct ip6_tnl *t = NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001329 struct net *net = dev_net(dev);
1330 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 switch (cmd) {
1333 case SIOCGETTUNNEL:
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001334 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001335 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 err = -EFAULT;
1337 break;
1338 }
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001339 ip6_tnl_parm_from_user(&p1, &p);
1340 t = ip6_tnl_locate(net, &p1, 0);
Dan Carpenter5ef5d6c2012-08-16 03:14:04 +00001341 } else {
1342 memset(&p, 0, sizeof(p));
Ville Nuorvala567131a2006-11-24 17:05:41 -08001343 }
1344 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -08001345 t = netdev_priv(dev);
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001346 ip6_tnl_parm_to_user(&p, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1348 err = -EFAULT;
1349 }
1350 break;
1351 case SIOCADDTUNNEL:
1352 case SIOCCHGTUNNEL:
1353 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001354 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001356 err = -EFAULT;
1357 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001359 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001360 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1361 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 break;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001363 ip6_tnl_parm_from_user(&p1, &p);
1364 t = ip6_tnl_locate(net, &p1, cmd == SIOCADDTUNNEL);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001365 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001366 if (t != NULL) {
1367 if (t->dev != dev) {
1368 err = -EEXIST;
1369 break;
1370 }
1371 } else
1372 t = netdev_priv(dev);
1373
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001374 err = ip6_tnl_update(t, &p1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001376 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 err = 0;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001378 ip6_tnl_parm_to_user(&p, &t->parms);
1379 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
Ville Nuorvala567131a2006-11-24 17:05:41 -08001380 err = -EFAULT;
1381
1382 } else
1383 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
1385 case SIOCDELTUNNEL:
1386 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00001387 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001390 if (dev == ip6n->fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001391 err = -EFAULT;
1392 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001394 err = -ENOENT;
xeb@mail.ruc12b3952012-08-10 00:51:50 +00001395 ip6_tnl_parm_from_user(&p1, &p);
1396 t = ip6_tnl_locate(net, &p1, 0);
1397 if (t == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001399 err = -EPERM;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001400 if (t->dev == ip6n->fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001402 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001404 err = 0;
1405 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 break;
1407 default:
1408 err = -EINVAL;
1409 }
1410 return err;
1411}
1412
1413/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001414 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 * @dev: virtual device associated with tunnel
1416 * @new_mtu: the new mtu
1417 *
1418 * Return:
1419 * 0 on success,
1420 * %-EINVAL if mtu too small
1421 **/
1422
1423static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001424ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 if (new_mtu < IPV6_MIN_MTU) {
1427 return -EINVAL;
1428 }
1429 dev->mtu = new_mtu;
1430 return 0;
1431}
1432
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001433
1434static const struct net_device_ops ip6_tnl_netdev_ops = {
Eric Dumazet8560f222010-09-28 03:23:34 +00001435 .ndo_uninit = ip6_tnl_dev_uninit,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001436 .ndo_start_xmit = ip6_tnl_xmit,
Eric Dumazet8560f222010-09-28 03:23:34 +00001437 .ndo_do_ioctl = ip6_tnl_ioctl,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001438 .ndo_change_mtu = ip6_tnl_change_mtu,
Eric Dumazet8560f222010-09-28 03:23:34 +00001439 .ndo_get_stats = ip6_get_stats,
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001440};
1441
Eric Dumazet8560f222010-09-28 03:23:34 +00001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001444 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 * @dev: virtual device associated with tunnel
1446 *
1447 * Description:
1448 * Initialize function pointers and device parameters
1449 **/
1450
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001451static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Anders Franzen381601e2010-11-24 05:47:18 +00001453 struct ip6_tnl *t;
1454
Stephen Hemminger1326c3d2008-11-20 20:33:56 -08001455 dev->netdev_ops = &ip6_tnl_netdev_ops;
Eric Dumazet8560f222010-09-28 03:23:34 +00001456 dev->destructor = ip6_dev_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 dev->type = ARPHRD_TUNNEL6;
1459 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1460 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
Anders Franzen381601e2010-11-24 05:47:18 +00001461 t = netdev_priv(dev);
1462 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1463 dev->mtu-=8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 dev->flags |= IFF_NOARP;
1465 dev->addr_len = sizeof(struct in6_addr);
Pavel Emelyanov554eb272008-04-16 01:24:13 -07001466 dev->features |= NETIF_F_NETNS_LOCAL;
Anders Franzen7e223de2010-10-19 03:50:47 +00001467 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470
1471/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001472 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * @dev: virtual device associated with tunnel
1474 **/
1475
Eric Dumazet8560f222010-09-28 03:23:34 +00001476static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001477ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Patrick McHardy2941a482006-01-08 22:05:26 -08001479 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 t->dev = dev;
Eric Dumazet8560f222010-09-28 03:23:34 +00001482 dev->tstats = alloc_percpu(struct pcpu_tstats);
1483 if (!dev->tstats)
1484 return -ENOMEM;
1485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001489 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 * @dev: virtual device associated with tunnel
1491 **/
1492
Eric Dumazet8560f222010-09-28 03:23:34 +00001493static int ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Patrick McHardy2941a482006-01-08 22:05:26 -08001495 struct ip6_tnl *t = netdev_priv(dev);
Eric Dumazet8560f222010-09-28 03:23:34 +00001496 int err = ip6_tnl_dev_init_gen(dev);
1497
1498 if (err)
1499 return err;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001500 ip6_tnl_link_config(t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
1504/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001505 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 * @dev: fallback device
1507 *
1508 * Return: 0
1509 **/
1510
Eric Dumazet8560f222010-09-28 03:23:34 +00001511static int __net_init ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Patrick McHardy2941a482006-01-08 22:05:26 -08001513 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001514 struct net *net = dev_net(dev);
1515 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Eric Dumazet8560f222010-09-28 03:23:34 +00001516 int err = ip6_tnl_dev_init_gen(dev);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001517
Eric Dumazet8560f222010-09-28 03:23:34 +00001518 if (err)
1519 return err;
1520
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001521 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 dev_hold(dev);
Ville Nuorvalad0087b22012-06-28 18:15:52 +00001523
1524 ip6_tnl_link_config(t);
1525
Eric Dumazetcf778b02012-01-12 04:41:32 +00001526 rcu_assign_pointer(ip6n->tnls_wc[0], t);
Eric Dumazet8560f222010-09-28 03:23:34 +00001527 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001530static int ip6_tnl_validate(struct nlattr *tb[], struct nlattr *data[])
1531{
1532 u8 proto;
1533
1534 if (!data)
1535 return 0;
1536
1537 proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1538 if (proto != IPPROTO_IPV6 &&
1539 proto != IPPROTO_IPIP &&
1540 proto != 0)
1541 return -EINVAL;
1542
1543 return 0;
1544}
1545
1546static void ip6_tnl_netlink_parms(struct nlattr *data[],
1547 struct __ip6_tnl_parm *parms)
1548{
1549 memset(parms, 0, sizeof(*parms));
1550
1551 if (!data)
1552 return;
1553
1554 if (data[IFLA_IPTUN_LINK])
1555 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1556
1557 if (data[IFLA_IPTUN_LOCAL])
1558 nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL],
1559 sizeof(struct in6_addr));
1560
1561 if (data[IFLA_IPTUN_REMOTE])
1562 nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE],
1563 sizeof(struct in6_addr));
1564
1565 if (data[IFLA_IPTUN_TTL])
1566 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
1567
1568 if (data[IFLA_IPTUN_ENCAP_LIMIT])
1569 parms->encap_limit = nla_get_u8(data[IFLA_IPTUN_ENCAP_LIMIT]);
1570
1571 if (data[IFLA_IPTUN_FLOWINFO])
Nicolas Dichtel1ff05fb2012-11-15 04:06:42 +00001572 parms->flowinfo = nla_get_be32(data[IFLA_IPTUN_FLOWINFO]);
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001573
1574 if (data[IFLA_IPTUN_FLAGS])
1575 parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
1576
1577 if (data[IFLA_IPTUN_PROTO])
1578 parms->proto = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1579}
1580
1581static int ip6_tnl_newlink(struct net *src_net, struct net_device *dev,
1582 struct nlattr *tb[], struct nlattr *data[])
1583{
1584 struct net *net = dev_net(dev);
1585 struct ip6_tnl *nt;
1586
1587 nt = netdev_priv(dev);
1588 ip6_tnl_netlink_parms(data, &nt->parms);
1589
1590 if (ip6_tnl_locate(net, &nt->parms, 0))
1591 return -EEXIST;
1592
1593 return ip6_tnl_create2(dev);
1594}
1595
1596static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1597 struct nlattr *data[])
1598{
1599 struct ip6_tnl *t;
1600 struct __ip6_tnl_parm p;
1601 struct net *net = dev_net(dev);
1602 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1603
1604 if (dev == ip6n->fb_tnl_dev)
1605 return -EINVAL;
1606
1607 ip6_tnl_netlink_parms(data, &p);
1608
1609 t = ip6_tnl_locate(net, &p, 0);
1610
1611 if (t) {
1612 if (t->dev != dev)
1613 return -EEXIST;
1614 } else
1615 t = netdev_priv(dev);
1616
1617 return ip6_tnl_update(t, &p);
1618}
1619
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001620static size_t ip6_tnl_get_size(const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001621{
1622 return
1623 /* IFLA_IPTUN_LINK */
1624 nla_total_size(4) +
1625 /* IFLA_IPTUN_LOCAL */
1626 nla_total_size(sizeof(struct in6_addr)) +
1627 /* IFLA_IPTUN_REMOTE */
1628 nla_total_size(sizeof(struct in6_addr)) +
1629 /* IFLA_IPTUN_TTL */
1630 nla_total_size(1) +
1631 /* IFLA_IPTUN_ENCAP_LIMIT */
1632 nla_total_size(1) +
1633 /* IFLA_IPTUN_FLOWINFO */
1634 nla_total_size(4) +
1635 /* IFLA_IPTUN_FLAGS */
1636 nla_total_size(4) +
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00001637 /* IFLA_IPTUN_PROTO */
1638 nla_total_size(1) +
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001639 0;
1640}
1641
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001642static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001643{
1644 struct ip6_tnl *tunnel = netdev_priv(dev);
1645 struct __ip6_tnl_parm *parm = &tunnel->parms;
1646
1647 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1648 nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr),
1649 &parm->raddr) ||
1650 nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr),
1651 &parm->laddr) ||
1652 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1653 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1654 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
Nicolas Dichtelcfa323b2012-11-14 05:13:58 +00001655 nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
1656 nla_put_u8(skb, IFLA_IPTUN_PROTO, parm->proto))
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001657 goto nla_put_failure;
1658 return 0;
1659
1660nla_put_failure:
1661 return -EMSGSIZE;
1662}
1663
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001664static const struct nla_policy ip6_tnl_policy[IFLA_IPTUN_MAX + 1] = {
1665 [IFLA_IPTUN_LINK] = { .type = NLA_U32 },
1666 [IFLA_IPTUN_LOCAL] = { .len = sizeof(struct in6_addr) },
1667 [IFLA_IPTUN_REMOTE] = { .len = sizeof(struct in6_addr) },
1668 [IFLA_IPTUN_TTL] = { .type = NLA_U8 },
1669 [IFLA_IPTUN_ENCAP_LIMIT] = { .type = NLA_U8 },
1670 [IFLA_IPTUN_FLOWINFO] = { .type = NLA_U32 },
1671 [IFLA_IPTUN_FLAGS] = { .type = NLA_U32 },
1672 [IFLA_IPTUN_PROTO] = { .type = NLA_U8 },
1673};
1674
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001675static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1676 .kind = "ip6tnl",
1677 .maxtype = IFLA_IPTUN_MAX,
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001678 .policy = ip6_tnl_policy,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001679 .priv_size = sizeof(struct ip6_tnl),
Nicolas Dichtel0b112452012-11-14 05:14:00 +00001680 .setup = ip6_tnl_dev_setup,
1681 .validate = ip6_tnl_validate,
1682 .newlink = ip6_tnl_newlink,
1683 .changelink = ip6_tnl_changelink,
Nicolas Dichtelb58d7312012-11-14 05:13:59 +00001684 .get_size = ip6_tnl_get_size,
1685 .fill_info = ip6_tnl_fill_info,
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001686};
1687
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001688static struct xfrm6_tunnel ip4ip6_handler __read_mostly = {
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001689 .handler = ip4ip6_rcv,
1690 .err_handler = ip4ip6_err,
1691 .priority = 1,
1692};
1693
Eric Dumazet3ff2cfa2010-08-30 10:27:10 +00001694static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
Patrick McHardy03037702005-07-19 14:03:34 -07001695 .handler = ip6ip6_rcv,
1696 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001697 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698};
1699
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001700static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n)
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001701{
1702 int h;
1703 struct ip6_tnl *t;
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001704 LIST_HEAD(list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001705
1706 for (h = 0; h < HASH_SIZE; h++) {
Eric Dumazet94767632010-09-15 20:25:34 +00001707 t = rtnl_dereference(ip6n->tnls_r_l[h]);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001708 while (t != NULL) {
1709 unregister_netdevice_queue(t->dev, &list);
Eric Dumazet94767632010-09-15 20:25:34 +00001710 t = rtnl_dereference(t->next);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001711 }
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001712 }
1713
Eric Dumazet94767632010-09-15 20:25:34 +00001714 t = rtnl_dereference(ip6n->tnls_wc[0]);
Eric Dumazetcf4432f2009-10-28 05:16:51 +00001715 unregister_netdevice_queue(t->dev, &list);
1716 unregister_netdevice_many(&list);
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001717}
1718
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001719static int __net_init ip6_tnl_init_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001720{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001721 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Josh Boyer731abb92011-11-10 15:10:23 +00001722 struct ip6_tnl *t = NULL;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001723 int err;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001724
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001725 ip6n->tnls[0] = ip6n->tnls_wc;
1726 ip6n->tnls[1] = ip6n->tnls_r_l;
1727
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001728 err = -ENOMEM;
1729 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1730 ip6_tnl_dev_setup);
1731
1732 if (!ip6n->fb_tnl_dev)
1733 goto err_alloc_dev;
Alexey Dobriyanbe77e592008-11-23 17:26:26 -08001734 dev_net_set(ip6n->fb_tnl_dev, net);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001735
Eric Dumazet8560f222010-09-28 03:23:34 +00001736 err = ip6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
1737 if (err < 0)
1738 goto err_register;
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001739
1740 err = register_netdev(ip6n->fb_tnl_dev);
1741 if (err < 0)
1742 goto err_register;
Josh Boyer731abb92011-11-10 15:10:23 +00001743
1744 t = netdev_priv(ip6n->fb_tnl_dev);
1745
1746 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001747 return 0;
1748
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001749err_register:
Eric Dumazet8560f222010-09-28 03:23:34 +00001750 ip6_dev_free(ip6n->fb_tnl_dev);
Pavel Emelyanov15820e1292008-04-16 01:23:02 -07001751err_alloc_dev:
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001752 return err;
1753}
1754
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001755static void __net_exit ip6_tnl_exit_net(struct net *net)
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001756{
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001757 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001758
Pavel Emelyanov3e6c9fb2008-04-16 01:23:22 -07001759 rtnl_lock();
1760 ip6_tnl_destroy_tunnels(ip6n);
1761 rtnl_unlock();
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001762}
1763
1764static struct pernet_operations ip6_tnl_net_ops = {
1765 .init = ip6_tnl_init_net,
1766 .exit = ip6_tnl_exit_net,
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001767 .id = &ip6_tnl_net_id,
1768 .size = sizeof(struct ip6_tnl_net),
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001769};
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771/**
1772 * ip6_tunnel_init - register protocol and reserve needed resources
1773 *
1774 * Return: 0 on success
1775 **/
1776
1777static int __init ip6_tunnel_init(void)
1778{
1779 int err;
1780
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001781 err = register_pernet_device(&ip6_tnl_net_ops);
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001782 if (err < 0)
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001783 goto out_pernet;
1784
1785 err = xfrm6_tunnel_register(&ip4ip6_handler, AF_INET);
1786 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001787 pr_err("%s: can't register ip4ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001788 goto out_ip4ip6;
1789 }
1790
1791 err = xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6);
1792 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00001793 pr_err("%s: can't register ip6ip6\n", __func__);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001794 goto out_ip6ip6;
1795 }
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001796 err = rtnl_link_register(&ip6_link_ops);
1797 if (err < 0)
1798 goto rtnl_link_failed;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return 0;
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001801
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001802rtnl_link_failed:
1803 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001804out_ip6ip6:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001805 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
Alexey Dobriyand5aa4072010-02-16 09:05:04 +00001806out_ip4ip6:
1807 unregister_pernet_device(&ip6_tnl_net_ops);
1808out_pernet:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return err;
1810}
1811
1812/**
1813 * ip6_tunnel_cleanup - free resources and unregister protocol
1814 **/
1815
1816static void __exit ip6_tunnel_cleanup(void)
1817{
Nicolas Dichtelc075b132012-11-09 06:10:01 +00001818 rtnl_link_unregister(&ip6_link_ops);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001819 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Joe Perchesf3213832012-05-15 14:11:53 +00001820 pr_info("%s: can't deregister ip4ip6\n", __func__);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001821
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001822 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Joe Perchesf3213832012-05-15 14:11:53 +00001823 pr_info("%s: can't deregister ip6ip6\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Eric W. Biedermanac31cd32009-11-29 15:46:15 +00001825 unregister_pernet_device(&ip6_tnl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
1828module_init(ip6_tunnel_init);
1829module_exit(ip6_tunnel_cleanup);