blob: fad1af8ae1cf0af79c4514f9b5cd82ff9e81a128 [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 *
9 * $Id$
10 *
11 * Based on:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090012 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * RFC 2473
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 *
21 */
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>
42
43#include <asm/uaccess.h>
44#include <asm/atomic.h>
45
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090046#include <net/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/ip.h>
48#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/ip6_route.h>
50#include <net/addrconf.h>
51#include <net/ip6_tunnel.h>
52#include <net/xfrm.h>
53#include <net/dsfield.h>
54#include <net/inet_ecn.h>
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070055#include <net/net_namespace.h>
56#include <net/netns/generic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58MODULE_AUTHOR("Ville Nuorvala");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090059MODULE_DESCRIPTION("IPv6 tunneling device");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060MODULE_LICENSE("GPL");
61
62#define IPV6_TLV_TEL_DST_SIZE 8
63
64#ifdef IP6_TNL_DEBUG
Harvey Harrison0dc47872008-03-05 20:47:47 -080065#define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#else
67#define IP6_TNL_TRACE(x...) do {;} while(0)
68#endif
69
70#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090071#define IPV6_TCLASS_SHIFT 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#define HASH_SIZE 32
74
Al Viroe69a4adc2006-11-14 20:56:00 -080075#define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090076 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
77 (HASH_SIZE - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090079static int ip6_fb_tnl_dev_init(struct net_device *dev);
80static int ip6_tnl_dev_init(struct net_device *dev);
81static void ip6_tnl_dev_setup(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -070083static int ip6_tnl_net_id;
84struct ip6_tnl_net {
85};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* the IPv6 tunnel fallback device */
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090088static struct net_device *ip6_fb_tnl_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90
91/* lists for storing tunnels in use */
92static struct ip6_tnl *tnls_r_l[HASH_SIZE];
93static struct ip6_tnl *tnls_wc[1];
94static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
95
96/* lock for the tunnel lists */
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090097static DEFINE_RWLOCK(ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
100{
101 struct dst_entry *dst = t->dst_cache;
102
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900103 if (dst && dst->obsolete &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 dst->ops->check(dst, t->dst_cookie) == NULL) {
105 t->dst_cache = NULL;
106 dst_release(dst);
107 return NULL;
108 }
109
110 return dst;
111}
112
113static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
114{
115 dst_release(t->dst_cache);
116 t->dst_cache = NULL;
117}
118
119static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
120{
121 struct rt6_info *rt = (struct rt6_info *) dst;
122 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
123 dst_release(t->dst_cache);
124 t->dst_cache = dst;
125}
126
127/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900128 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900129 * @remote: the address of the tunnel exit-point
130 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900132 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900134 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * else %NULL
136 **/
137
138static struct ip6_tnl *
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700139ip6_tnl_lookup(struct net *net, struct in6_addr *remote, struct in6_addr *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 unsigned h0 = HASH(remote);
142 unsigned h1 = HASH(local);
143 struct ip6_tnl *t;
144
145 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
146 if (ipv6_addr_equal(local, &t->parms.laddr) &&
147 ipv6_addr_equal(remote, &t->parms.raddr) &&
148 (t->dev->flags & IFF_UP))
149 return t;
150 }
151 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
152 return t;
153
154 return NULL;
155}
156
157/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900158 * ip6_tnl_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900159 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
161 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900162 * ip6_tnl_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * &struct in6_addr entries laddr and raddr in @p.
164 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900165 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 **/
167
168static struct ip6_tnl **
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700169ip6_tnl_bucket(struct ip6_tnl_net *ip6n, struct ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct in6_addr *remote = &p->raddr;
172 struct in6_addr *local = &p->laddr;
173 unsigned h = 0;
174 int prio = 0;
175
176 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
177 prio = 1;
178 h = HASH(remote) ^ HASH(local);
179 }
180 return &tnls[prio][h];
181}
182
183/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900184 * ip6_tnl_link - add tunnel to hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * @t: tunnel to be added
186 **/
187
188static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700189ip6_tnl_link(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700191 struct ip6_tnl **tp = ip6_tnl_bucket(ip6n, &t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 t->next = *tp;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900194 write_lock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *tp = t;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900196 write_unlock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900200 * ip6_tnl_unlink - remove tunnel from hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * @t: tunnel to be removed
202 **/
203
204static void
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700205ip6_tnl_unlink(struct ip6_tnl_net *ip6n, struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 struct ip6_tnl **tp;
208
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700209 for (tp = ip6_tnl_bucket(ip6n, &t->parms); *tp; tp = &(*tp)->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (t == *tp) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900211 write_lock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *tp = t->next;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900213 write_unlock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 break;
215 }
216 }
217}
218
219/**
220 * ip6_tnl_create() - create a new tunnel
221 * @p: tunnel parameters
222 * @pt: pointer to new tunnel
223 *
224 * Description:
225 * Create tunnel matching given parameters.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900226 *
227 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800228 * created tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 **/
230
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700231static struct ip6_tnl *ip6_tnl_create(struct net *net, struct ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 struct net_device *dev;
234 struct ip6_tnl *t;
235 char name[IFNAMSIZ];
236 int err;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700237 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800239 if (p->name[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 strlcpy(name, p->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800241 else
242 sprintf(name, "ip6tnl%%d");
243
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900244 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (dev == NULL)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800246 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800248 if (strchr(name, '%')) {
249 if (dev_alloc_name(dev, name) < 0)
250 goto failed_free;
251 }
252
Patrick McHardy2941a482006-01-08 22:05:26 -0800253 t = netdev_priv(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900254 dev->init = ip6_tnl_dev_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 t->parms = *p;
256
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800257 if ((err = register_netdevice(dev)) < 0)
258 goto failed_free;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dev_hold(dev);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700261 ip6_tnl_link(ip6n, t);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800262 return t;
Pavel Emelyanovb37d428b2008-02-26 23:51:04 -0800263
264failed_free:
265 free_netdev(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800266failed:
267 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900271 * ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900272 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * @create: != 0 if allowed to create new tunnel if no match found
274 *
275 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900276 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * based on @parms. If this is unsuccessful, but @create is set a new
278 * tunnel device is created and registered for use.
279 *
280 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800281 * matching tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 **/
283
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700284static struct ip6_tnl *ip6_tnl_locate(struct net *net,
285 struct ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct in6_addr *remote = &p->raddr;
288 struct in6_addr *local = &p->laddr;
289 struct ip6_tnl *t;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700290 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700292 for (t = *ip6_tnl_bucket(ip6n, p); t; t = t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Ville Nuorvala567131a2006-11-24 17:05:41 -0800294 ipv6_addr_equal(remote, &t->parms.raddr))
295 return t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 if (!create)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800298 return NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700299 return ip6_tnl_create(net, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900303 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900305 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900307 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 **/
309
310static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900311ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Patrick McHardy2941a482006-01-08 22:05:26 -0800313 struct ip6_tnl *t = netdev_priv(dev);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700314 struct net *net = dev_net(dev);
315 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900317 if (dev == ip6_fb_tnl_dev) {
318 write_lock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 tnls_wc[0] = NULL;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900320 write_unlock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 } else {
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700322 ip6_tnl_unlink(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 ip6_tnl_dst_reset(t);
325 dev_put(dev);
326}
327
328/**
329 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
330 * @skb: received socket buffer
331 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900332 * Return:
333 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * else index to encapsulation limit
335 **/
336
337static __u16
338parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
339{
340 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
341 __u8 nexthdr = ipv6h->nexthdr;
342 __u16 off = sizeof (*ipv6h);
343
344 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
345 __u16 optlen = 0;
346 struct ipv6_opt_hdr *hdr;
347 if (raw + off + sizeof (*hdr) > skb->data &&
348 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
349 break;
350
351 hdr = (struct ipv6_opt_hdr *) (raw + off);
352 if (nexthdr == NEXTHDR_FRAGMENT) {
353 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
354 if (frag_hdr->frag_off)
355 break;
356 optlen = 8;
357 } else if (nexthdr == NEXTHDR_AUTH) {
358 optlen = (hdr->hdrlen + 2) << 2;
359 } else {
360 optlen = ipv6_optlen(hdr);
361 }
362 if (nexthdr == NEXTHDR_DEST) {
363 __u16 i = off + 2;
364 while (1) {
365 struct ipv6_tlv_tnl_enc_lim *tel;
366
367 /* No more room for encapsulation limit */
368 if (i + sizeof (*tel) > off + optlen)
369 break;
370
371 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
372 /* return index of option if found and valid */
373 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
374 tel->length == 1)
375 return i;
376 /* else jump to next option */
377 if (tel->type)
378 i += tel->length + 2;
379 else
380 i++;
381 }
382 }
383 nexthdr = hdr->nexthdr;
384 off += optlen;
385 }
386 return 0;
387}
388
389/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900390 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *
392 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900393 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * to the specifications in RFC 2473.
395 **/
396
Herbert Xud2acc342006-03-28 01:12:13 -0800397static int
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900398ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Al Viro704eae12007-07-26 17:33:29 +0100399 int *type, int *code, int *msg, __u32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
402 struct ip6_tnl *t;
403 int rel_msg = 0;
404 int rel_type = ICMPV6_DEST_UNREACH;
405 int rel_code = ICMPV6_ADDR_UNREACH;
406 __u32 rel_info = 0;
407 __u16 len;
Herbert Xud2acc342006-03-28 01:12:13 -0800408 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900410 /* If the packet doesn't contain the original IPv6 header we are
411 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 processing of the error. */
413
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900414 read_lock(&ip6_tnl_lock);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700415 if ((t = ip6_tnl_lookup(&init_net, &ipv6h->daddr,
416 &ipv6h->saddr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 goto out;
418
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900419 if (t->parms.proto != ipproto && t->parms.proto != 0)
420 goto out;
421
Herbert Xud2acc342006-03-28 01:12:13 -0800422 err = 0;
423
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900424 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 __u32 teli;
426 struct ipv6_tlv_tnl_enc_lim *tel;
427 __u32 mtu;
428 case ICMPV6_DEST_UNREACH:
429 if (net_ratelimit())
430 printk(KERN_WARNING
431 "%s: Path to destination invalid "
432 "or inactive!\n", t->parms.name);
433 rel_msg = 1;
434 break;
435 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900436 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (net_ratelimit())
438 printk(KERN_WARNING
439 "%s: Too small hop limit or "
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900440 "routing loop in tunnel!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 t->parms.name);
442 rel_msg = 1;
443 }
444 break;
445 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800446 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900447 if ((*code) == ICMPV6_HDR_FIELD)
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800448 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Al Viro704eae12007-07-26 17:33:29 +0100450 if (teli && teli == *info - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
452 if (tel->encap_limit == 0) {
453 if (net_ratelimit())
454 printk(KERN_WARNING
455 "%s: Too small encapsulation "
456 "limit or routing loop in "
457 "tunnel!\n", t->parms.name);
458 rel_msg = 1;
459 }
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800460 } else if (net_ratelimit()) {
461 printk(KERN_WARNING
462 "%s: Recipient unable to parse tunneled "
463 "packet!\n ", t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 break;
466 case ICMPV6_PKT_TOOBIG:
Al Viro704eae12007-07-26 17:33:29 +0100467 mtu = *info - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (mtu < IPV6_MIN_MTU)
469 mtu = IPV6_MIN_MTU;
470 t->dev->mtu = mtu;
471
Al Virocc6cdac2006-02-18 16:02:18 -0500472 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 rel_type = ICMPV6_PKT_TOOBIG;
474 rel_code = 0;
475 rel_info = mtu;
476 rel_msg = 1;
477 }
478 break;
479 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900480
481 *type = rel_type;
482 *code = rel_code;
483 *info = rel_info;
484 *msg = rel_msg;
485
486out:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900487 read_unlock(&ip6_tnl_lock);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900488 return err;
489}
490
491static int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900492ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Al Viro704eae12007-07-26 17:33:29 +0100493 int type, int code, int offset, __be32 info)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900494{
495 int rel_msg = 0;
496 int rel_type = type;
497 int rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100498 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900499 int err;
500 struct sk_buff *skb2;
501 struct iphdr *eiph;
502 struct flowi fl;
503 struct rtable *rt;
504
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900505 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
506 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900507 if (err < 0)
508 return err;
509
510 if (rel_msg == 0)
511 return 0;
512
513 switch (rel_type) {
514 case ICMPV6_DEST_UNREACH:
515 if (rel_code != ICMPV6_ADDR_UNREACH)
516 return 0;
517 rel_type = ICMP_DEST_UNREACH;
518 rel_code = ICMP_HOST_UNREACH;
519 break;
520 case ICMPV6_PKT_TOOBIG:
521 if (rel_code != 0)
522 return 0;
523 rel_type = ICMP_DEST_UNREACH;
524 rel_code = ICMP_FRAG_NEEDED;
525 break;
526 default:
527 return 0;
528 }
529
530 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
531 return 0;
532
533 skb2 = skb_clone(skb, GFP_ATOMIC);
534 if (!skb2)
535 return 0;
536
537 dst_release(skb2->dst);
538 skb2->dst = NULL;
539 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700540 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700541 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900542
543 /* Try to guess incoming interface */
544 memset(&fl, 0, sizeof(fl));
545 fl.fl4_dst = eiph->saddr;
546 fl.fl4_tos = RT_TOS(eiph->tos);
547 fl.proto = IPPROTO_IPIP;
Denis V. Lunevf2063512008-01-22 22:07:34 -0800548 if (ip_route_output_key(&init_net, &rt, &fl))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900549 goto out;
550
551 skb2->dev = rt->u.dst.dev;
552
553 /* route "incoming" packet */
554 if (rt->rt_flags & RTCF_LOCAL) {
555 ip_rt_put(rt);
556 rt = NULL;
557 fl.fl4_dst = eiph->daddr;
558 fl.fl4_src = eiph->saddr;
559 fl.fl4_tos = eiph->tos;
Denis V. Lunevf2063512008-01-22 22:07:34 -0800560 if (ip_route_output_key(&init_net, &rt, &fl) ||
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900561 rt->u.dst.dev->type != ARPHRD_TUNNEL) {
562 ip_rt_put(rt);
563 goto out;
564 }
Denis V. Lunev9937ded2008-02-18 20:49:36 -0800565 skb2->dst = (struct dst_entry *)rt;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900566 } else {
567 ip_rt_put(rt);
568 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
569 skb2->dev) ||
570 skb2->dst->dev->type != ARPHRD_TUNNEL)
571 goto out;
572 }
573
574 /* change mtu on this route */
575 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
576 if (rel_info > dst_mtu(skb2->dst))
577 goto out;
578
579 skb2->dst->ops->update_pmtu(skb2->dst, rel_info);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900580 }
581
Al Viro704eae12007-07-26 17:33:29 +0100582 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900583
584out:
585 kfree_skb(skb2);
586 return 0;
587}
588
589static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900590ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Al Viro704eae12007-07-26 17:33:29 +0100591 int type, int code, int offset, __be32 info)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900592{
593 int rel_msg = 0;
594 int rel_type = type;
595 int rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100596 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900597 int err;
598
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900599 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
600 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900601 if (err < 0)
602 return err;
603
604 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct rt6_info *rt;
606 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 dst_release(skb2->dst);
612 skb2->dst = NULL;
613 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700614 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* Try to guess incoming interface */
Daniel Lezcano606a2b42008-03-04 13:45:59 -0800617 rt = rt6_lookup(&init_net, &ipv6_hdr(skb2)->saddr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (rt && rt->rt6i_dev)
620 skb2->dev = rt->rt6i_dev;
621
622 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
623
624 if (rt)
625 dst_release(&rt->u.dst);
626
627 kfree_skb(skb2);
628 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900629
630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900633static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
634 struct ipv6hdr *ipv6h,
635 struct sk_buff *skb)
636{
637 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
638
639 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700640 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900641
642 if (INET_ECN_is_ce(dsfield))
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700643 IP_ECN_set_ce(ip_hdr(skb));
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900644}
645
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900646static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
647 struct ipv6hdr *ipv6h,
648 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900650 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800651 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900653 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700654 IP6_ECN_set_ce(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900656
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800657static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
658{
659 struct ip6_tnl_parm *p = &t->parms;
660 int ret = 0;
661
662 if (p->flags & IP6_TNL_F_CAP_RCV) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900663 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800664
665 if (p->link)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700666 ldev = dev_get_by_index(&init_net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800667
668 if ((ipv6_addr_is_multicast(&p->laddr) ||
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800669 likely(ipv6_chk_addr(&init_net, &p->laddr, ldev, 0))) &&
670 likely(!ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800671 ret = 1;
672
673 if (ldev)
674 dev_put(ldev);
675 }
676 return ret;
677}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900680 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900682 * @protocol: ethernet protocol ID
683 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
685 * Return: 0
686 **/
687
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900688static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900689 __u8 ipproto,
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900690 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
691 struct ipv6hdr *ipv6h,
692 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct ip6_tnl *t;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700695 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900697 read_lock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -0700699 if ((t = ip6_tnl_lookup(&init_net, &ipv6h->saddr,
700 &ipv6h->daddr)) != NULL) {
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900701 if (t->parms.proto != ipproto && t->parms.proto != 0) {
702 read_unlock(&ip6_tnl_lock);
703 goto discard;
704 }
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900707 read_unlock(&ip6_tnl_lock);
Herbert Xu50fba2a2006-04-04 13:50:45 -0700708 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800711 if (!ip6_tnl_rcv_ctl(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 t->stat.rx_dropped++;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900713 read_unlock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto discard;
715 }
716 secpath_reset(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700717 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700718 skb_reset_network_header(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900719 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 skb->pkt_type = PACKET_HOST;
721 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
722 skb->dev = t->dev;
723 dst_release(skb->dst);
724 skb->dst = NULL;
Yasuyuki Kozakai53ab61c2006-11-06 10:06:23 -0800725 nf_reset(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900726
727 dscp_ecn_decapsulate(t, ipv6h, skb);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 t->stat.rx_packets++;
730 t->stat.rx_bytes += skb->len;
731 netif_rx(skb);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900732 read_unlock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900735 read_unlock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700737
738discard:
739 kfree_skb(skb);
740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900743static int ip4ip6_rcv(struct sk_buff *skb)
744{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900745 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
746 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900747}
748
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900749static int ip6ip6_rcv(struct sk_buff *skb)
750{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900751 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
752 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900753}
754
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800755struct ipv6_tel_txoption {
756 struct ipv6_txoptions ops;
757 __u8 dst_opt[8];
758};
759
760static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800762 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800764 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
765 opt->dst_opt[3] = 1;
766 opt->dst_opt[4] = encap_limit;
767 opt->dst_opt[5] = IPV6_TLV_PADN;
768 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800770 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
771 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900775 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900777 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 *
779 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900780 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * doesn't match source of incoming packet.
782 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900783 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * 1 if conflict,
785 * 0 else
786 **/
787
788static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900789ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
792}
793
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800794static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
795{
796 struct ip6_tnl_parm *p = &t->parms;
797 int ret = 0;
798
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900799 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800800 struct net_device *ldev = NULL;
801
802 if (p->link)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700803 ldev = dev_get_by_index(&init_net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800804
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800805 if (unlikely(!ipv6_chk_addr(&init_net, &p->laddr, ldev, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800806 printk(KERN_WARNING
807 "%s xmit: Local address not yet configured!\n",
808 p->name);
809 else if (!ipv6_addr_is_multicast(&p->raddr) &&
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800810 unlikely(ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800811 printk(KERN_WARNING
812 "%s xmit: Routing loop! "
813 "Remote address found on this node!\n",
814 p->name);
815 else
816 ret = 1;
817 if (ldev)
818 dev_put(ldev);
819 }
820 return ret;
821}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900823 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900825 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900826 * @dsfield: dscp code for outer header
827 * @fl: flow of tunneled packet
828 * @encap_limit: encapsulation limit
829 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *
831 * Description:
832 * Build new header and do some sanity checks on the packet before sending
833 * it.
834 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900835 * Return:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900836 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900837 * -1 fail
838 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 **/
840
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900841static int ip6_tnl_xmit2(struct sk_buff *skb,
842 struct net_device *dev,
843 __u8 dsfield,
844 struct flowi *fl,
845 int encap_limit,
846 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Patrick McHardy2941a482006-01-08 22:05:26 -0800848 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct net_device_stats *stats = &t->stat;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700850 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800851 struct ipv6_tel_txoption opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct dst_entry *dst;
853 struct net_device *tdev;
854 int mtu;
Chuck Leverc2636b42007-10-23 21:07:32 -0700855 unsigned int max_headroom = sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900857 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if ((dst = ip6_tnl_dst_check(t)) != NULL)
861 dst_hold(dst);
Patrick McHardya57ebc92005-09-08 14:27:47 -0700862 else {
Daniel Lezcano4591db42008-03-05 10:48:10 -0800863 dst = ip6_route_output(&init_net, NULL, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900865 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
Patrick McHardya57ebc92005-09-08 14:27:47 -0700866 goto tx_err_link_failure;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 tdev = dst->dev;
870
871 if (tdev == dev) {
872 stats->collisions++;
873 if (net_ratelimit())
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900874 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 "%s: Local routing loop detected!\n",
876 t->parms.name);
877 goto tx_err_dst_release;
878 }
879 mtu = dst_mtu(dst) - sizeof (*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800880 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 max_headroom += 8;
882 mtu -= 8;
883 }
884 if (mtu < IPV6_MIN_MTU)
885 mtu = IPV6_MIN_MTU;
Yasuyuki Kozakai26892052006-09-10 03:59:17 +0900886 if (skb->dst)
887 skb->dst->ops->update_pmtu(skb->dst, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900889 *pmtu = mtu;
890 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 goto tx_err_dst_release;
892 }
893
894 /*
895 * Okay, now see if we can stuff it in the buffer as-is.
896 */
897 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900898
Patrick McHardycfbba492007-07-09 15:33:40 -0700899 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
900 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
904 goto tx_err_dst_release;
905
906 if (skb->sk)
907 skb_set_owner_w(new_skb, skb->sk);
908 kfree_skb(skb);
909 skb = new_skb;
910 }
911 dst_release(skb->dst);
912 skb->dst = dst_clone(dst);
913
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700914 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900916 proto = fl->proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800917 if (encap_limit >= 0) {
918 init_tel_txopt(&opt, encap_limit);
919 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
920 }
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700921 skb_push(skb, sizeof(struct ipv6hdr));
922 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700923 ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900924 *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 dsfield = INET_ECN_encapsulate(0, dsfield);
926 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 ipv6h->hop_limit = t->parms.hop_limit;
928 ipv6h->nexthdr = proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900929 ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
930 ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 nf_reset(skb);
932 pkt_len = skb->len;
Herbert Xuef76bc22008-01-11 19:15:08 -0800933 err = ip6_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200935 if (net_xmit_eval(err) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 stats->tx_bytes += pkt_len;
937 stats->tx_packets++;
938 } else {
939 stats->tx_errors++;
940 stats->tx_aborted_errors++;
941 }
942 ip6_tnl_dst_store(t, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
944tx_err_link_failure:
945 stats->tx_carrier_errors++;
946 dst_link_failure(skb);
947tx_err_dst_release:
948 dst_release(dst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900949 return err;
950}
951
952static inline int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900953ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
954{
955 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700956 struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900957 int encap_limit = -1;
958 struct flowi fl;
959 __u8 dsfield;
960 __u32 mtu;
961 int err;
962
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900963 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
964 !ip6_tnl_xmit_ctl(t))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900965 return -1;
966
967 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
968 encap_limit = t->parms.encap_limit;
969
970 memcpy(&fl, &t->fl, sizeof (fl));
971 fl.proto = IPPROTO_IPIP;
972
973 dsfield = ipv4_get_dsfield(iph);
974
975 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
Al Virob77f2fa2007-07-21 19:09:41 -0700976 fl.fl6_flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
977 & IPV6_TCLASS_MASK;
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900978
979 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
980 if (err != 0) {
981 /* XXX: send ICMP error even if DF is not set. */
982 if (err == -EMSGSIZE)
983 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
984 htonl(mtu));
985 return -1;
986 }
987
988 return 0;
989}
990
991static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900992ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
993{
994 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700995 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900996 int encap_limit = -1;
997 __u16 offset;
998 struct flowi fl;
999 __u8 dsfield;
1000 __u32 mtu;
1001 int err;
1002
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001003 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
1004 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001005 return -1;
1006
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001007 offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
1008 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001009 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001010 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001011 if (tel->encap_limit == 0) {
1012 icmpv6_send(skb, ICMPV6_PARAMPROB,
1013 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
1014 return -1;
1015 }
1016 encap_limit = tel->encap_limit - 1;
1017 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1018 encap_limit = t->parms.encap_limit;
1019
1020 memcpy(&fl, &t->fl, sizeof (fl));
1021 fl.proto = IPPROTO_IPV6;
1022
1023 dsfield = ipv6_get_dsfield(ipv6h);
1024 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
1025 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1026 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
1027 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1028
1029 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1030 if (err != 0) {
1031 if (err == -EMSGSIZE)
1032 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1033 return -1;
1034 }
1035
1036 return 0;
1037}
1038
1039static int
1040ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1041{
1042 struct ip6_tnl *t = netdev_priv(dev);
1043 struct net_device_stats *stats = &t->stat;
1044 int ret;
1045
1046 if (t->recursion++) {
1047 t->stat.collisions++;
1048 goto tx_err;
1049 }
1050
1051 switch (skb->protocol) {
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001052 case __constant_htons(ETH_P_IP):
1053 ret = ip4ip6_tnl_xmit(skb, dev);
1054 break;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001055 case __constant_htons(ETH_P_IPV6):
1056 ret = ip6ip6_tnl_xmit(skb, dev);
1057 break;
1058 default:
1059 goto tx_err;
1060 }
1061
1062 if (ret < 0)
1063 goto tx_err;
1064
1065 t->recursion--;
1066 return 0;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068tx_err:
1069 stats->tx_errors++;
1070 stats->tx_dropped++;
1071 kfree_skb(skb);
1072 t->recursion--;
1073 return 0;
1074}
1075
1076static void ip6_tnl_set_cap(struct ip6_tnl *t)
1077{
1078 struct ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001079 int ltype = ipv6_addr_type(&p->laddr);
1080 int rtype = ipv6_addr_type(&p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
1083
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001084 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1085 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1086 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001087 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001088 if (ltype&IPV6_ADDR_UNICAST)
1089 p->flags |= IP6_TNL_F_CAP_XMIT;
1090 if (rtype&IPV6_ADDR_UNICAST)
1091 p->flags |= IP6_TNL_F_CAP_RCV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093}
1094
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001095static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct net_device *dev = t->dev;
1098 struct ip6_tnl_parm *p = &t->parms;
1099 struct flowi *fl = &t->fl;
1100
1101 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1102 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1103
1104 /* Set up flowi template */
1105 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
1106 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
1107 fl->oif = p->link;
1108 fl->fl6_flowlabel = 0;
1109
1110 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1111 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1112 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1113 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1114
1115 ip6_tnl_set_cap(t);
1116
1117 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1118 dev->flags |= IFF_POINTOPOINT;
1119 else
1120 dev->flags &= ~IFF_POINTOPOINT;
1121
1122 dev->iflink = p->link;
1123
1124 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001125 int strict = (ipv6_addr_type(&p->raddr) &
1126 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1127
Daniel Lezcano606a2b42008-03-04 13:45:59 -08001128 struct rt6_info *rt = rt6_lookup(&init_net, &p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001129 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 if (rt == NULL)
1132 return;
1133
1134 if (rt->rt6i_dev) {
1135 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
1136 sizeof (struct ipv6hdr);
1137
1138 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
1139
1140 if (dev->mtu < IPV6_MIN_MTU)
1141 dev->mtu = IPV6_MIN_MTU;
1142 }
1143 dst_release(&rt->u.dst);
1144 }
1145}
1146
1147/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001148 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 * @t: tunnel to be changed
1150 * @p: tunnel configuration parameters
1151 * @active: != 0 if tunnel is ready for use
1152 *
1153 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001154 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 **/
1156
1157static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001158ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
1160 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
1161 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
1162 t->parms.flags = p->flags;
1163 t->parms.hop_limit = p->hop_limit;
1164 t->parms.encap_limit = p->encap_limit;
1165 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001166 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001167 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001168 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001169 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return 0;
1171}
1172
1173/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001174 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 * @dev: virtual device associated with tunnel
1176 * @ifr: parameters passed from userspace
1177 * @cmd: command to be performed
1178 *
1179 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001180 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001181 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 *
1183 * The possible commands are the following:
1184 * %SIOCGETTUNNEL: get tunnel parameters for device
1185 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1186 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1187 * %SIOCDELTUNNEL: delete tunnel
1188 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001189 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * initialization, can be used for creating other tunnel devices.
1191 *
1192 * Return:
1193 * 0 on success,
1194 * %-EFAULT if unable to copy data to or from userspace,
1195 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1196 * %-EINVAL if passed tunnel parameters are invalid,
1197 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1198 * %-ENODEV if attempting to change or delete a nonexisting device
1199 **/
1200
1201static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001202ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct ip6_tnl_parm p;
1206 struct ip6_tnl *t = NULL;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001207 struct net *net = dev_net(dev);
1208 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 switch (cmd) {
1211 case SIOCGETTUNNEL:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001212 if (dev == ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001213 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 err = -EFAULT;
1215 break;
1216 }
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001217 t = ip6_tnl_locate(net, &p, 0);
Ville Nuorvala567131a2006-11-24 17:05:41 -08001218 }
1219 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -08001220 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 memcpy(&p, &t->parms, sizeof (p));
1222 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1223 err = -EFAULT;
1224 }
1225 break;
1226 case SIOCADDTUNNEL:
1227 case SIOCCHGTUNNEL:
1228 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!capable(CAP_NET_ADMIN))
1230 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001231 err = -EFAULT;
1232 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001234 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001235 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1236 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 break;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001238 t = ip6_tnl_locate(net, &p, cmd == SIOCADDTUNNEL);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001239 if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001240 if (t != NULL) {
1241 if (t->dev != dev) {
1242 err = -EEXIST;
1243 break;
1244 }
1245 } else
1246 t = netdev_priv(dev);
1247
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001248 ip6_tnl_unlink(ip6n, t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001249 err = ip6_tnl_change(t, &p);
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001250 ip6_tnl_link(ip6n, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 netdev_state_change(dev);
1252 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001253 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 err = 0;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001255 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1256 err = -EFAULT;
1257
1258 } else
1259 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261 case SIOCDELTUNNEL:
1262 err = -EPERM;
1263 if (!capable(CAP_NET_ADMIN))
1264 break;
1265
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001266 if (dev == ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001267 err = -EFAULT;
1268 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001270 err = -ENOENT;
Pavel Emelyanov2dd02c82008-04-16 01:22:23 -07001271 if ((t = ip6_tnl_locate(net, &p, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001273 err = -EPERM;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001274 if (t->dev == ip6_fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001276 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001278 err = 0;
1279 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 break;
1281 default:
1282 err = -EINVAL;
1283 }
1284 return err;
1285}
1286
1287/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001288 * ip6_tnl_get_stats - return the stats for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 * @dev: virtual device associated with tunnel
1290 *
1291 * Return: stats for device
1292 **/
1293
1294static struct net_device_stats *
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001295ip6_tnl_get_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Patrick McHardy2941a482006-01-08 22:05:26 -08001297 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
1300/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001301 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 * @dev: virtual device associated with tunnel
1303 * @new_mtu: the new mtu
1304 *
1305 * Return:
1306 * 0 on success,
1307 * %-EINVAL if mtu too small
1308 **/
1309
1310static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001311ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 if (new_mtu < IPV6_MIN_MTU) {
1314 return -EINVAL;
1315 }
1316 dev->mtu = new_mtu;
1317 return 0;
1318}
1319
1320/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001321 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * @dev: virtual device associated with tunnel
1323 *
1324 * Description:
1325 * Initialize function pointers and device parameters
1326 **/
1327
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001328static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001330 dev->uninit = ip6_tnl_dev_uninit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 dev->destructor = free_netdev;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001332 dev->hard_start_xmit = ip6_tnl_xmit;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001333 dev->get_stats = ip6_tnl_get_stats;
1334 dev->do_ioctl = ip6_tnl_ioctl;
1335 dev->change_mtu = ip6_tnl_change_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 dev->type = ARPHRD_TUNNEL6;
1338 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1339 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1340 dev->flags |= IFF_NOARP;
1341 dev->addr_len = sizeof(struct in6_addr);
1342}
1343
1344
1345/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001346 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 * @dev: virtual device associated with tunnel
1348 **/
1349
1350static inline void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001351ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Patrick McHardy2941a482006-01-08 22:05:26 -08001353 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 t->dev = dev;
1355 strcpy(t->parms.name, dev->name);
1356}
1357
1358/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001359 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * @dev: virtual device associated with tunnel
1361 **/
1362
1363static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001364ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Patrick McHardy2941a482006-01-08 22:05:26 -08001366 struct ip6_tnl *t = netdev_priv(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001367 ip6_tnl_dev_init_gen(dev);
1368 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 0;
1370}
1371
1372/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001373 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * @dev: fallback device
1375 *
1376 * Return: 0
1377 **/
1378
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001379static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001380ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Patrick McHardy2941a482006-01-08 22:05:26 -08001382 struct ip6_tnl *t = netdev_priv(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001383 ip6_tnl_dev_init_gen(dev);
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001384 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 dev_hold(dev);
1386 tnls_wc[0] = t;
1387 return 0;
1388}
1389
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001390static struct xfrm6_tunnel ip4ip6_handler = {
1391 .handler = ip4ip6_rcv,
1392 .err_handler = ip4ip6_err,
1393 .priority = 1,
1394};
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396static struct xfrm6_tunnel ip6ip6_handler = {
Patrick McHardy03037702005-07-19 14:03:34 -07001397 .handler = ip6ip6_rcv,
1398 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001399 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400};
1401
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001402static int ip6_tnl_init_net(struct net *net)
1403{
1404 int err;
1405 struct ip6_tnl_net *ip6n;
1406
1407 err = -ENOMEM;
1408 ip6n = kmalloc(sizeof(struct ip6_tnl_net), GFP_KERNEL);
1409 if (ip6n == NULL)
1410 goto err_alloc;
1411
1412 err = net_assign_generic(net, ip6_tnl_net_id, ip6n);
1413 if (err < 0)
1414 goto err_assign;
1415
1416 return 0;
1417
1418err_assign:
1419 kfree(ip6n);
1420err_alloc:
1421 return err;
1422}
1423
1424static void ip6_tnl_exit_net(struct net *net)
1425{
1426 struct ip6_tnl_net *ip6n;
1427
1428 ip6n = net_generic(net, ip6_tnl_net_id);
1429 kfree(ip6n);
1430}
1431
1432static struct pernet_operations ip6_tnl_net_ops = {
1433 .init = ip6_tnl_init_net,
1434 .exit = ip6_tnl_exit_net,
1435};
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/**
1438 * ip6_tunnel_init - register protocol and reserve needed resources
1439 *
1440 * Return: 0 on success
1441 **/
1442
1443static int __init ip6_tunnel_init(void)
1444{
1445 int err;
1446
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001447 if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001448 printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001449 err = -EAGAIN;
1450 goto out;
1451 }
1452
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001453 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001454 printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001455 err = -EAGAIN;
1456 goto unreg_ip4ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001458 ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1459 ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001461 if (!ip6_fb_tnl_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 err = -ENOMEM;
1463 goto fail;
1464 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001465 ip6_fb_tnl_dev->init = ip6_fb_tnl_dev_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001467 if ((err = register_netdev(ip6_fb_tnl_dev))) {
1468 free_netdev(ip6_fb_tnl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 goto fail;
1470 }
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001471
1472 err = register_pernet_gen_device(&ip6_tnl_net_id, &ip6_tnl_net_ops);
1473 if (err < 0)
1474 goto err_pernet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return 0;
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001476err_pernet:
1477 unregister_netdevice(ip6_fb_tnl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478fail:
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001479 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001480unreg_ip4ip6:
1481 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1482out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return err;
1484}
1485
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001486static void __exit ip6_tnl_destroy_tunnels(void)
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001487{
1488 int h;
1489 struct ip6_tnl *t;
1490
1491 for (h = 0; h < HASH_SIZE; h++) {
1492 while ((t = tnls_r_l[h]) != NULL)
1493 unregister_netdevice(t->dev);
1494 }
1495
1496 t = tnls_wc[0];
1497 unregister_netdevice(t->dev);
1498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500/**
1501 * ip6_tunnel_cleanup - free resources and unregister protocol
1502 **/
1503
1504static void __exit ip6_tunnel_cleanup(void)
1505{
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001506 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001507 printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001508
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001509 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001510 printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001512 rtnl_lock();
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001513 ip6_tnl_destroy_tunnels();
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001514 rtnl_unlock();
Pavel Emelyanov13eeb8e2008-04-16 01:22:02 -07001515
1516 unregister_pernet_gen_device(ip6_tnl_net_id, &ip6_tnl_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519module_init(ip6_tunnel_init);
1520module_exit(ip6_tunnel_cleanup);