Merge git://git.kernel.org/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[linux-2.6-block.git] / net / ipv6 / ip6_tunnel.c
CommitLineData
1da177e4 1/*
c4d3efaf 2 * IPv6 tunneling device
1da177e4
LT
3 * Linux INET6 implementation
4 *
5 * Authors:
1ab1457c 6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
c4d3efaf 7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
1da177e4
LT
8 *
9 * $Id$
10 *
11 * Based on:
c4d3efaf 12 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
1da177e4
LT
13 *
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
1da177e4 23#include <linux/module.h>
4fc268d2 24#include <linux/capability.h>
1da177e4
LT
25#include <linux/errno.h>
26#include <linux/types.h>
27#include <linux/sockios.h>
c4d3efaf 28#include <linux/icmp.h>
1da177e4
LT
29#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
c4d3efaf 46#include <net/icmp.h>
1da177e4
LT
47#include <net/ip.h>
48#include <net/ipv6.h>
1da177e4
LT
49#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>
55
56MODULE_AUTHOR("Ville Nuorvala");
c4d3efaf 57MODULE_DESCRIPTION("IPv6 tunneling device");
1da177e4
LT
58MODULE_LICENSE("GPL");
59
60#define IPV6_TLV_TEL_DST_SIZE 8
61
62#ifdef IP6_TNL_DEBUG
63#define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
64#else
65#define IP6_TNL_TRACE(x...) do {;} while(0)
66#endif
67
68#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
c4d3efaf 69#define IPV6_TCLASS_SHIFT 20
1da177e4
LT
70
71#define HASH_SIZE 32
72
e69a4adc 73#define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
1ab1457c
YH
74 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
75 (HASH_SIZE - 1))
1da177e4 76
3144581c
YK
77static int ip6_fb_tnl_dev_init(struct net_device *dev);
78static int ip6_tnl_dev_init(struct net_device *dev);
79static void ip6_tnl_dev_setup(struct net_device *dev);
1da177e4
LT
80
81/* the IPv6 tunnel fallback device */
3144581c 82static struct net_device *ip6_fb_tnl_dev;
1da177e4
LT
83
84
85/* lists for storing tunnels in use */
86static struct ip6_tnl *tnls_r_l[HASH_SIZE];
87static struct ip6_tnl *tnls_wc[1];
88static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
89
90/* lock for the tunnel lists */
3144581c 91static DEFINE_RWLOCK(ip6_tnl_lock);
1da177e4
LT
92
93static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
94{
95 struct dst_entry *dst = t->dst_cache;
96
1ab1457c 97 if (dst && dst->obsolete &&
1da177e4
LT
98 dst->ops->check(dst, t->dst_cookie) == NULL) {
99 t->dst_cache = NULL;
100 dst_release(dst);
101 return NULL;
102 }
103
104 return dst;
105}
106
107static inline void ip6_tnl_dst_reset(struct ip6_tnl *t)
108{
109 dst_release(t->dst_cache);
110 t->dst_cache = NULL;
111}
112
113static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
114{
115 struct rt6_info *rt = (struct rt6_info *) dst;
116 t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0;
117 dst_release(t->dst_cache);
118 t->dst_cache = dst;
119}
120
121/**
3144581c 122 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
1ab1457c
YH
123 * @remote: the address of the tunnel exit-point
124 * @local: the address of the tunnel entry-point
1da177e4 125 *
1ab1457c 126 * Return:
1da177e4 127 * tunnel matching given end-points if found,
1ab1457c 128 * else fallback tunnel if its device is up,
1da177e4
LT
129 * else %NULL
130 **/
131
132static struct ip6_tnl *
3144581c 133ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
1da177e4
LT
134{
135 unsigned h0 = HASH(remote);
136 unsigned h1 = HASH(local);
137 struct ip6_tnl *t;
138
139 for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) {
140 if (ipv6_addr_equal(local, &t->parms.laddr) &&
141 ipv6_addr_equal(remote, &t->parms.raddr) &&
142 (t->dev->flags & IFF_UP))
143 return t;
144 }
145 if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP))
146 return t;
147
148 return NULL;
149}
150
151/**
3144581c 152 * ip6_tnl_bucket - get head of list matching given tunnel parameters
1ab1457c 153 * @p: parameters containing tunnel end-points
1da177e4
LT
154 *
155 * Description:
3144581c 156 * ip6_tnl_bucket() returns the head of the list matching the
1da177e4
LT
157 * &struct in6_addr entries laddr and raddr in @p.
158 *
1ab1457c 159 * Return: head of IPv6 tunnel list
1da177e4
LT
160 **/
161
162static struct ip6_tnl **
3144581c 163ip6_tnl_bucket(struct ip6_tnl_parm *p)
1da177e4
LT
164{
165 struct in6_addr *remote = &p->raddr;
166 struct in6_addr *local = &p->laddr;
167 unsigned h = 0;
168 int prio = 0;
169
170 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
171 prio = 1;
172 h = HASH(remote) ^ HASH(local);
173 }
174 return &tnls[prio][h];
175}
176
177/**
3144581c 178 * ip6_tnl_link - add tunnel to hash table
1da177e4
LT
179 * @t: tunnel to be added
180 **/
181
182static void
3144581c 183ip6_tnl_link(struct ip6_tnl *t)
1da177e4 184{
3144581c 185 struct ip6_tnl **tp = ip6_tnl_bucket(&t->parms);
1da177e4
LT
186
187 t->next = *tp;
3144581c 188 write_lock_bh(&ip6_tnl_lock);
1da177e4 189 *tp = t;
3144581c 190 write_unlock_bh(&ip6_tnl_lock);
1da177e4
LT
191}
192
193/**
3144581c 194 * ip6_tnl_unlink - remove tunnel from hash table
1da177e4
LT
195 * @t: tunnel to be removed
196 **/
197
198static void
3144581c 199ip6_tnl_unlink(struct ip6_tnl *t)
1da177e4
LT
200{
201 struct ip6_tnl **tp;
202
3144581c 203 for (tp = ip6_tnl_bucket(&t->parms); *tp; tp = &(*tp)->next) {
1da177e4 204 if (t == *tp) {
3144581c 205 write_lock_bh(&ip6_tnl_lock);
1da177e4 206 *tp = t->next;
3144581c 207 write_unlock_bh(&ip6_tnl_lock);
1da177e4
LT
208 break;
209 }
210 }
211}
212
213/**
214 * ip6_tnl_create() - create a new tunnel
215 * @p: tunnel parameters
216 * @pt: pointer to new tunnel
217 *
218 * Description:
219 * Create tunnel matching given parameters.
1ab1457c
YH
220 *
221 * Return:
567131a7 222 * created tunnel or NULL
1da177e4
LT
223 **/
224
567131a7 225static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
1da177e4
LT
226{
227 struct net_device *dev;
228 struct ip6_tnl *t;
229 char name[IFNAMSIZ];
230 int err;
231
232 if (p->name[0]) {
233 strlcpy(name, p->name, IFNAMSIZ);
234 } else {
235 int i;
236 for (i = 1; i < IP6_TNL_MAX; i++) {
237 sprintf(name, "ip6tnl%d", i);
881d966b 238 if (__dev_get_by_name(&init_net, name) == NULL)
1da177e4
LT
239 break;
240 }
1ab1457c 241 if (i == IP6_TNL_MAX)
567131a7 242 goto failed;
1da177e4 243 }
3144581c 244 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
1da177e4 245 if (dev == NULL)
567131a7 246 goto failed;
1da177e4 247
2941a486 248 t = netdev_priv(dev);
3144581c 249 dev->init = ip6_tnl_dev_init;
1da177e4
LT
250 t->parms = *p;
251
252 if ((err = register_netdevice(dev)) < 0) {
253 free_netdev(dev);
567131a7 254 goto failed;
1da177e4
LT
255 }
256 dev_hold(dev);
3144581c 257 ip6_tnl_link(t);
567131a7
VN
258 return t;
259failed:
260 return NULL;
1da177e4
LT
261}
262
263/**
3144581c 264 * ip6_tnl_locate - find or create tunnel matching given parameters
1ab1457c 265 * @p: tunnel parameters
1da177e4
LT
266 * @create: != 0 if allowed to create new tunnel if no match found
267 *
268 * Description:
3144581c 269 * ip6_tnl_locate() first tries to locate an existing tunnel
1da177e4
LT
270 * based on @parms. If this is unsuccessful, but @create is set a new
271 * tunnel device is created and registered for use.
272 *
273 * Return:
567131a7 274 * matching tunnel or NULL
1da177e4
LT
275 **/
276
3144581c 277static struct ip6_tnl *ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
1da177e4
LT
278{
279 struct in6_addr *remote = &p->raddr;
280 struct in6_addr *local = &p->laddr;
281 struct ip6_tnl *t;
282
3144581c 283 for (t = *ip6_tnl_bucket(p); t; t = t->next) {
1da177e4 284 if (ipv6_addr_equal(local, &t->parms.laddr) &&
567131a7
VN
285 ipv6_addr_equal(remote, &t->parms.raddr))
286 return t;
1da177e4
LT
287 }
288 if (!create)
567131a7
VN
289 return NULL;
290 return ip6_tnl_create(p);
1da177e4
LT
291}
292
293/**
3144581c 294 * ip6_tnl_dev_uninit - tunnel device uninitializer
1da177e4 295 * @dev: the device to be destroyed
1ab1457c 296 *
1da177e4 297 * Description:
3144581c 298 * ip6_tnl_dev_uninit() removes tunnel from its list
1da177e4
LT
299 **/
300
301static void
3144581c 302ip6_tnl_dev_uninit(struct net_device *dev)
1da177e4 303{
2941a486 304 struct ip6_tnl *t = netdev_priv(dev);
1da177e4 305
3144581c
YK
306 if (dev == ip6_fb_tnl_dev) {
307 write_lock_bh(&ip6_tnl_lock);
1da177e4 308 tnls_wc[0] = NULL;
3144581c 309 write_unlock_bh(&ip6_tnl_lock);
1da177e4 310 } else {
3144581c 311 ip6_tnl_unlink(t);
1da177e4
LT
312 }
313 ip6_tnl_dst_reset(t);
314 dev_put(dev);
315}
316
317/**
318 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
319 * @skb: received socket buffer
320 *
1ab1457c
YH
321 * Return:
322 * 0 if none was found,
1da177e4
LT
323 * else index to encapsulation limit
324 **/
325
326static __u16
327parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
328{
329 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
330 __u8 nexthdr = ipv6h->nexthdr;
331 __u16 off = sizeof (*ipv6h);
332
333 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
334 __u16 optlen = 0;
335 struct ipv6_opt_hdr *hdr;
336 if (raw + off + sizeof (*hdr) > skb->data &&
337 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
338 break;
339
340 hdr = (struct ipv6_opt_hdr *) (raw + off);
341 if (nexthdr == NEXTHDR_FRAGMENT) {
342 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
343 if (frag_hdr->frag_off)
344 break;
345 optlen = 8;
346 } else if (nexthdr == NEXTHDR_AUTH) {
347 optlen = (hdr->hdrlen + 2) << 2;
348 } else {
349 optlen = ipv6_optlen(hdr);
350 }
351 if (nexthdr == NEXTHDR_DEST) {
352 __u16 i = off + 2;
353 while (1) {
354 struct ipv6_tlv_tnl_enc_lim *tel;
355
356 /* No more room for encapsulation limit */
357 if (i + sizeof (*tel) > off + optlen)
358 break;
359
360 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
361 /* return index of option if found and valid */
362 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
363 tel->length == 1)
364 return i;
365 /* else jump to next option */
366 if (tel->type)
367 i += tel->length + 2;
368 else
369 i++;
370 }
371 }
372 nexthdr = hdr->nexthdr;
373 off += optlen;
374 }
375 return 0;
376}
377
378/**
e490d1d8 379 * ip6_tnl_err - tunnel error handler
1da177e4
LT
380 *
381 * Description:
e490d1d8 382 * ip6_tnl_err() should handle errors in the tunnel according
1da177e4
LT
383 * to the specifications in RFC 2473.
384 **/
385
d2acc347 386static int
502b0935 387ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
704eae1f 388 int *type, int *code, int *msg, __u32 *info, int offset)
1da177e4
LT
389{
390 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
391 struct ip6_tnl *t;
392 int rel_msg = 0;
393 int rel_type = ICMPV6_DEST_UNREACH;
394 int rel_code = ICMPV6_ADDR_UNREACH;
395 __u32 rel_info = 0;
396 __u16 len;
d2acc347 397 int err = -ENOENT;
1da177e4 398
1ab1457c
YH
399 /* If the packet doesn't contain the original IPv6 header we are
400 in trouble since we might need the source address for further
1da177e4
LT
401 processing of the error. */
402
3144581c
YK
403 read_lock(&ip6_tnl_lock);
404 if ((t = ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
1da177e4
LT
405 goto out;
406
502b0935
YK
407 if (t->parms.proto != ipproto && t->parms.proto != 0)
408 goto out;
409
d2acc347
HX
410 err = 0;
411
e490d1d8 412 switch (*type) {
1da177e4
LT
413 __u32 teli;
414 struct ipv6_tlv_tnl_enc_lim *tel;
415 __u32 mtu;
416 case ICMPV6_DEST_UNREACH:
417 if (net_ratelimit())
418 printk(KERN_WARNING
419 "%s: Path to destination invalid "
420 "or inactive!\n", t->parms.name);
421 rel_msg = 1;
422 break;
423 case ICMPV6_TIME_EXCEED:
e490d1d8 424 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
1da177e4
LT
425 if (net_ratelimit())
426 printk(KERN_WARNING
427 "%s: Too small hop limit or "
1ab1457c 428 "routing loop in tunnel!\n",
1da177e4
LT
429 t->parms.name);
430 rel_msg = 1;
431 }
432 break;
433 case ICMPV6_PARAMPROB:
107a5fe6 434 teli = 0;
e490d1d8 435 if ((*code) == ICMPV6_HDR_FIELD)
107a5fe6 436 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
1da177e4 437
704eae1f 438 if (teli && teli == *info - 2) {
1da177e4
LT
439 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
440 if (tel->encap_limit == 0) {
441 if (net_ratelimit())
442 printk(KERN_WARNING
443 "%s: Too small encapsulation "
444 "limit or routing loop in "
445 "tunnel!\n", t->parms.name);
446 rel_msg = 1;
447 }
107a5fe6
VN
448 } else if (net_ratelimit()) {
449 printk(KERN_WARNING
450 "%s: Recipient unable to parse tunneled "
451 "packet!\n ", t->parms.name);
1da177e4
LT
452 }
453 break;
454 case ICMPV6_PKT_TOOBIG:
704eae1f 455 mtu = *info - offset;
1da177e4
LT
456 if (mtu < IPV6_MIN_MTU)
457 mtu = IPV6_MIN_MTU;
458 t->dev->mtu = mtu;
459
cc6cdac0 460 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
1da177e4
LT
461 rel_type = ICMPV6_PKT_TOOBIG;
462 rel_code = 0;
463 rel_info = mtu;
464 rel_msg = 1;
465 }
466 break;
467 }
e490d1d8
YK
468
469 *type = rel_type;
470 *code = rel_code;
471 *info = rel_info;
472 *msg = rel_msg;
473
474out:
3144581c 475 read_unlock(&ip6_tnl_lock);
e490d1d8
YK
476 return err;
477}
478
c4d3efaf
YK
479static int
480ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
704eae1f 481 int type, int code, int offset, __be32 info)
c4d3efaf
YK
482{
483 int rel_msg = 0;
484 int rel_type = type;
485 int rel_code = code;
704eae1f 486 __u32 rel_info = ntohl(info);
c4d3efaf
YK
487 int err;
488 struct sk_buff *skb2;
489 struct iphdr *eiph;
490 struct flowi fl;
491 struct rtable *rt;
492
502b0935
YK
493 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
494 &rel_msg, &rel_info, offset);
c4d3efaf
YK
495 if (err < 0)
496 return err;
497
498 if (rel_msg == 0)
499 return 0;
500
501 switch (rel_type) {
502 case ICMPV6_DEST_UNREACH:
503 if (rel_code != ICMPV6_ADDR_UNREACH)
504 return 0;
505 rel_type = ICMP_DEST_UNREACH;
506 rel_code = ICMP_HOST_UNREACH;
507 break;
508 case ICMPV6_PKT_TOOBIG:
509 if (rel_code != 0)
510 return 0;
511 rel_type = ICMP_DEST_UNREACH;
512 rel_code = ICMP_FRAG_NEEDED;
513 break;
514 default:
515 return 0;
516 }
517
518 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
519 return 0;
520
521 skb2 = skb_clone(skb, GFP_ATOMIC);
522 if (!skb2)
523 return 0;
524
525 dst_release(skb2->dst);
526 skb2->dst = NULL;
527 skb_pull(skb2, offset);
c1d2bbe1 528 skb_reset_network_header(skb2);
eddc9ec5 529 eiph = ip_hdr(skb2);
c4d3efaf
YK
530
531 /* Try to guess incoming interface */
532 memset(&fl, 0, sizeof(fl));
533 fl.fl4_dst = eiph->saddr;
534 fl.fl4_tos = RT_TOS(eiph->tos);
535 fl.proto = IPPROTO_IPIP;
f206351a 536 if (ip_route_output_key(&init_net, &rt, &fl))
c4d3efaf
YK
537 goto out;
538
539 skb2->dev = rt->u.dst.dev;
540
541 /* route "incoming" packet */
542 if (rt->rt_flags & RTCF_LOCAL) {
543 ip_rt_put(rt);
544 rt = NULL;
545 fl.fl4_dst = eiph->daddr;
546 fl.fl4_src = eiph->saddr;
547 fl.fl4_tos = eiph->tos;
f206351a 548 if (ip_route_output_key(&init_net, &rt, &fl) ||
c4d3efaf
YK
549 rt->u.dst.dev->type != ARPHRD_TUNNEL) {
550 ip_rt_put(rt);
551 goto out;
552 }
553 } else {
554 ip_rt_put(rt);
555 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
556 skb2->dev) ||
557 skb2->dst->dev->type != ARPHRD_TUNNEL)
558 goto out;
559 }
560
561 /* change mtu on this route */
562 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
563 if (rel_info > dst_mtu(skb2->dst))
564 goto out;
565
566 skb2->dst->ops->update_pmtu(skb2->dst, rel_info);
c4d3efaf
YK
567 }
568
704eae1f 569 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
c4d3efaf
YK
570
571out:
572 kfree_skb(skb2);
573 return 0;
574}
575
e490d1d8
YK
576static int
577ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
704eae1f 578 int type, int code, int offset, __be32 info)
e490d1d8
YK
579{
580 int rel_msg = 0;
581 int rel_type = type;
582 int rel_code = code;
704eae1f 583 __u32 rel_info = ntohl(info);
e490d1d8
YK
584 int err;
585
502b0935
YK
586 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
587 &rel_msg, &rel_info, offset);
e490d1d8
YK
588 if (err < 0)
589 return err;
590
591 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
1da177e4
LT
592 struct rt6_info *rt;
593 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
305d4b3c 594
1da177e4 595 if (!skb2)
e490d1d8 596 return 0;
1da177e4
LT
597
598 dst_release(skb2->dst);
599 skb2->dst = NULL;
600 skb_pull(skb2, offset);
c1d2bbe1 601 skb_reset_network_header(skb2);
1da177e4
LT
602
603 /* Try to guess incoming interface */
0660e03f 604 rt = rt6_lookup(&ipv6_hdr(skb2)->saddr, NULL, 0, 0);
1da177e4
LT
605
606 if (rt && rt->rt6i_dev)
607 skb2->dev = rt->rt6i_dev;
608
609 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
610
611 if (rt)
612 dst_release(&rt->u.dst);
613
614 kfree_skb(skb2);
615 }
e490d1d8
YK
616
617 return 0;
1da177e4
LT
618}
619
c4d3efaf
YK
620static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
621 struct ipv6hdr *ipv6h,
622 struct sk_buff *skb)
623{
624 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
625
626 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
eddc9ec5 627 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
c4d3efaf
YK
628
629 if (INET_ECN_is_ce(dsfield))
eddc9ec5 630 IP_ECN_set_ce(ip_hdr(skb));
c4d3efaf
YK
631}
632
8359925b
YK
633static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
634 struct ipv6hdr *ipv6h,
635 struct sk_buff *skb)
1da177e4 636{
8359925b 637 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
29bb43b4 638 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
1da177e4 639
8359925b 640 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
0660e03f 641 IP6_ECN_set_ce(ipv6_hdr(skb));
1da177e4 642}
8359925b 643
09c6bbf0
VN
644static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
645{
646 struct ip6_tnl_parm *p = &t->parms;
647 int ret = 0;
648
649 if (p->flags & IP6_TNL_F_CAP_RCV) {
1ab1457c 650 struct net_device *ldev = NULL;
09c6bbf0
VN
651
652 if (p->link)
881d966b 653 ldev = dev_get_by_index(&init_net, p->link);
09c6bbf0
VN
654
655 if ((ipv6_addr_is_multicast(&p->laddr) ||
bfeade08
DL
656 likely(ipv6_chk_addr(&init_net, &p->laddr, ldev, 0))) &&
657 likely(!ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
09c6bbf0
VN
658 ret = 1;
659
660 if (ldev)
661 dev_put(ldev);
662 }
663 return ret;
664}
1da177e4
LT
665
666/**
3144581c 667 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
1da177e4 668 * @skb: received socket buffer
8359925b
YK
669 * @protocol: ethernet protocol ID
670 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
1da177e4
LT
671 *
672 * Return: 0
673 **/
674
8359925b 675static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
502b0935 676 __u8 ipproto,
8359925b
YK
677 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
678 struct ipv6hdr *ipv6h,
679 struct sk_buff *skb))
1da177e4 680{
1da177e4 681 struct ip6_tnl *t;
0660e03f 682 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1da177e4 683
3144581c 684 read_lock(&ip6_tnl_lock);
1da177e4 685
3144581c 686 if ((t = ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
502b0935
YK
687 if (t->parms.proto != ipproto && t->parms.proto != 0) {
688 read_unlock(&ip6_tnl_lock);
689 goto discard;
690 }
691
1da177e4 692 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
3144581c 693 read_unlock(&ip6_tnl_lock);
50fba2aa 694 goto discard;
1da177e4
LT
695 }
696
09c6bbf0 697 if (!ip6_tnl_rcv_ctl(t)) {
1da177e4 698 t->stat.rx_dropped++;
3144581c 699 read_unlock(&ip6_tnl_lock);
1da177e4
LT
700 goto discard;
701 }
702 secpath_reset(skb);
b0e380b1 703 skb->mac_header = skb->network_header;
c1d2bbe1 704 skb_reset_network_header(skb);
8359925b 705 skb->protocol = htons(protocol);
1da177e4
LT
706 skb->pkt_type = PACKET_HOST;
707 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
708 skb->dev = t->dev;
709 dst_release(skb->dst);
710 skb->dst = NULL;
53ab61c6 711 nf_reset(skb);
8359925b
YK
712
713 dscp_ecn_decapsulate(t, ipv6h, skb);
714
1da177e4
LT
715 t->stat.rx_packets++;
716 t->stat.rx_bytes += skb->len;
717 netif_rx(skb);
3144581c 718 read_unlock(&ip6_tnl_lock);
1da177e4
LT
719 return 0;
720 }
3144581c 721 read_unlock(&ip6_tnl_lock);
1da177e4 722 return 1;
50fba2aa
HX
723
724discard:
725 kfree_skb(skb);
726 return 0;
1da177e4
LT
727}
728
c4d3efaf
YK
729static int ip4ip6_rcv(struct sk_buff *skb)
730{
502b0935
YK
731 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
732 ip4ip6_dscp_ecn_decapsulate);
c4d3efaf
YK
733}
734
8359925b
YK
735static int ip6ip6_rcv(struct sk_buff *skb)
736{
502b0935
YK
737 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
738 ip6ip6_dscp_ecn_decapsulate);
8359925b
YK
739}
740
6fb32dde
VN
741struct ipv6_tel_txoption {
742 struct ipv6_txoptions ops;
743 __u8 dst_opt[8];
744};
1da177e4 745
6fb32dde
VN
746static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
747{
748 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
1da177e4 749
6fb32dde
VN
750 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
751 opt->dst_opt[3] = 1;
752 opt->dst_opt[4] = encap_limit;
753 opt->dst_opt[5] = IPV6_TLV_PADN;
754 opt->dst_opt[6] = 1;
1da177e4 755
6fb32dde
VN
756 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
757 opt->ops.opt_nflen = 8;
1da177e4
LT
758}
759
760/**
3144581c 761 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
1da177e4 762 * @t: the outgoing tunnel device
1ab1457c 763 * @hdr: IPv6 header from the incoming packet
1da177e4
LT
764 *
765 * Description:
1ab1457c 766 * Avoid trivial tunneling loop by checking that tunnel exit-point
1da177e4
LT
767 * doesn't match source of incoming packet.
768 *
1ab1457c 769 * Return:
1da177e4
LT
770 * 1 if conflict,
771 * 0 else
772 **/
773
774static inline int
3144581c 775ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
1da177e4
LT
776{
777 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
778}
779
09c6bbf0
VN
780static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
781{
782 struct ip6_tnl_parm *p = &t->parms;
783 int ret = 0;
784
1ab1457c 785 if (p->flags & IP6_TNL_F_CAP_XMIT) {
09c6bbf0
VN
786 struct net_device *ldev = NULL;
787
788 if (p->link)
881d966b 789 ldev = dev_get_by_index(&init_net, p->link);
09c6bbf0 790
bfeade08 791 if (unlikely(!ipv6_chk_addr(&init_net, &p->laddr, ldev, 0)))
09c6bbf0
VN
792 printk(KERN_WARNING
793 "%s xmit: Local address not yet configured!\n",
794 p->name);
795 else if (!ipv6_addr_is_multicast(&p->raddr) &&
bfeade08 796 unlikely(ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
09c6bbf0
VN
797 printk(KERN_WARNING
798 "%s xmit: Routing loop! "
799 "Remote address found on this node!\n",
800 p->name);
801 else
802 ret = 1;
803 if (ldev)
804 dev_put(ldev);
805 }
806 return ret;
807}
1da177e4 808/**
61ec2aec 809 * ip6_tnl_xmit2 - encapsulate packet and send
1da177e4 810 * @skb: the outgoing socket buffer
1ab1457c 811 * @dev: the outgoing tunnel device
61ec2aec
YK
812 * @dsfield: dscp code for outer header
813 * @fl: flow of tunneled packet
814 * @encap_limit: encapsulation limit
815 * @pmtu: Path MTU is stored if packet is too big
1da177e4
LT
816 *
817 * Description:
818 * Build new header and do some sanity checks on the packet before sending
819 * it.
820 *
1ab1457c 821 * Return:
c4d3efaf 822 * 0 on success
61ec2aec
YK
823 * -1 fail
824 * %-EMSGSIZE message too big. return mtu in this case.
1da177e4
LT
825 **/
826
61ec2aec
YK
827static int ip6_tnl_xmit2(struct sk_buff *skb,
828 struct net_device *dev,
829 __u8 dsfield,
830 struct flowi *fl,
831 int encap_limit,
832 __u32 *pmtu)
1da177e4 833{
2941a486 834 struct ip6_tnl *t = netdev_priv(dev);
1da177e4 835 struct net_device_stats *stats = &t->stat;
0660e03f 836 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
6fb32dde 837 struct ipv6_tel_txoption opt;
1da177e4
LT
838 struct dst_entry *dst;
839 struct net_device *tdev;
840 int mtu;
c2636b4d 841 unsigned int max_headroom = sizeof(struct ipv6hdr);
1da177e4 842 u8 proto;
61ec2aec 843 int err = -1;
1da177e4 844 int pkt_len;
1da177e4 845
1da177e4
LT
846 if ((dst = ip6_tnl_dst_check(t)) != NULL)
847 dst_hold(dst);
a57ebc90 848 else {
61ec2aec 849 dst = ip6_route_output(NULL, fl);
1da177e4 850
61ec2aec 851 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
a57ebc90
PM
852 goto tx_err_link_failure;
853 }
1da177e4
LT
854
855 tdev = dst->dev;
856
857 if (tdev == dev) {
858 stats->collisions++;
859 if (net_ratelimit())
1ab1457c 860 printk(KERN_WARNING
1da177e4
LT
861 "%s: Local routing loop detected!\n",
862 t->parms.name);
863 goto tx_err_dst_release;
864 }
865 mtu = dst_mtu(dst) - sizeof (*ipv6h);
6fb32dde 866 if (encap_limit >= 0) {
1da177e4
LT
867 max_headroom += 8;
868 mtu -= 8;
869 }
870 if (mtu < IPV6_MIN_MTU)
871 mtu = IPV6_MIN_MTU;
26892058
YK
872 if (skb->dst)
873 skb->dst->ops->update_pmtu(skb->dst, mtu);
1da177e4 874 if (skb->len > mtu) {
61ec2aec
YK
875 *pmtu = mtu;
876 err = -EMSGSIZE;
1da177e4
LT
877 goto tx_err_dst_release;
878 }
879
880 /*
881 * Okay, now see if we can stuff it in the buffer as-is.
882 */
883 max_headroom += LL_RESERVED_SPACE(tdev);
1ab1457c 884
cfbba49d
PM
885 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
886 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
1da177e4 887 struct sk_buff *new_skb;
1ab1457c 888
1da177e4
LT
889 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
890 goto tx_err_dst_release;
891
892 if (skb->sk)
893 skb_set_owner_w(new_skb, skb->sk);
894 kfree_skb(skb);
895 skb = new_skb;
896 }
897 dst_release(skb->dst);
898 skb->dst = dst_clone(dst);
899
b0e380b1 900 skb->transport_header = skb->network_header;
1da177e4 901
61ec2aec 902 proto = fl->proto;
6fb32dde
VN
903 if (encap_limit >= 0) {
904 init_tel_txopt(&opt, encap_limit);
905 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
906 }
e2d1bca7
ACM
907 skb_push(skb, sizeof(struct ipv6hdr));
908 skb_reset_network_header(skb);
0660e03f 909 ipv6h = ipv6_hdr(skb);
61ec2aec 910 *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
1da177e4
LT
911 dsfield = INET_ECN_encapsulate(0, dsfield);
912 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
1da177e4
LT
913 ipv6h->hop_limit = t->parms.hop_limit;
914 ipv6h->nexthdr = proto;
61ec2aec
YK
915 ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
916 ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
1da177e4
LT
917 nf_reset(skb);
918 pkt_len = skb->len;
ef76bc23 919 err = ip6_local_out(skb);
1da177e4 920
b9df3cb8 921 if (net_xmit_eval(err) == 0) {
1da177e4
LT
922 stats->tx_bytes += pkt_len;
923 stats->tx_packets++;
924 } else {
925 stats->tx_errors++;
926 stats->tx_aborted_errors++;
927 }
928 ip6_tnl_dst_store(t, dst);
1da177e4
LT
929 return 0;
930tx_err_link_failure:
931 stats->tx_carrier_errors++;
932 dst_link_failure(skb);
933tx_err_dst_release:
934 dst_release(dst);
61ec2aec
YK
935 return err;
936}
937
c4d3efaf
YK
938static inline int
939ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
940{
941 struct ip6_tnl *t = netdev_priv(dev);
eddc9ec5 942 struct iphdr *iph = ip_hdr(skb);
c4d3efaf
YK
943 int encap_limit = -1;
944 struct flowi fl;
945 __u8 dsfield;
946 __u32 mtu;
947 int err;
948
502b0935
YK
949 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
950 !ip6_tnl_xmit_ctl(t))
c4d3efaf
YK
951 return -1;
952
953 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
954 encap_limit = t->parms.encap_limit;
955
956 memcpy(&fl, &t->fl, sizeof (fl));
957 fl.proto = IPPROTO_IPIP;
958
959 dsfield = ipv4_get_dsfield(iph);
960
961 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
b77f2fa6
AV
962 fl.fl6_flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
963 & IPV6_TCLASS_MASK;
c4d3efaf
YK
964
965 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
966 if (err != 0) {
967 /* XXX: send ICMP error even if DF is not set. */
968 if (err == -EMSGSIZE)
969 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
970 htonl(mtu));
971 return -1;
972 }
973
974 return 0;
975}
976
61ec2aec
YK
977static inline int
978ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
979{
980 struct ip6_tnl *t = netdev_priv(dev);
0660e03f 981 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
61ec2aec
YK
982 int encap_limit = -1;
983 __u16 offset;
984 struct flowi fl;
985 __u8 dsfield;
986 __u32 mtu;
987 int err;
988
502b0935
YK
989 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
990 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
61ec2aec
YK
991 return -1;
992
d56f90a7
ACM
993 offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
994 if (offset > 0) {
61ec2aec 995 struct ipv6_tlv_tnl_enc_lim *tel;
d56f90a7 996 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
61ec2aec
YK
997 if (tel->encap_limit == 0) {
998 icmpv6_send(skb, ICMPV6_PARAMPROB,
999 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
1000 return -1;
1001 }
1002 encap_limit = tel->encap_limit - 1;
1003 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1004 encap_limit = t->parms.encap_limit;
1005
1006 memcpy(&fl, &t->fl, sizeof (fl));
1007 fl.proto = IPPROTO_IPV6;
1008
1009 dsfield = ipv6_get_dsfield(ipv6h);
1010 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
1011 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1012 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
1013 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1014
1015 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1016 if (err != 0) {
1017 if (err == -EMSGSIZE)
1018 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1019 return -1;
1020 }
1021
1022 return 0;
1023}
1024
1025static int
1026ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1027{
1028 struct ip6_tnl *t = netdev_priv(dev);
1029 struct net_device_stats *stats = &t->stat;
1030 int ret;
1031
1032 if (t->recursion++) {
1033 t->stat.collisions++;
1034 goto tx_err;
1035 }
1036
1037 switch (skb->protocol) {
c4d3efaf
YK
1038 case __constant_htons(ETH_P_IP):
1039 ret = ip4ip6_tnl_xmit(skb, dev);
1040 break;
61ec2aec
YK
1041 case __constant_htons(ETH_P_IPV6):
1042 ret = ip6ip6_tnl_xmit(skb, dev);
1043 break;
1044 default:
1045 goto tx_err;
1046 }
1047
1048 if (ret < 0)
1049 goto tx_err;
1050
1051 t->recursion--;
1052 return 0;
1053
1da177e4
LT
1054tx_err:
1055 stats->tx_errors++;
1056 stats->tx_dropped++;
1057 kfree_skb(skb);
1058 t->recursion--;
1059 return 0;
1060}
1061
1062static void ip6_tnl_set_cap(struct ip6_tnl *t)
1063{
1064 struct ip6_tnl_parm *p = &t->parms;
09c6bbf0
VN
1065 int ltype = ipv6_addr_type(&p->laddr);
1066 int rtype = ipv6_addr_type(&p->raddr);
1da177e4
LT
1067
1068 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
1069
09c6bbf0
VN
1070 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1071 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1072 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
305d4b3c 1073 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
09c6bbf0
VN
1074 if (ltype&IPV6_ADDR_UNICAST)
1075 p->flags |= IP6_TNL_F_CAP_XMIT;
1076 if (rtype&IPV6_ADDR_UNICAST)
1077 p->flags |= IP6_TNL_F_CAP_RCV;
1da177e4
LT
1078 }
1079}
1080
3144581c 1081static void ip6_tnl_link_config(struct ip6_tnl *t)
1da177e4
LT
1082{
1083 struct net_device *dev = t->dev;
1084 struct ip6_tnl_parm *p = &t->parms;
1085 struct flowi *fl = &t->fl;
1086
1087 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1088 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1089
1090 /* Set up flowi template */
1091 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
1092 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
1093 fl->oif = p->link;
1094 fl->fl6_flowlabel = 0;
1095
1096 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1097 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1098 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1099 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1100
1101 ip6_tnl_set_cap(t);
1102
1103 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1104 dev->flags |= IFF_POINTOPOINT;
1105 else
1106 dev->flags &= ~IFF_POINTOPOINT;
1107
1108 dev->iflink = p->link;
1109
1110 if (p->flags & IP6_TNL_F_CAP_XMIT) {
305d4b3c
VN
1111 int strict = (ipv6_addr_type(&p->raddr) &
1112 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1113
1da177e4 1114 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
305d4b3c 1115 p->link, strict);
1da177e4
LT
1116
1117 if (rt == NULL)
1118 return;
1119
1120 if (rt->rt6i_dev) {
1121 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
1122 sizeof (struct ipv6hdr);
1123
1124 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
1125
1126 if (dev->mtu < IPV6_MIN_MTU)
1127 dev->mtu = IPV6_MIN_MTU;
1128 }
1129 dst_release(&rt->u.dst);
1130 }
1131}
1132
1133/**
3144581c 1134 * ip6_tnl_change - update the tunnel parameters
1da177e4
LT
1135 * @t: tunnel to be changed
1136 * @p: tunnel configuration parameters
1137 * @active: != 0 if tunnel is ready for use
1138 *
1139 * Description:
3144581c 1140 * ip6_tnl_change() updates the tunnel parameters
1da177e4
LT
1141 **/
1142
1143static int
3144581c 1144ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
1da177e4
LT
1145{
1146 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
1147 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
1148 t->parms.flags = p->flags;
1149 t->parms.hop_limit = p->hop_limit;
1150 t->parms.encap_limit = p->encap_limit;
1151 t->parms.flowinfo = p->flowinfo;
8181b8c1 1152 t->parms.link = p->link;
502b0935 1153 t->parms.proto = p->proto;
0c088890 1154 ip6_tnl_dst_reset(t);
3144581c 1155 ip6_tnl_link_config(t);
1da177e4
LT
1156 return 0;
1157}
1158
1159/**
3144581c 1160 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1da177e4
LT
1161 * @dev: virtual device associated with tunnel
1162 * @ifr: parameters passed from userspace
1163 * @cmd: command to be performed
1164 *
1165 * Description:
3144581c 1166 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1ab1457c 1167 * from userspace.
1da177e4
LT
1168 *
1169 * The possible commands are the following:
1170 * %SIOCGETTUNNEL: get tunnel parameters for device
1171 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1172 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1173 * %SIOCDELTUNNEL: delete tunnel
1174 *
1ab1457c 1175 * The fallback device "ip6tnl0", created during module
1da177e4
LT
1176 * initialization, can be used for creating other tunnel devices.
1177 *
1178 * Return:
1179 * 0 on success,
1180 * %-EFAULT if unable to copy data to or from userspace,
1181 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1182 * %-EINVAL if passed tunnel parameters are invalid,
1183 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1184 * %-ENODEV if attempting to change or delete a nonexisting device
1185 **/
1186
1187static int
3144581c 1188ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1da177e4
LT
1189{
1190 int err = 0;
1da177e4
LT
1191 struct ip6_tnl_parm p;
1192 struct ip6_tnl *t = NULL;
1193
1194 switch (cmd) {
1195 case SIOCGETTUNNEL:
3144581c 1196 if (dev == ip6_fb_tnl_dev) {
567131a7 1197 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
1da177e4
LT
1198 err = -EFAULT;
1199 break;
1200 }
3144581c 1201 t = ip6_tnl_locate(&p, 0);
567131a7
VN
1202 }
1203 if (t == NULL)
2941a486 1204 t = netdev_priv(dev);
1da177e4
LT
1205 memcpy(&p, &t->parms, sizeof (p));
1206 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1207 err = -EFAULT;
1208 }
1209 break;
1210 case SIOCADDTUNNEL:
1211 case SIOCCHGTUNNEL:
1212 err = -EPERM;
1da177e4
LT
1213 if (!capable(CAP_NET_ADMIN))
1214 break;
567131a7
VN
1215 err = -EFAULT;
1216 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1da177e4 1217 break;
567131a7 1218 err = -EINVAL;
502b0935
YK
1219 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1220 p.proto != 0)
1da177e4 1221 break;
3144581c
YK
1222 t = ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
1223 if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
567131a7
VN
1224 if (t != NULL) {
1225 if (t->dev != dev) {
1226 err = -EEXIST;
1227 break;
1228 }
1229 } else
1230 t = netdev_priv(dev);
1231
3144581c
YK
1232 ip6_tnl_unlink(t);
1233 err = ip6_tnl_change(t, &p);
1234 ip6_tnl_link(t);
1da177e4
LT
1235 netdev_state_change(dev);
1236 }
567131a7 1237 if (t) {
1da177e4 1238 err = 0;
567131a7
VN
1239 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1240 err = -EFAULT;
1241
1242 } else
1243 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
1da177e4
LT
1244 break;
1245 case SIOCDELTUNNEL:
1246 err = -EPERM;
1247 if (!capable(CAP_NET_ADMIN))
1248 break;
1249
3144581c 1250 if (dev == ip6_fb_tnl_dev) {
567131a7
VN
1251 err = -EFAULT;
1252 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
1da177e4 1253 break;
567131a7 1254 err = -ENOENT;
3144581c 1255 if ((t = ip6_tnl_locate(&p, 0)) == NULL)
1da177e4 1256 break;
567131a7 1257 err = -EPERM;
3144581c 1258 if (t->dev == ip6_fb_tnl_dev)
1da177e4 1259 break;
567131a7 1260 dev = t->dev;
1da177e4 1261 }
22f8cde5
SH
1262 err = 0;
1263 unregister_netdevice(dev);
1da177e4
LT
1264 break;
1265 default:
1266 err = -EINVAL;
1267 }
1268 return err;
1269}
1270
1271/**
3144581c 1272 * ip6_tnl_get_stats - return the stats for tunnel device
1da177e4
LT
1273 * @dev: virtual device associated with tunnel
1274 *
1275 * Return: stats for device
1276 **/
1277
1278static struct net_device_stats *
3144581c 1279ip6_tnl_get_stats(struct net_device *dev)
1da177e4 1280{
2941a486 1281 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
1da177e4
LT
1282}
1283
1284/**
3144581c 1285 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1da177e4
LT
1286 * @dev: virtual device associated with tunnel
1287 * @new_mtu: the new mtu
1288 *
1289 * Return:
1290 * 0 on success,
1291 * %-EINVAL if mtu too small
1292 **/
1293
1294static int
3144581c 1295ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1da177e4
LT
1296{
1297 if (new_mtu < IPV6_MIN_MTU) {
1298 return -EINVAL;
1299 }
1300 dev->mtu = new_mtu;
1301 return 0;
1302}
1303
1304/**
3144581c 1305 * ip6_tnl_dev_setup - setup virtual tunnel device
1da177e4
LT
1306 * @dev: virtual device associated with tunnel
1307 *
1308 * Description:
1309 * Initialize function pointers and device parameters
1310 **/
1311
3144581c 1312static void ip6_tnl_dev_setup(struct net_device *dev)
1da177e4 1313{
3144581c 1314 dev->uninit = ip6_tnl_dev_uninit;
1da177e4 1315 dev->destructor = free_netdev;
61ec2aec 1316 dev->hard_start_xmit = ip6_tnl_xmit;
3144581c
YK
1317 dev->get_stats = ip6_tnl_get_stats;
1318 dev->do_ioctl = ip6_tnl_ioctl;
1319 dev->change_mtu = ip6_tnl_change_mtu;
1da177e4
LT
1320
1321 dev->type = ARPHRD_TUNNEL6;
1322 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1323 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1324 dev->flags |= IFF_NOARP;
1325 dev->addr_len = sizeof(struct in6_addr);
1326}
1327
1328
1329/**
3144581c 1330 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1da177e4
LT
1331 * @dev: virtual device associated with tunnel
1332 **/
1333
1334static inline void
3144581c 1335ip6_tnl_dev_init_gen(struct net_device *dev)
1da177e4 1336{
2941a486 1337 struct ip6_tnl *t = netdev_priv(dev);
1da177e4
LT
1338 t->dev = dev;
1339 strcpy(t->parms.name, dev->name);
1340}
1341
1342/**
3144581c 1343 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1da177e4
LT
1344 * @dev: virtual device associated with tunnel
1345 **/
1346
1347static int
3144581c 1348ip6_tnl_dev_init(struct net_device *dev)
1da177e4 1349{
2941a486 1350 struct ip6_tnl *t = netdev_priv(dev);
3144581c
YK
1351 ip6_tnl_dev_init_gen(dev);
1352 ip6_tnl_link_config(t);
1da177e4
LT
1353 return 0;
1354}
1355
1356/**
3144581c 1357 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1da177e4
LT
1358 * @dev: fallback device
1359 *
1360 * Return: 0
1361 **/
1362
1ab1457c 1363static int
3144581c 1364ip6_fb_tnl_dev_init(struct net_device *dev)
1da177e4 1365{
2941a486 1366 struct ip6_tnl *t = netdev_priv(dev);
3144581c 1367 ip6_tnl_dev_init_gen(dev);
502b0935 1368 t->parms.proto = IPPROTO_IPV6;
1da177e4
LT
1369 dev_hold(dev);
1370 tnls_wc[0] = t;
1371 return 0;
1372}
1373
c4d3efaf
YK
1374static struct xfrm6_tunnel ip4ip6_handler = {
1375 .handler = ip4ip6_rcv,
1376 .err_handler = ip4ip6_err,
1377 .priority = 1,
1378};
1379
1da177e4 1380static struct xfrm6_tunnel ip6ip6_handler = {
0303770d
PM
1381 .handler = ip6ip6_rcv,
1382 .err_handler = ip6ip6_err,
d2acc347 1383 .priority = 1,
1da177e4
LT
1384};
1385
1386/**
1387 * ip6_tunnel_init - register protocol and reserve needed resources
1388 *
1389 * Return: 0 on success
1390 **/
1391
1392static int __init ip6_tunnel_init(void)
1393{
1394 int err;
1395
c4d3efaf 1396 if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
3144581c 1397 printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
c4d3efaf
YK
1398 err = -EAGAIN;
1399 goto out;
1400 }
1401
73d605d1 1402 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
3144581c 1403 printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
c4d3efaf
YK
1404 err = -EAGAIN;
1405 goto unreg_ip4ip6;
1da177e4 1406 }
3144581c
YK
1407 ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1408 ip6_tnl_dev_setup);
1da177e4 1409
3144581c 1410 if (!ip6_fb_tnl_dev) {
1da177e4
LT
1411 err = -ENOMEM;
1412 goto fail;
1413 }
3144581c 1414 ip6_fb_tnl_dev->init = ip6_fb_tnl_dev_init;
1da177e4 1415
3144581c
YK
1416 if ((err = register_netdev(ip6_fb_tnl_dev))) {
1417 free_netdev(ip6_fb_tnl_dev);
1da177e4
LT
1418 goto fail;
1419 }
1420 return 0;
1421fail:
73d605d1 1422 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
c4d3efaf
YK
1423unreg_ip4ip6:
1424 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1425out:
1da177e4
LT
1426 return err;
1427}
1428
3144581c 1429static void __exit ip6_tnl_destroy_tunnels(void)
b3fdd9f1
YK
1430{
1431 int h;
1432 struct ip6_tnl *t;
1433
1434 for (h = 0; h < HASH_SIZE; h++) {
1435 while ((t = tnls_r_l[h]) != NULL)
1436 unregister_netdevice(t->dev);
1437 }
1438
1439 t = tnls_wc[0];
1440 unregister_netdevice(t->dev);
1441}
1442
1da177e4
LT
1443/**
1444 * ip6_tunnel_cleanup - free resources and unregister protocol
1445 **/
1446
1447static void __exit ip6_tunnel_cleanup(void)
1448{
c4d3efaf 1449 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
3144581c 1450 printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
c4d3efaf 1451
73d605d1 1452 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
3144581c 1453 printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
1da177e4 1454
b3fdd9f1 1455 rtnl_lock();
3144581c 1456 ip6_tnl_destroy_tunnels();
b3fdd9f1 1457 rtnl_unlock();
1da177e4
LT
1458}
1459
1460module_init(ip6_tunnel_init);
1461module_exit(ip6_tunnel_cleanup);