blob: 985d106dff6d312ad097e8a603a4af4f8afd8289 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 over IPv6 tunnel device
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Ville Nuorvala <vnuorval@tcs.hut.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * $Id$
9 *
10 * Based on:
11 * linux/net/ipv6/sit.c
12 *
13 * RFC 2473
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080023#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/sockios.h>
27#include <linux/if.h>
28#include <linux/in.h>
29#include <linux/ip.h>
30#include <linux/if_tunnel.h>
31#include <linux/net.h>
32#include <linux/in6.h>
33#include <linux/netdevice.h>
34#include <linux/if_arp.h>
35#include <linux/icmpv6.h>
36#include <linux/init.h>
37#include <linux/route.h>
38#include <linux/rtnetlink.h>
39#include <linux/netfilter_ipv6.h>
40
41#include <asm/uaccess.h>
42#include <asm/atomic.h>
43
44#include <net/ip.h>
45#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/ip6_route.h>
47#include <net/addrconf.h>
48#include <net/ip6_tunnel.h>
49#include <net/xfrm.h>
50#include <net/dsfield.h>
51#include <net/inet_ecn.h>
52
53MODULE_AUTHOR("Ville Nuorvala");
54MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
55MODULE_LICENSE("GPL");
56
57#define IPV6_TLV_TEL_DST_SIZE 8
58
59#ifdef IP6_TNL_DEBUG
60#define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
61#else
62#define IP6_TNL_TRACE(x...) do {;} while(0)
63#endif
64
65#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
66
67#define HASH_SIZE 32
68
Al Viroe69a4adc2006-11-14 20:56:00 -080069#define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090070 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
71 (HASH_SIZE - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
74static int ip6ip6_tnl_dev_init(struct net_device *dev);
75static void ip6ip6_tnl_dev_setup(struct net_device *dev);
76
77/* the IPv6 tunnel fallback device */
78static struct net_device *ip6ip6_fb_tnl_dev;
79
80
81/* lists for storing tunnels in use */
82static struct ip6_tnl *tnls_r_l[HASH_SIZE];
83static struct ip6_tnl *tnls_wc[1];
84static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
85
86/* lock for the tunnel lists */
87static DEFINE_RWLOCK(ip6ip6_lock);
88
89static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
90{
91 struct dst_entry *dst = t->dst_cache;
92
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090093 if (dst && dst->obsolete &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 dst->ops->check(dst, t->dst_cookie) == NULL) {
95 t->dst_cache = NULL;
96 dst_release(dst);
97 return NULL;
98 }
99
100 return dst;
101}
102
103static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
104{
105 dst_release(t->dst_cache);
106 t->dst_cache = NULL;
107}
108
109static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
110{
111 struct rt6_info *rt = (struct rt6_info *) dst;
112 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
113 dst_release(t->dst_cache);
114 t->dst_cache = dst;
115}
116
117/**
118 * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900119 * @remote: the address of the tunnel exit-point
120 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900122 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900124 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * else %NULL
126 **/
127
128static struct ip6_tnl *
129ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
130{
131 unsigned h0 = HASH(remote);
132 unsigned h1 = HASH(local);
133 struct ip6_tnl *t;
134
135 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
136 if (ipv6_addr_equal(local, &t->parms.laddr) &&
137 ipv6_addr_equal(remote, &t->parms.raddr) &&
138 (t->dev->flags & IFF_UP))
139 return t;
140 }
141 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
142 return t;
143
144 return NULL;
145}
146
147/**
148 * ip6ip6_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900149 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900152 * ip6ip6_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * &struct in6_addr entries laddr and raddr in @p.
154 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900155 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 **/
157
158static struct ip6_tnl **
159ip6ip6_bucket(struct ip6_tnl_parm *p)
160{
161 struct in6_addr *remote = &p->raddr;
162 struct in6_addr *local = &p->laddr;
163 unsigned h = 0;
164 int prio = 0;
165
166 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
167 prio = 1;
168 h = HASH(remote) ^ HASH(local);
169 }
170 return &tnls[prio][h];
171}
172
173/**
174 * ip6ip6_tnl_link - add tunnel to hash table
175 * @t: tunnel to be added
176 **/
177
178static void
179ip6ip6_tnl_link(struct ip6_tnl *t)
180{
181 struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
182
183 t->next = *tp;
184 write_lock_bh(&ip6ip6_lock);
185 *tp = t;
186 write_unlock_bh(&ip6ip6_lock);
187}
188
189/**
190 * ip6ip6_tnl_unlink - remove tunnel from hash table
191 * @t: tunnel to be removed
192 **/
193
194static void
195ip6ip6_tnl_unlink(struct ip6_tnl *t)
196{
197 struct ip6_tnl **tp;
198
199 for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) {
200 if (t == *tp) {
201 write_lock_bh(&ip6ip6_lock);
202 *tp = t->next;
203 write_unlock_bh(&ip6ip6_lock);
204 break;
205 }
206 }
207}
208
209/**
210 * ip6_tnl_create() - create a new tunnel
211 * @p: tunnel parameters
212 * @pt: pointer to new tunnel
213 *
214 * Description:
215 * Create tunnel matching given parameters.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900216 *
217 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800218 * created tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 **/
220
Ville Nuorvala567131a2006-11-24 17:05:41 -0800221static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct net_device *dev;
224 struct ip6_tnl *t;
225 char name[IFNAMSIZ];
226 int err;
227
228 if (p->name[0]) {
229 strlcpy(name, p->name, IFNAMSIZ);
230 } else {
231 int i;
232 for (i = 1; i < IP6_TNL_MAX; i++) {
233 sprintf(name, "ip6tnl%d", i);
234 if (__dev_get_by_name(name) == NULL)
235 break;
236 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900237 if (i == IP6_TNL_MAX)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800238 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
241 if (dev == NULL)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800242 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Patrick McHardy2941a482006-01-08 22:05:26 -0800244 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dev->init = ip6ip6_tnl_dev_init;
246 t->parms = *p;
247
248 if ((err = register_netdevice(dev)) < 0) {
249 free_netdev(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800250 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ip6ip6_tnl_link(t);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800254 return t;
255failed:
256 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259/**
260 * ip6ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900261 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * @create: != 0 if allowed to create new tunnel if no match found
263 *
264 * Description:
265 * ip6ip6_tnl_locate() first tries to locate an existing tunnel
266 * based on @parms. If this is unsuccessful, but @create is set a new
267 * tunnel device is created and registered for use.
268 *
269 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800270 * matching tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 **/
272
Ville Nuorvala567131a2006-11-24 17:05:41 -0800273static struct ip6_tnl *ip6ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 struct in6_addr *remote = &p->raddr;
276 struct in6_addr *local = &p->laddr;
277 struct ip6_tnl *t;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 for (t = *ip6ip6_bucket(p); t; t = t->next) {
280 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Ville Nuorvala567131a2006-11-24 17:05:41 -0800281 ipv6_addr_equal(remote, &t->parms.raddr))
282 return t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 if (!create)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800285 return NULL;
286 return ip6_tnl_create(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/**
290 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
291 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900292 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * Description:
294 * ip6ip6_tnl_dev_uninit() removes tunnel from its list
295 **/
296
297static void
298ip6ip6_tnl_dev_uninit(struct net_device *dev)
299{
Patrick McHardy2941a482006-01-08 22:05:26 -0800300 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (dev == ip6ip6_fb_tnl_dev) {
303 write_lock_bh(&ip6ip6_lock);
304 tnls_wc[0] = NULL;
305 write_unlock_bh(&ip6ip6_lock);
306 } else {
307 ip6ip6_tnl_unlink(t);
308 }
309 ip6_tnl_dst_reset(t);
310 dev_put(dev);
311}
312
313/**
314 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
315 * @skb: received socket buffer
316 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900317 * Return:
318 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * else index to encapsulation limit
320 **/
321
322static __u16
323parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
324{
325 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
326 __u8 nexthdr = ipv6h->nexthdr;
327 __u16 off = sizeof (*ipv6h);
328
329 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
330 __u16 optlen = 0;
331 struct ipv6_opt_hdr *hdr;
332 if (raw + off + sizeof (*hdr) > skb->data &&
333 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
334 break;
335
336 hdr = (struct ipv6_opt_hdr *) (raw + off);
337 if (nexthdr == NEXTHDR_FRAGMENT) {
338 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
339 if (frag_hdr->frag_off)
340 break;
341 optlen = 8;
342 } else if (nexthdr == NEXTHDR_AUTH) {
343 optlen = (hdr->hdrlen + 2) << 2;
344 } else {
345 optlen = ipv6_optlen(hdr);
346 }
347 if (nexthdr == NEXTHDR_DEST) {
348 __u16 i = off + 2;
349 while (1) {
350 struct ipv6_tlv_tnl_enc_lim *tel;
351
352 /* No more room for encapsulation limit */
353 if (i + sizeof (*tel) > off + optlen)
354 break;
355
356 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
357 /* return index of option if found and valid */
358 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
359 tel->length == 1)
360 return i;
361 /* else jump to next option */
362 if (tel->type)
363 i += tel->length + 2;
364 else
365 i++;
366 }
367 }
368 nexthdr = hdr->nexthdr;
369 off += optlen;
370 }
371 return 0;
372}
373
374/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900375 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 *
377 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900378 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * to the specifications in RFC 2473.
380 **/
381
Herbert Xud2acc342006-03-28 01:12:13 -0800382static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900383ip6_tnl_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
384 int *type, int *code, int *msg, __be32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
387 struct ip6_tnl *t;
388 int rel_msg = 0;
389 int rel_type = ICMPV6_DEST_UNREACH;
390 int rel_code = ICMPV6_ADDR_UNREACH;
391 __u32 rel_info = 0;
392 __u16 len;
Herbert Xud2acc342006-03-28 01:12:13 -0800393 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900395 /* If the packet doesn't contain the original IPv6 header we are
396 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 processing of the error. */
398
399 read_lock(&ip6ip6_lock);
400 if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
401 goto out;
402
Herbert Xud2acc342006-03-28 01:12:13 -0800403 err = 0;
404
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900405 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 __u32 teli;
407 struct ipv6_tlv_tnl_enc_lim *tel;
408 __u32 mtu;
409 case ICMPV6_DEST_UNREACH:
410 if (net_ratelimit())
411 printk(KERN_WARNING
412 "%s: Path to destination invalid "
413 "or inactive!\n", t->parms.name);
414 rel_msg = 1;
415 break;
416 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900417 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (net_ratelimit())
419 printk(KERN_WARNING
420 "%s: Too small hop limit or "
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900421 "routing loop in tunnel!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 t->parms.name);
423 rel_msg = 1;
424 }
425 break;
426 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800427 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900428 if ((*code) == ICMPV6_HDR_FIELD)
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800429 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900431 if (teli && teli == ntohl(*info) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
433 if (tel->encap_limit == 0) {
434 if (net_ratelimit())
435 printk(KERN_WARNING
436 "%s: Too small encapsulation "
437 "limit or routing loop in "
438 "tunnel!\n", t->parms.name);
439 rel_msg = 1;
440 }
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800441 } else if (net_ratelimit()) {
442 printk(KERN_WARNING
443 "%s: Recipient unable to parse tunneled "
444 "packet!\n ", t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 break;
447 case ICMPV6_PKT_TOOBIG:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900448 mtu = ntohl(*info) - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (mtu < IPV6_MIN_MTU)
450 mtu = IPV6_MIN_MTU;
451 t->dev->mtu = mtu;
452
Al Virocc6cdac2006-02-18 16:02:18 -0500453 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 rel_type = ICMPV6_PKT_TOOBIG;
455 rel_code = 0;
456 rel_info = mtu;
457 rel_msg = 1;
458 }
459 break;
460 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900461
462 *type = rel_type;
463 *code = rel_code;
464 *info = rel_info;
465 *msg = rel_msg;
466
467out:
468 read_unlock(&ip6ip6_lock);
469 return err;
470}
471
472static int
473ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
474 int type, int code, int offset, __u32 info)
475{
476 int rel_msg = 0;
477 int rel_type = type;
478 int rel_code = code;
479 __u32 rel_info = info;
480 int err;
481
482 err = ip6_tnl_err(skb, opt, &rel_type, &rel_code, &rel_msg, &rel_info,
483 offset);
484 if (err < 0)
485 return err;
486
487 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct rt6_info *rt;
489 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 dst_release(skb2->dst);
495 skb2->dst = NULL;
496 skb_pull(skb2, offset);
497 skb2->nh.raw = skb2->data;
498
499 /* Try to guess incoming interface */
500 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
501
502 if (rt && rt->rt6i_dev)
503 skb2->dev = rt->rt6i_dev;
504
505 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
506
507 if (rt)
508 dst_release(&rt->u.dst);
509
510 kfree_skb(skb2);
511 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900512
513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900516static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
517 struct ipv6hdr *ipv6h,
518 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900520 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
521 ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900523 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
524 IP6_ECN_set_ce(skb->nh.ipv6h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900526
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800527static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
528{
529 struct ip6_tnl_parm *p = &t->parms;
530 int ret = 0;
531
532 if (p->flags & IP6_TNL_F_CAP_RCV) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900533 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800534
535 if (p->link)
536 ldev = dev_get_by_index(p->link);
537
538 if ((ipv6_addr_is_multicast(&p->laddr) ||
539 likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
540 likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
541 ret = 1;
542
543 if (ldev)
544 dev_put(ldev);
545 }
546 return ret;
547}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549/**
550 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
551 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900552 * @protocol: ethernet protocol ID
553 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 *
555 * Return: 0
556 **/
557
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900558static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
559 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
560 struct ipv6hdr *ipv6h,
561 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct ipv6hdr *ipv6h;
564 struct ip6_tnl *t;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 ipv6h = skb->nh.ipv6h;
567
568 read_lock(&ip6ip6_lock);
569
570 if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
571 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Ken-ichirou MATSUZAWA9f0ede52005-11-09 13:08:29 -0800572 read_unlock(&ip6ip6_lock);
Herbert Xu50fba2a2006-04-04 13:50:45 -0700573 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800576 if (!ip6_tnl_rcv_ctl(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 t->stat.rx_dropped++;
578 read_unlock(&ip6ip6_lock);
579 goto discard;
580 }
581 secpath_reset(skb);
582 skb->mac.raw = skb->nh.raw;
583 skb->nh.raw = skb->data;
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900584 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 skb->pkt_type = PACKET_HOST;
586 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
587 skb->dev = t->dev;
588 dst_release(skb->dst);
589 skb->dst = NULL;
Yasuyuki Kozakai53ab61c2006-11-06 10:06:23 -0800590 nf_reset(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900591
592 dscp_ecn_decapsulate(t, ipv6h, skb);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 t->stat.rx_packets++;
595 t->stat.rx_bytes += skb->len;
596 netif_rx(skb);
597 read_unlock(&ip6ip6_lock);
598 return 0;
599 }
600 read_unlock(&ip6ip6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700602
603discard:
604 kfree_skb(skb);
605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900608static int ip6ip6_rcv(struct sk_buff *skb)
609{
610 return ip6_tnl_rcv(skb, ETH_P_IPV6, ip6ip6_dscp_ecn_decapsulate);
611}
612
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800613struct ipv6_tel_txoption {
614 struct ipv6_txoptions ops;
615 __u8 dst_opt[8];
616};
617
618static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800620 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800622 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
623 opt->dst_opt[3] = 1;
624 opt->dst_opt[4] = encap_limit;
625 opt->dst_opt[5] = IPV6_TLV_PADN;
626 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800628 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
629 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632/**
633 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
634 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900635 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 *
637 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900638 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 * doesn't match source of incoming packet.
640 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900641 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * 1 if conflict,
643 * 0 else
644 **/
645
646static inline int
647ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
648{
649 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
650}
651
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800652static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
653{
654 struct ip6_tnl_parm *p = &t->parms;
655 int ret = 0;
656
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900657 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800658 struct net_device *ldev = NULL;
659
660 if (p->link)
661 ldev = dev_get_by_index(p->link);
662
663 if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
664 printk(KERN_WARNING
665 "%s xmit: Local address not yet configured!\n",
666 p->name);
667 else if (!ipv6_addr_is_multicast(&p->raddr) &&
668 unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
669 printk(KERN_WARNING
670 "%s xmit: Routing loop! "
671 "Remote address found on this node!\n",
672 p->name);
673 else
674 ret = 1;
675 if (ldev)
676 dev_put(ldev);
677 }
678 return ret;
679}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680/**
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900681 * ip6ip6_tnl_xmit - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900683 * @dev: the outgoing tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
685 * Description:
686 * Build new header and do some sanity checks on the packet before sending
687 * it.
688 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900689 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * 0
691 **/
692
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900693static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
695{
Patrick McHardy2941a482006-01-08 22:05:26 -0800696 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct net_device_stats *stats = &t->stat;
698 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int encap_limit = -1;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800700 struct ipv6_tel_txoption opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 __u16 offset;
702 struct flowi fl;
703 struct dst_entry *dst;
704 struct net_device *tdev;
705 int mtu;
706 int max_headroom = sizeof(struct ipv6hdr);
707 u8 proto;
708 int err;
709 int pkt_len;
710 int dsfield;
711
712 if (t->recursion++) {
713 stats->collisions++;
714 goto tx_err;
715 }
716 if (skb->protocol != htons(ETH_P_IPV6) ||
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800717 !ip6_tnl_xmit_ctl(t) || ip6ip6_tnl_addr_conflict(t, ipv6h))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 goto tx_err;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
721 struct ipv6_tlv_tnl_enc_lim *tel;
722 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
723 if (tel->encap_limit == 0) {
724 icmpv6_send(skb, ICMPV6_PARAMPROB,
725 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
726 goto tx_err;
727 }
728 encap_limit = tel->encap_limit - 1;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800729 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 encap_limit = t->parms.encap_limit;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 memcpy(&fl, &t->fl, sizeof (fl));
733 proto = fl.proto;
734
735 dsfield = ipv6_get_dsfield(ipv6h);
736 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
Al Viro90bcaf72006-11-08 00:25:17 -0800737 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
Al Viro90bcaf72006-11-08 00:25:17 -0800739 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if ((dst = ip6_tnl_dst_check(t)) != NULL)
742 dst_hold(dst);
Patrick McHardya57ebc92005-09-08 14:27:47 -0700743 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dst = ip6_route_output(NULL, &fl);
745
Patrick McHardya57ebc92005-09-08 14:27:47 -0700746 if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0)
747 goto tx_err_link_failure;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 tdev = dst->dev;
751
752 if (tdev == dev) {
753 stats->collisions++;
754 if (net_ratelimit())
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900755 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 "%s: Local routing loop detected!\n",
757 t->parms.name);
758 goto tx_err_dst_release;
759 }
760 mtu = dst_mtu(dst) - sizeof (*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800761 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 max_headroom += 8;
763 mtu -= 8;
764 }
765 if (mtu < IPV6_MIN_MTU)
766 mtu = IPV6_MIN_MTU;
Yasuyuki Kozakai26892052006-09-10 03:59:17 +0900767 if (skb->dst)
768 skb->dst->ops->update_pmtu(skb->dst, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (skb->len > mtu) {
770 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
771 goto tx_err_dst_release;
772 }
773
774 /*
775 * Okay, now see if we can stuff it in the buffer as-is.
776 */
777 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900778
779 if (skb_headroom(skb) < max_headroom ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 skb_cloned(skb) || skb_shared(skb)) {
781 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
784 goto tx_err_dst_release;
785
786 if (skb->sk)
787 skb_set_owner_w(new_skb, skb->sk);
788 kfree_skb(skb);
789 skb = new_skb;
790 }
791 dst_release(skb->dst);
792 skb->dst = dst_clone(dst);
793
794 skb->h.raw = skb->nh.raw;
795
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800796 if (encap_limit >= 0) {
797 init_tel_txopt(&opt, encap_limit);
798 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
801 ipv6h = skb->nh.ipv6h;
Al Viro90bcaf72006-11-08 00:25:17 -0800802 *(__be32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 dsfield = INET_ECN_encapsulate(0, dsfield);
804 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
805 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
806 ipv6h->hop_limit = t->parms.hop_limit;
807 ipv6h->nexthdr = proto;
808 ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
809 ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
810 nf_reset(skb);
811 pkt_len = skb->len;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900812 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 skb->dst->dev, dst_output);
814
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200815 if (net_xmit_eval(err) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 stats->tx_bytes += pkt_len;
817 stats->tx_packets++;
818 } else {
819 stats->tx_errors++;
820 stats->tx_aborted_errors++;
821 }
822 ip6_tnl_dst_store(t, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 t->recursion--;
824 return 0;
825tx_err_link_failure:
826 stats->tx_carrier_errors++;
827 dst_link_failure(skb);
828tx_err_dst_release:
829 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830tx_err:
831 stats->tx_errors++;
832 stats->tx_dropped++;
833 kfree_skb(skb);
834 t->recursion--;
835 return 0;
836}
837
838static void ip6_tnl_set_cap(struct ip6_tnl *t)
839{
840 struct ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800841 int ltype = ipv6_addr_type(&p->laddr);
842 int rtype = ipv6_addr_type(&p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
845
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800846 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
847 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
848 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800849 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800850 if (ltype&IPV6_ADDR_UNICAST)
851 p->flags |= IP6_TNL_F_CAP_XMIT;
852 if (rtype&IPV6_ADDR_UNICAST)
853 p->flags |= IP6_TNL_F_CAP_RCV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855}
856
857static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
858{
859 struct net_device *dev = t->dev;
860 struct ip6_tnl_parm *p = &t->parms;
861 struct flowi *fl = &t->fl;
862
863 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
864 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
865
866 /* Set up flowi template */
867 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
868 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
869 fl->oif = p->link;
870 fl->fl6_flowlabel = 0;
871
872 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
873 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
874 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
875 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
876
877 ip6_tnl_set_cap(t);
878
879 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
880 dev->flags |= IFF_POINTOPOINT;
881 else
882 dev->flags &= ~IFF_POINTOPOINT;
883
884 dev->iflink = p->link;
885
886 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800887 int strict = (ipv6_addr_type(&p->raddr) &
888 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800891 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (rt == NULL)
894 return;
895
896 if (rt->rt6i_dev) {
897 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
898 sizeof (struct ipv6hdr);
899
900 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
901
902 if (dev->mtu < IPV6_MIN_MTU)
903 dev->mtu = IPV6_MIN_MTU;
904 }
905 dst_release(&rt->u.dst);
906 }
907}
908
909/**
910 * ip6ip6_tnl_change - update the tunnel parameters
911 * @t: tunnel to be changed
912 * @p: tunnel configuration parameters
913 * @active: != 0 if tunnel is ready for use
914 *
915 * Description:
916 * ip6ip6_tnl_change() updates the tunnel parameters
917 **/
918
919static int
920ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
921{
922 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
923 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
924 t->parms.flags = p->flags;
925 t->parms.hop_limit = p->hop_limit;
926 t->parms.encap_limit = p->encap_limit;
927 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -0700928 t->parms.link = p->link;
Hugo Santos0c088892006-02-24 13:16:25 -0800929 ip6_tnl_dst_reset(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 ip6ip6_tnl_link_config(t);
931 return 0;
932}
933
934/**
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900935 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * @dev: virtual device associated with tunnel
937 * @ifr: parameters passed from userspace
938 * @cmd: command to be performed
939 *
940 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900941 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
942 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
944 * The possible commands are the following:
945 * %SIOCGETTUNNEL: get tunnel parameters for device
946 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
947 * %SIOCCHGTUNNEL: change tunnel parameters to those given
948 * %SIOCDELTUNNEL: delete tunnel
949 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900950 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * initialization, can be used for creating other tunnel devices.
952 *
953 * Return:
954 * 0 on success,
955 * %-EFAULT if unable to copy data to or from userspace,
956 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
957 * %-EINVAL if passed tunnel parameters are invalid,
958 * %-EEXIST if changing a tunnel's parameters would cause a conflict
959 * %-ENODEV if attempting to change or delete a nonexisting device
960 **/
961
962static int
963ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
964{
965 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct ip6_tnl_parm p;
967 struct ip6_tnl *t = NULL;
968
969 switch (cmd) {
970 case SIOCGETTUNNEL:
971 if (dev == ip6ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -0800972 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 err = -EFAULT;
974 break;
975 }
Ville Nuorvala567131a2006-11-24 17:05:41 -0800976 t = ip6ip6_tnl_locate(&p, 0);
977 }
978 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -0800979 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 memcpy(&p, &t->parms, sizeof (p));
981 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
982 err = -EFAULT;
983 }
984 break;
985 case SIOCADDTUNNEL:
986 case SIOCCHGTUNNEL:
987 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (!capable(CAP_NET_ADMIN))
989 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -0800990 err = -EFAULT;
991 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -0800993 err = -EINVAL;
994 if (p.proto != IPPROTO_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -0800996 t = ip6ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
997 if (dev != ip6ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
998 if (t != NULL) {
999 if (t->dev != dev) {
1000 err = -EEXIST;
1001 break;
1002 }
1003 } else
1004 t = netdev_priv(dev);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 ip6ip6_tnl_unlink(t);
1007 err = ip6ip6_tnl_change(t, &p);
1008 ip6ip6_tnl_link(t);
1009 netdev_state_change(dev);
1010 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001011 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 err = 0;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001013 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1014 err = -EFAULT;
1015
1016 } else
1017 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
1019 case SIOCDELTUNNEL:
1020 err = -EPERM;
1021 if (!capable(CAP_NET_ADMIN))
1022 break;
1023
1024 if (dev == ip6ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001025 err = -EFAULT;
1026 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001028 err = -ENOENT;
1029 if ((t = ip6ip6_tnl_locate(&p, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001031 err = -EPERM;
1032 if (t->dev == ip6ip6_fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001034 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001036 err = 0;
1037 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 break;
1039 default:
1040 err = -EINVAL;
1041 }
1042 return err;
1043}
1044
1045/**
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001046 * ip6ip6_tnl_get_stats - return the stats for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * @dev: virtual device associated with tunnel
1048 *
1049 * Return: stats for device
1050 **/
1051
1052static struct net_device_stats *
1053ip6ip6_tnl_get_stats(struct net_device *dev)
1054{
Patrick McHardy2941a482006-01-08 22:05:26 -08001055 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058/**
1059 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1060 * @dev: virtual device associated with tunnel
1061 * @new_mtu: the new mtu
1062 *
1063 * Return:
1064 * 0 on success,
1065 * %-EINVAL if mtu too small
1066 **/
1067
1068static int
1069ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1070{
1071 if (new_mtu < IPV6_MIN_MTU) {
1072 return -EINVAL;
1073 }
1074 dev->mtu = new_mtu;
1075 return 0;
1076}
1077
1078/**
1079 * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1080 * @dev: virtual device associated with tunnel
1081 *
1082 * Description:
1083 * Initialize function pointers and device parameters
1084 **/
1085
1086static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1087{
1088 SET_MODULE_OWNER(dev);
1089 dev->uninit = ip6ip6_tnl_dev_uninit;
1090 dev->destructor = free_netdev;
1091 dev->hard_start_xmit = ip6ip6_tnl_xmit;
1092 dev->get_stats = ip6ip6_tnl_get_stats;
1093 dev->do_ioctl = ip6ip6_tnl_ioctl;
1094 dev->change_mtu = ip6ip6_tnl_change_mtu;
1095
1096 dev->type = ARPHRD_TUNNEL6;
1097 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1098 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1099 dev->flags |= IFF_NOARP;
1100 dev->addr_len = sizeof(struct in6_addr);
1101}
1102
1103
1104/**
1105 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1106 * @dev: virtual device associated with tunnel
1107 **/
1108
1109static inline void
1110ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1111{
Patrick McHardy2941a482006-01-08 22:05:26 -08001112 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 t->fl.proto = IPPROTO_IPV6;
1114 t->dev = dev;
1115 strcpy(t->parms.name, dev->name);
1116}
1117
1118/**
1119 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1120 * @dev: virtual device associated with tunnel
1121 **/
1122
1123static int
1124ip6ip6_tnl_dev_init(struct net_device *dev)
1125{
Patrick McHardy2941a482006-01-08 22:05:26 -08001126 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ip6ip6_tnl_dev_init_gen(dev);
1128 ip6ip6_tnl_link_config(t);
1129 return 0;
1130}
1131
1132/**
1133 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1134 * @dev: fallback device
1135 *
1136 * Return: 0
1137 **/
1138
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001139static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1141{
Patrick McHardy2941a482006-01-08 22:05:26 -08001142 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ip6ip6_tnl_dev_init_gen(dev);
1144 dev_hold(dev);
1145 tnls_wc[0] = t;
1146 return 0;
1147}
1148
1149static struct xfrm6_tunnel ip6ip6_handler = {
Patrick McHardy03037702005-07-19 14:03:34 -07001150 .handler = ip6ip6_rcv,
1151 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001152 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153};
1154
1155/**
1156 * ip6_tunnel_init - register protocol and reserve needed resources
1157 *
1158 * Return: 0 on success
1159 **/
1160
1161static int __init ip6_tunnel_init(void)
1162{
1163 int err;
1164
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001165 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
1167 return -EAGAIN;
1168 }
1169 ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1170 ip6ip6_tnl_dev_setup);
1171
1172 if (!ip6ip6_fb_tnl_dev) {
1173 err = -ENOMEM;
1174 goto fail;
1175 }
1176 ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1177
1178 if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1179 free_netdev(ip6ip6_fb_tnl_dev);
1180 goto fail;
1181 }
1182 return 0;
1183fail:
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001184 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return err;
1186}
1187
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001188static void __exit ip6ip6_destroy_tunnels(void)
1189{
1190 int h;
1191 struct ip6_tnl *t;
1192
1193 for (h = 0; h < HASH_SIZE; h++) {
1194 while ((t = tnls_r_l[h]) != NULL)
1195 unregister_netdevice(t->dev);
1196 }
1197
1198 t = tnls_wc[0];
1199 unregister_netdevice(t->dev);
1200}
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202/**
1203 * ip6_tunnel_cleanup - free resources and unregister protocol
1204 **/
1205
1206static void __exit ip6_tunnel_cleanup(void)
1207{
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001208 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
1210
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001211 rtnl_lock();
1212 ip6ip6_destroy_tunnels();
1213 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216module_init(ip6_tunnel_init);
1217module_exit(ip6_tunnel_cleanup);