aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv6/ip6_tunnel.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@woody.linux-foundation.org>2007-04-27 12:26:46 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-04-27 12:26:46 -0400
commit15c54033964a943de7b0763efd3bd0ede7326395 (patch)
tree840b292612d1b5396d5bab5bde537a9013db3ceb /net/ipv6/ip6_tunnel.c
parentad5da3cf39a5b11a198929be1f2644e17ecd767e (diff)
parent912a41a4ab935ce8c4308428ec13fc7f8b1f18f4 (diff)
Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
* master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6: (448 commits) [IPV4] nl_fib_lookup: Initialise res.r before fib_res_put(&res) [IPV6]: Fix thinko in ipv6_rthdr_rcv() changes. [IPV4]: Add multipath cached to feature-removal-schedule.txt [WIRELESS] cfg80211: Clarify locking comment. [WIRELESS] cfg80211: Fix locking in wiphy_new. [WEXT] net_device: Don't include wext bits if not required. [WEXT]: Misc code cleanups. [WEXT]: Reduce inline abuse. [WEXT]: Move EXPORT_SYMBOL statements where they belong. [WEXT]: Cleanup early ioctl call path. [WEXT]: Remove options. [WEXT]: Remove dead debug code. [WEXT]: Clean up how wext is called. [WEXT]: Move to net/wireless [AFS]: Eliminate cmpxchg() usage in vlocation code. [RXRPC]: Fix pointers passed to bitops. [RXRPC]: Remove bogus atomic_* overrides. [AFS]: Fix u64 printing in debug logging. [AFS]: Add "directory write" support. [AFS]: Implement the CB.InitCallBackState3 operation. ...
Diffstat (limited to 'net/ipv6/ip6_tunnel.c')
-rw-r--r--net/ipv6/ip6_tunnel.c643
1 files changed, 464 insertions, 179 deletions
diff --git a/net/ipv6/ip6_tunnel.c b/net/ipv6/ip6_tunnel.c
index 08d944223ec8..a0902fbdb4e1 100644
--- a/net/ipv6/ip6_tunnel.c
+++ b/net/ipv6/ip6_tunnel.c
@@ -1,14 +1,15 @@
1/* 1/*
2 * IPv6 over IPv6 tunnel device 2 * IPv6 tunneling device
3 * Linux INET6 implementation 3 * Linux INET6 implementation
4 * 4 *
5 * Authors: 5 * Authors:
6 * Ville Nuorvala <vnuorval@tcs.hut.fi> 6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
7 * 8 *
8 * $Id$ 9 * $Id$
9 * 10 *
10 * Based on: 11 * Based on:
11 * linux/net/ipv6/sit.c 12 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
12 * 13 *
13 * RFC 2473 14 * RFC 2473
14 * 15 *
@@ -24,6 +25,7 @@
24#include <linux/errno.h> 25#include <linux/errno.h>
25#include <linux/types.h> 26#include <linux/types.h>
26#include <linux/sockios.h> 27#include <linux/sockios.h>
28#include <linux/icmp.h>
27#include <linux/if.h> 29#include <linux/if.h>
28#include <linux/in.h> 30#include <linux/in.h>
29#include <linux/ip.h> 31#include <linux/ip.h>
@@ -41,6 +43,7 @@
41#include <asm/uaccess.h> 43#include <asm/uaccess.h>
42#include <asm/atomic.h> 44#include <asm/atomic.h>
43 45
46#include <net/icmp.h>
44#include <net/ip.h> 47#include <net/ip.h>
45#include <net/ipv6.h> 48#include <net/ipv6.h>
46#include <net/ip6_route.h> 49#include <net/ip6_route.h>
@@ -51,7 +54,7 @@
51#include <net/inet_ecn.h> 54#include <net/inet_ecn.h>
52 55
53MODULE_AUTHOR("Ville Nuorvala"); 56MODULE_AUTHOR("Ville Nuorvala");
54MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel"); 57MODULE_DESCRIPTION("IPv6 tunneling device");
55MODULE_LICENSE("GPL"); 58MODULE_LICENSE("GPL");
56 59
57#define IPV6_TLV_TEL_DST_SIZE 8 60#define IPV6_TLV_TEL_DST_SIZE 8
@@ -63,6 +66,7 @@ MODULE_LICENSE("GPL");
63#endif 66#endif
64 67
65#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) 68#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
69#define IPV6_TCLASS_SHIFT 20
66 70
67#define HASH_SIZE 32 71#define HASH_SIZE 32
68 72
@@ -70,12 +74,12 @@ MODULE_LICENSE("GPL");
70 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \ 74 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
71 (HASH_SIZE - 1)) 75 (HASH_SIZE - 1))
72 76
73static int ip6ip6_fb_tnl_dev_init(struct net_device *dev); 77static int ip6_fb_tnl_dev_init(struct net_device *dev);
74static int ip6ip6_tnl_dev_init(struct net_device *dev); 78static int ip6_tnl_dev_init(struct net_device *dev);
75static void ip6ip6_tnl_dev_setup(struct net_device *dev); 79static void ip6_tnl_dev_setup(struct net_device *dev);
76 80
77/* the IPv6 tunnel fallback device */ 81/* the IPv6 tunnel fallback device */
78static struct net_device *ip6ip6_fb_tnl_dev; 82static struct net_device *ip6_fb_tnl_dev;
79 83
80 84
81/* lists for storing tunnels in use */ 85/* lists for storing tunnels in use */
@@ -84,7 +88,7 @@ static struct ip6_tnl *tnls_wc[1];
84static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l }; 88static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };
85 89
86/* lock for the tunnel lists */ 90/* lock for the tunnel lists */
87static DEFINE_RWLOCK(ip6ip6_lock); 91static DEFINE_RWLOCK(ip6_tnl_lock);
88 92
89static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t) 93static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
90{ 94{
@@ -115,7 +119,7 @@ static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
115} 119}
116 120
117/** 121/**
118 * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses 122 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
119 * @remote: the address of the tunnel exit-point 123 * @remote: the address of the tunnel exit-point
120 * @local: the address of the tunnel entry-point 124 * @local: the address of the tunnel entry-point
121 * 125 *
@@ -126,7 +130,7 @@ static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst)
126 **/ 130 **/
127 131
128static struct ip6_tnl * 132static struct ip6_tnl *
129ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local) 133ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
130{ 134{
131 unsigned h0 = HASH(remote); 135 unsigned h0 = HASH(remote);
132 unsigned h1 = HASH(local); 136 unsigned h1 = HASH(local);
@@ -145,18 +149,18 @@ ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
145} 149}
146 150
147/** 151/**
148 * ip6ip6_bucket - get head of list matching given tunnel parameters 152 * ip6_tnl_bucket - get head of list matching given tunnel parameters
149 * @p: parameters containing tunnel end-points 153 * @p: parameters containing tunnel end-points
150 * 154 *
151 * Description: 155 * Description:
152 * ip6ip6_bucket() returns the head of the list matching the 156 * ip6_tnl_bucket() returns the head of the list matching the
153 * &struct in6_addr entries laddr and raddr in @p. 157 * &struct in6_addr entries laddr and raddr in @p.
154 * 158 *
155 * Return: head of IPv6 tunnel list 159 * Return: head of IPv6 tunnel list
156 **/ 160 **/
157 161
158static struct ip6_tnl ** 162static struct ip6_tnl **
159ip6ip6_bucket(struct ip6_tnl_parm *p) 163ip6_tnl_bucket(struct ip6_tnl_parm *p)
160{ 164{
161 struct in6_addr *remote = &p->raddr; 165 struct in6_addr *remote = &p->raddr;
162 struct in6_addr *local = &p->laddr; 166 struct in6_addr *local = &p->laddr;
@@ -171,36 +175,36 @@ ip6ip6_bucket(struct ip6_tnl_parm *p)
171} 175}
172 176
173/** 177/**
174 * ip6ip6_tnl_link - add tunnel to hash table 178 * ip6_tnl_link - add tunnel to hash table
175 * @t: tunnel to be added 179 * @t: tunnel to be added
176 **/ 180 **/
177 181
178static void 182static void
179ip6ip6_tnl_link(struct ip6_tnl *t) 183ip6_tnl_link(struct ip6_tnl *t)
180{ 184{
181 struct ip6_tnl **tp = ip6ip6_bucket(&t->parms); 185 struct ip6_tnl **tp = ip6_tnl_bucket(&t->parms);
182 186
183 t->next = *tp; 187 t->next = *tp;
184 write_lock_bh(&ip6ip6_lock); 188 write_lock_bh(&ip6_tnl_lock);
185 *tp = t; 189 *tp = t;
186 write_unlock_bh(&ip6ip6_lock); 190 write_unlock_bh(&ip6_tnl_lock);
187} 191}
188 192
189/** 193/**
190 * ip6ip6_tnl_unlink - remove tunnel from hash table 194 * ip6_tnl_unlink - remove tunnel from hash table
191 * @t: tunnel to be removed 195 * @t: tunnel to be removed
192 **/ 196 **/
193 197
194static void 198static void
195ip6ip6_tnl_unlink(struct ip6_tnl *t) 199ip6_tnl_unlink(struct ip6_tnl *t)
196{ 200{
197 struct ip6_tnl **tp; 201 struct ip6_tnl **tp;
198 202
199 for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) { 203 for (tp = ip6_tnl_bucket(&t->parms); *tp; tp = &(*tp)->next) {
200 if (t == *tp) { 204 if (t == *tp) {
201 write_lock_bh(&ip6ip6_lock); 205 write_lock_bh(&ip6_tnl_lock);
202 *tp = t->next; 206 *tp = t->next;
203 write_unlock_bh(&ip6ip6_lock); 207 write_unlock_bh(&ip6_tnl_lock);
204 break; 208 break;
205 } 209 }
206 } 210 }
@@ -237,12 +241,12 @@ static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
237 if (i == IP6_TNL_MAX) 241 if (i == IP6_TNL_MAX)
238 goto failed; 242 goto failed;
239 } 243 }
240 dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup); 244 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
241 if (dev == NULL) 245 if (dev == NULL)
242 goto failed; 246 goto failed;
243 247
244 t = netdev_priv(dev); 248 t = netdev_priv(dev);
245 dev->init = ip6ip6_tnl_dev_init; 249 dev->init = ip6_tnl_dev_init;
246 t->parms = *p; 250 t->parms = *p;
247 251
248 if ((err = register_netdevice(dev)) < 0) { 252 if ((err = register_netdevice(dev)) < 0) {
@@ -250,19 +254,19 @@ static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
250 goto failed; 254 goto failed;
251 } 255 }
252 dev_hold(dev); 256 dev_hold(dev);
253 ip6ip6_tnl_link(t); 257 ip6_tnl_link(t);
254 return t; 258 return t;
255failed: 259failed:
256 return NULL; 260 return NULL;
257} 261}
258 262
259/** 263/**
260 * ip6ip6_tnl_locate - find or create tunnel matching given parameters 264 * ip6_tnl_locate - find or create tunnel matching given parameters
261 * @p: tunnel parameters 265 * @p: tunnel parameters
262 * @create: != 0 if allowed to create new tunnel if no match found 266 * @create: != 0 if allowed to create new tunnel if no match found
263 * 267 *
264 * Description: 268 * Description:
265 * ip6ip6_tnl_locate() first tries to locate an existing tunnel 269 * ip6_tnl_locate() first tries to locate an existing tunnel
266 * based on @parms. If this is unsuccessful, but @create is set a new 270 * based on @parms. If this is unsuccessful, but @create is set a new
267 * tunnel device is created and registered for use. 271 * tunnel device is created and registered for use.
268 * 272 *
@@ -270,13 +274,13 @@ failed:
270 * matching tunnel or NULL 274 * matching tunnel or NULL
271 **/ 275 **/
272 276
273static struct ip6_tnl *ip6ip6_tnl_locate(struct ip6_tnl_parm *p, int create) 277static struct ip6_tnl *ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
274{ 278{
275 struct in6_addr *remote = &p->raddr; 279 struct in6_addr *remote = &p->raddr;
276 struct in6_addr *local = &p->laddr; 280 struct in6_addr *local = &p->laddr;
277 struct ip6_tnl *t; 281 struct ip6_tnl *t;
278 282
279 for (t = *ip6ip6_bucket(p); t; t = t->next) { 283 for (t = *ip6_tnl_bucket(p); t; t = t->next) {
280 if (ipv6_addr_equal(local, &t->parms.laddr) && 284 if (ipv6_addr_equal(local, &t->parms.laddr) &&
281 ipv6_addr_equal(remote, &t->parms.raddr)) 285 ipv6_addr_equal(remote, &t->parms.raddr))
282 return t; 286 return t;
@@ -287,24 +291,24 @@ static struct ip6_tnl *ip6ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
287} 291}
288 292
289/** 293/**
290 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer 294 * ip6_tnl_dev_uninit - tunnel device uninitializer
291 * @dev: the device to be destroyed 295 * @dev: the device to be destroyed
292 * 296 *
293 * Description: 297 * Description:
294 * ip6ip6_tnl_dev_uninit() removes tunnel from its list 298 * ip6_tnl_dev_uninit() removes tunnel from its list
295 **/ 299 **/
296 300
297static void 301static void
298ip6ip6_tnl_dev_uninit(struct net_device *dev) 302ip6_tnl_dev_uninit(struct net_device *dev)
299{ 303{
300 struct ip6_tnl *t = netdev_priv(dev); 304 struct ip6_tnl *t = netdev_priv(dev);
301 305
302 if (dev == ip6ip6_fb_tnl_dev) { 306 if (dev == ip6_fb_tnl_dev) {
303 write_lock_bh(&ip6ip6_lock); 307 write_lock_bh(&ip6_tnl_lock);
304 tnls_wc[0] = NULL; 308 tnls_wc[0] = NULL;
305 write_unlock_bh(&ip6ip6_lock); 309 write_unlock_bh(&ip6_tnl_lock);
306 } else { 310 } else {
307 ip6ip6_tnl_unlink(t); 311 ip6_tnl_unlink(t);
308 } 312 }
309 ip6_tnl_dst_reset(t); 313 ip6_tnl_dst_reset(t);
310 dev_put(dev); 314 dev_put(dev);
@@ -372,16 +376,16 @@ parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
372} 376}
373 377
374/** 378/**
375 * ip6ip6_err - tunnel error handler 379 * ip6_tnl_err - tunnel error handler
376 * 380 *
377 * Description: 381 * Description:
378 * ip6ip6_err() should handle errors in the tunnel according 382 * ip6_tnl_err() should handle errors in the tunnel according
379 * to the specifications in RFC 2473. 383 * to the specifications in RFC 2473.
380 **/ 384 **/
381 385
382static int 386static int
383ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 387ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
384 int type, int code, int offset, __be32 info) 388 int *type, int *code, int *msg, __be32 *info, int offset)
385{ 389{
386 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data; 390 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
387 struct ip6_tnl *t; 391 struct ip6_tnl *t;
@@ -396,13 +400,16 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
396 in trouble since we might need the source address for further 400 in trouble since we might need the source address for further
397 processing of the error. */ 401 processing of the error. */
398 402
399 read_lock(&ip6ip6_lock); 403 read_lock(&ip6_tnl_lock);
400 if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL) 404 if ((t = ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
405 goto out;
406
407 if (t->parms.proto != ipproto && t->parms.proto != 0)
401 goto out; 408 goto out;
402 409
403 err = 0; 410 err = 0;
404 411
405 switch (type) { 412 switch (*type) {
406 __u32 teli; 413 __u32 teli;
407 struct ipv6_tlv_tnl_enc_lim *tel; 414 struct ipv6_tlv_tnl_enc_lim *tel;
408 __u32 mtu; 415 __u32 mtu;
@@ -414,7 +421,7 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
414 rel_msg = 1; 421 rel_msg = 1;
415 break; 422 break;
416 case ICMPV6_TIME_EXCEED: 423 case ICMPV6_TIME_EXCEED:
417 if (code == ICMPV6_EXC_HOPLIMIT) { 424 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
418 if (net_ratelimit()) 425 if (net_ratelimit())
419 printk(KERN_WARNING 426 printk(KERN_WARNING
420 "%s: Too small hop limit or " 427 "%s: Too small hop limit or "
@@ -425,10 +432,10 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
425 break; 432 break;
426 case ICMPV6_PARAMPROB: 433 case ICMPV6_PARAMPROB:
427 teli = 0; 434 teli = 0;
428 if (code == ICMPV6_HDR_FIELD) 435 if ((*code) == ICMPV6_HDR_FIELD)
429 teli = parse_tlv_tnl_enc_lim(skb, skb->data); 436 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
430 437
431 if (teli && teli == ntohl(info) - 2) { 438 if (teli && teli == ntohl(*info) - 2) {
432 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; 439 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
433 if (tel->encap_limit == 0) { 440 if (tel->encap_limit == 0) {
434 if (net_ratelimit()) 441 if (net_ratelimit())
@@ -445,7 +452,7 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
445 } 452 }
446 break; 453 break;
447 case ICMPV6_PKT_TOOBIG: 454 case ICMPV6_PKT_TOOBIG:
448 mtu = ntohl(info) - offset; 455 mtu = ntohl(*info) - offset;
449 if (mtu < IPV6_MIN_MTU) 456 if (mtu < IPV6_MIN_MTU)
450 mtu = IPV6_MIN_MTU; 457 mtu = IPV6_MIN_MTU;
451 t->dev->mtu = mtu; 458 t->dev->mtu = mtu;
@@ -458,20 +465,144 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
458 } 465 }
459 break; 466 break;
460 } 467 }
461 if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) { 468
469 *type = rel_type;
470 *code = rel_code;
471 *info = rel_info;
472 *msg = rel_msg;
473
474out:
475 read_unlock(&ip6_tnl_lock);
476 return err;
477}
478
479static int
480ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
481 int type, int code, int offset, __u32 info)
482{
483 int rel_msg = 0;
484 int rel_type = type;
485 int rel_code = code;
486 __u32 rel_info = info;
487 int err;
488 struct sk_buff *skb2;
489 struct iphdr *eiph;
490 struct flowi fl;
491 struct rtable *rt;
492
493 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
494 &rel_msg, &rel_info, offset);
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);
528 skb_reset_network_header(skb2);
529 eiph = ip_hdr(skb2);
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;
536 if (ip_route_output_key(&rt, &fl))
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;
548 if (ip_route_output_key(&rt, &fl) ||
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);
567 rel_info = htonl(rel_info);
568 }
569
570 icmp_send(skb2, rel_type, rel_code, rel_info);
571
572out:
573 kfree_skb(skb2);
574 return 0;
575}
576
577static int
578ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
579 int type, int code, int offset, __u32 info)
580{
581 int rel_msg = 0;
582 int rel_type = type;
583 int rel_code = code;
584 __u32 rel_info = info;
585 int err;
586
587 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
588 &rel_msg, &rel_info, offset);
589 if (err < 0)
590 return err;
591
592 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
462 struct rt6_info *rt; 593 struct rt6_info *rt;
463 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 594 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
464 595
465 if (!skb2) 596 if (!skb2)
466 goto out; 597 return 0;
467 598
468 dst_release(skb2->dst); 599 dst_release(skb2->dst);
469 skb2->dst = NULL; 600 skb2->dst = NULL;
470 skb_pull(skb2, offset); 601 skb_pull(skb2, offset);
471 skb2->nh.raw = skb2->data; 602 skb_reset_network_header(skb2);
472 603
473 /* Try to guess incoming interface */ 604 /* Try to guess incoming interface */
474 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0); 605 rt = rt6_lookup(&ipv6_hdr(skb2)->saddr, NULL, 0, 0);
475 606
476 if (rt && rt->rt6i_dev) 607 if (rt && rt->rt6i_dev)
477 skb2->dev = rt->rt6i_dev; 608 skb2->dev = rt->rt6i_dev;
@@ -483,19 +614,34 @@ ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
483 614
484 kfree_skb(skb2); 615 kfree_skb(skb2);
485 } 616 }
486out: 617
487 read_unlock(&ip6ip6_lock); 618 return 0;
488 return err;
489} 619}
490 620
491static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph, 621static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
492 struct sk_buff *skb) 622 struct ipv6hdr *ipv6h,
623 struct sk_buff *skb)
493{ 624{
494 struct ipv6hdr *inner_iph = skb->nh.ipv6h; 625 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
495 626
496 if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph))) 627 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
497 IP6_ECN_set_ce(inner_iph); 628 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
629
630 if (INET_ECN_is_ce(dsfield))
631 IP_ECN_set_ce(ip_hdr(skb));
632}
633
634static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
635 struct ipv6hdr *ipv6h,
636 struct sk_buff *skb)
637{
638 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
639 ipv6_copy_dscp(ipv6h, ipv6_hdr(skb));
640
641 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
642 IP6_ECN_set_ce(ipv6_hdr(skb));
498} 643}
644
499static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t) 645static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
500{ 646{
501 struct ip6_tnl_parm *p = &t->parms; 647 struct ip6_tnl_parm *p = &t->parms;
@@ -519,53 +665,61 @@ static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
519} 665}
520 666
521/** 667/**
522 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally 668 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
523 * @skb: received socket buffer 669 * @skb: received socket buffer
670 * @protocol: ethernet protocol ID
671 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
524 * 672 *
525 * Return: 0 673 * Return: 0
526 **/ 674 **/
527 675
528static int 676static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
529ip6ip6_rcv(struct sk_buff *skb) 677 __u8 ipproto,
678 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
679 struct ipv6hdr *ipv6h,
680 struct sk_buff *skb))
530{ 681{
531 struct ipv6hdr *ipv6h;
532 struct ip6_tnl *t; 682 struct ip6_tnl *t;
683 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
533 684
534 ipv6h = skb->nh.ipv6h; 685 read_lock(&ip6_tnl_lock);
535 686
536 read_lock(&ip6ip6_lock); 687 if ((t = ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
688 if (t->parms.proto != ipproto && t->parms.proto != 0) {
689 read_unlock(&ip6_tnl_lock);
690 goto discard;
691 }
537 692
538 if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
539 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { 693 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
540 read_unlock(&ip6ip6_lock); 694 read_unlock(&ip6_tnl_lock);
541 goto discard; 695 goto discard;
542 } 696 }
543 697
544 if (!ip6_tnl_rcv_ctl(t)) { 698 if (!ip6_tnl_rcv_ctl(t)) {
545 t->stat.rx_dropped++; 699 t->stat.rx_dropped++;
546 read_unlock(&ip6ip6_lock); 700 read_unlock(&ip6_tnl_lock);
547 goto discard; 701 goto discard;
548 } 702 }
549 secpath_reset(skb); 703 secpath_reset(skb);
550 skb->mac.raw = skb->nh.raw; 704 skb->mac_header = skb->network_header;
551 skb->nh.raw = skb->data; 705 skb_reset_network_header(skb);
552 skb->protocol = htons(ETH_P_IPV6); 706 skb->protocol = htons(protocol);
553 skb->pkt_type = PACKET_HOST; 707 skb->pkt_type = PACKET_HOST;
554 memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); 708 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
555 skb->dev = t->dev; 709 skb->dev = t->dev;
556 dst_release(skb->dst); 710 dst_release(skb->dst);
557 skb->dst = NULL; 711 skb->dst = NULL;
558 nf_reset(skb); 712 nf_reset(skb);
559 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) 713
560 ipv6_copy_dscp(ipv6h, skb->nh.ipv6h); 714 dscp_ecn_decapsulate(t, ipv6h, skb);
561 ip6ip6_ecn_decapsulate(ipv6h, skb); 715
562 t->stat.rx_packets++; 716 t->stat.rx_packets++;
563 t->stat.rx_bytes += skb->len; 717 t->stat.rx_bytes += skb->len;
564 netif_rx(skb); 718 netif_rx(skb);
565 read_unlock(&ip6ip6_lock); 719 read_unlock(&ip6_tnl_lock);
566 return 0; 720 return 0;
567 } 721 }
568 read_unlock(&ip6ip6_lock); 722 read_unlock(&ip6_tnl_lock);
569 return 1; 723 return 1;
570 724
571discard: 725discard:
@@ -573,6 +727,18 @@ discard:
573 return 0; 727 return 0;
574} 728}
575 729
730static int ip4ip6_rcv(struct sk_buff *skb)
731{
732 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
733 ip4ip6_dscp_ecn_decapsulate);
734}
735
736static int ip6ip6_rcv(struct sk_buff *skb)
737{
738 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
739 ip6ip6_dscp_ecn_decapsulate);
740}
741
576struct ipv6_tel_txoption { 742struct ipv6_tel_txoption {
577 struct ipv6_txoptions ops; 743 struct ipv6_txoptions ops;
578 __u8 dst_opt[8]; 744 __u8 dst_opt[8];
@@ -593,7 +759,7 @@ static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
593} 759}
594 760
595/** 761/**
596 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own 762 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
597 * @t: the outgoing tunnel device 763 * @t: the outgoing tunnel device
598 * @hdr: IPv6 header from the incoming packet 764 * @hdr: IPv6 header from the incoming packet
599 * 765 *
@@ -607,7 +773,7 @@ static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
607 **/ 773 **/
608 774
609static inline int 775static inline int
610ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr) 776ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
611{ 777{
612 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); 778 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
613} 779}
@@ -641,72 +807,49 @@ static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
641 return ret; 807 return ret;
642} 808}
643/** 809/**
644 * ip6ip6_tnl_xmit - encapsulate packet and send 810 * ip6_tnl_xmit2 - encapsulate packet and send
645 * @skb: the outgoing socket buffer 811 * @skb: the outgoing socket buffer
646 * @dev: the outgoing tunnel device 812 * @dev: the outgoing tunnel device
813 * @dsfield: dscp code for outer header
814 * @fl: flow of tunneled packet
815 * @encap_limit: encapsulation limit
816 * @pmtu: Path MTU is stored if packet is too big
647 * 817 *
648 * Description: 818 * Description:
649 * Build new header and do some sanity checks on the packet before sending 819 * Build new header and do some sanity checks on the packet before sending
650 * it. 820 * it.
651 * 821 *
652 * Return: 822 * Return:
653 * 0 823 * 0 on success
824 * -1 fail
825 * %-EMSGSIZE message too big. return mtu in this case.
654 **/ 826 **/
655 827
656static int 828static int ip6_tnl_xmit2(struct sk_buff *skb,
657ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) 829 struct net_device *dev,
830 __u8 dsfield,
831 struct flowi *fl,
832 int encap_limit,
833 __u32 *pmtu)
658{ 834{
659 struct ip6_tnl *t = netdev_priv(dev); 835 struct ip6_tnl *t = netdev_priv(dev);
660 struct net_device_stats *stats = &t->stat; 836 struct net_device_stats *stats = &t->stat;
661 struct ipv6hdr *ipv6h = skb->nh.ipv6h; 837 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
662 int encap_limit = -1;
663 struct ipv6_tel_txoption opt; 838 struct ipv6_tel_txoption opt;
664 __u16 offset;
665 struct flowi fl;
666 struct dst_entry *dst; 839 struct dst_entry *dst;
667 struct net_device *tdev; 840 struct net_device *tdev;
668 int mtu; 841 int mtu;
669 int max_headroom = sizeof(struct ipv6hdr); 842 int max_headroom = sizeof(struct ipv6hdr);
670 u8 proto; 843 u8 proto;
671 int err; 844 int err = -1;
672 int pkt_len; 845 int pkt_len;
673 int dsfield;
674
675 if (t->recursion++) {
676 stats->collisions++;
677 goto tx_err;
678 }
679 if (skb->protocol != htons(ETH_P_IPV6) ||
680 !ip6_tnl_xmit_ctl(t) || ip6ip6_tnl_addr_conflict(t, ipv6h))
681 goto tx_err;
682
683 if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
684 struct ipv6_tlv_tnl_enc_lim *tel;
685 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
686 if (tel->encap_limit == 0) {
687 icmpv6_send(skb, ICMPV6_PARAMPROB,
688 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
689 goto tx_err;
690 }
691 encap_limit = tel->encap_limit - 1;
692 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
693 encap_limit = t->parms.encap_limit;
694
695 memcpy(&fl, &t->fl, sizeof (fl));
696 proto = fl.proto;
697
698 dsfield = ipv6_get_dsfield(ipv6h);
699 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
700 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
701 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
702 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
703 846
704 if ((dst = ip6_tnl_dst_check(t)) != NULL) 847 if ((dst = ip6_tnl_dst_check(t)) != NULL)
705 dst_hold(dst); 848 dst_hold(dst);
706 else { 849 else {
707 dst = ip6_route_output(NULL, &fl); 850 dst = ip6_route_output(NULL, fl);
708 851
709 if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0) 852 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
710 goto tx_err_link_failure; 853 goto tx_err_link_failure;
711 } 854 }
712 855
@@ -730,7 +873,8 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
730 if (skb->dst) 873 if (skb->dst)
731 skb->dst->ops->update_pmtu(skb->dst, mtu); 874 skb->dst->ops->update_pmtu(skb->dst, mtu);
732 if (skb->len > mtu) { 875 if (skb->len > mtu) {
733 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev); 876 *pmtu = mtu;
877 err = -EMSGSIZE;
734 goto tx_err_dst_release; 878 goto tx_err_dst_release;
735 } 879 }
736 880
@@ -754,22 +898,24 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
754 dst_release(skb->dst); 898 dst_release(skb->dst);
755 skb->dst = dst_clone(dst); 899 skb->dst = dst_clone(dst);
756 900
757 skb->h.raw = skb->nh.raw; 901 skb->transport_header = skb->network_header;
758 902
903 proto = fl->proto;
759 if (encap_limit >= 0) { 904 if (encap_limit >= 0) {
760 init_tel_txopt(&opt, encap_limit); 905 init_tel_txopt(&opt, encap_limit);
761 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL); 906 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
762 } 907 }
763 skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr)); 908 skb_push(skb, sizeof(struct ipv6hdr));
764 ipv6h = skb->nh.ipv6h; 909 skb_reset_network_header(skb);
765 *(__be32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000); 910 ipv6h = ipv6_hdr(skb);
911 *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
766 dsfield = INET_ECN_encapsulate(0, dsfield); 912 dsfield = INET_ECN_encapsulate(0, dsfield);
767 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield); 913 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
768 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); 914 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
769 ipv6h->hop_limit = t->parms.hop_limit; 915 ipv6h->hop_limit = t->parms.hop_limit;
770 ipv6h->nexthdr = proto; 916 ipv6h->nexthdr = proto;
771 ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src); 917 ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
772 ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst); 918 ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
773 nf_reset(skb); 919 nf_reset(skb);
774 pkt_len = skb->len; 920 pkt_len = skb->len;
775 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, 921 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
@@ -783,13 +929,131 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
783 stats->tx_aborted_errors++; 929 stats->tx_aborted_errors++;
784 } 930 }
785 ip6_tnl_dst_store(t, dst); 931 ip6_tnl_dst_store(t, dst);
786 t->recursion--;
787 return 0; 932 return 0;
788tx_err_link_failure: 933tx_err_link_failure:
789 stats->tx_carrier_errors++; 934 stats->tx_carrier_errors++;
790 dst_link_failure(skb); 935 dst_link_failure(skb);
791tx_err_dst_release: 936tx_err_dst_release:
792 dst_release(dst); 937 dst_release(dst);
938 return err;
939}
940
941static inline int
942ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
943{
944 struct ip6_tnl *t = netdev_priv(dev);
945 struct iphdr *iph = ip_hdr(skb);
946 int encap_limit = -1;
947 struct flowi fl;
948 __u8 dsfield;
949 __u32 mtu;
950 int err;
951
952 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
953 !ip6_tnl_xmit_ctl(t))
954 return -1;
955
956 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
957 encap_limit = t->parms.encap_limit;
958
959 memcpy(&fl, &t->fl, sizeof (fl));
960 fl.proto = IPPROTO_IPIP;
961
962 dsfield = ipv4_get_dsfield(iph);
963
964 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
965 fl.fl6_flowlabel |= ntohl(((__u32)iph->tos << IPV6_TCLASS_SHIFT)
966 & IPV6_TCLASS_MASK);
967
968 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
969 if (err != 0) {
970 /* XXX: send ICMP error even if DF is not set. */
971 if (err == -EMSGSIZE)
972 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
973 htonl(mtu));
974 return -1;
975 }
976
977 return 0;
978}
979
980static inline int
981ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
982{
983 struct ip6_tnl *t = netdev_priv(dev);
984 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
985 int encap_limit = -1;
986 __u16 offset;
987 struct flowi fl;
988 __u8 dsfield;
989 __u32 mtu;
990 int err;
991
992 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
993 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
994 return -1;
995
996 offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
997 if (offset > 0) {
998 struct ipv6_tlv_tnl_enc_lim *tel;
999 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
1000 if (tel->encap_limit == 0) {
1001 icmpv6_send(skb, ICMPV6_PARAMPROB,
1002 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
1003 return -1;
1004 }
1005 encap_limit = tel->encap_limit - 1;
1006 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1007 encap_limit = t->parms.encap_limit;
1008
1009 memcpy(&fl, &t->fl, sizeof (fl));
1010 fl.proto = IPPROTO_IPV6;
1011
1012 dsfield = ipv6_get_dsfield(ipv6h);
1013 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
1014 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1015 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
1016 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1017
1018 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1019 if (err != 0) {
1020 if (err == -EMSGSIZE)
1021 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1022 return -1;
1023 }
1024
1025 return 0;
1026}
1027
1028static int
1029ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1030{
1031 struct ip6_tnl *t = netdev_priv(dev);
1032 struct net_device_stats *stats = &t->stat;
1033 int ret;
1034
1035 if (t->recursion++) {
1036 t->stat.collisions++;
1037 goto tx_err;
1038 }
1039
1040 switch (skb->protocol) {
1041 case __constant_htons(ETH_P_IP):
1042 ret = ip4ip6_tnl_xmit(skb, dev);
1043 break;
1044 case __constant_htons(ETH_P_IPV6):
1045 ret = ip6ip6_tnl_xmit(skb, dev);
1046 break;
1047 default:
1048 goto tx_err;
1049 }
1050
1051 if (ret < 0)
1052 goto tx_err;
1053
1054 t->recursion--;
1055 return 0;
1056
793tx_err: 1057tx_err:
794 stats->tx_errors++; 1058 stats->tx_errors++;
795 stats->tx_dropped++; 1059 stats->tx_dropped++;
@@ -817,7 +1081,7 @@ static void ip6_tnl_set_cap(struct ip6_tnl *t)
817 } 1081 }
818} 1082}
819 1083
820static void ip6ip6_tnl_link_config(struct ip6_tnl *t) 1084static void ip6_tnl_link_config(struct ip6_tnl *t)
821{ 1085{
822 struct net_device *dev = t->dev; 1086 struct net_device *dev = t->dev;
823 struct ip6_tnl_parm *p = &t->parms; 1087 struct ip6_tnl_parm *p = &t->parms;
@@ -870,17 +1134,17 @@ static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
870} 1134}
871 1135
872/** 1136/**
873 * ip6ip6_tnl_change - update the tunnel parameters 1137 * ip6_tnl_change - update the tunnel parameters
874 * @t: tunnel to be changed 1138 * @t: tunnel to be changed
875 * @p: tunnel configuration parameters 1139 * @p: tunnel configuration parameters
876 * @active: != 0 if tunnel is ready for use 1140 * @active: != 0 if tunnel is ready for use
877 * 1141 *
878 * Description: 1142 * Description:
879 * ip6ip6_tnl_change() updates the tunnel parameters 1143 * ip6_tnl_change() updates the tunnel parameters
880 **/ 1144 **/
881 1145
882static int 1146static int
883ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p) 1147ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
884{ 1148{
885 ipv6_addr_copy(&t->parms.laddr, &p->laddr); 1149 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
886 ipv6_addr_copy(&t->parms.raddr, &p->raddr); 1150 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
@@ -889,19 +1153,20 @@ ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
889 t->parms.encap_limit = p->encap_limit; 1153 t->parms.encap_limit = p->encap_limit;
890 t->parms.flowinfo = p->flowinfo; 1154 t->parms.flowinfo = p->flowinfo;
891 t->parms.link = p->link; 1155 t->parms.link = p->link;
1156 t->parms.proto = p->proto;
892 ip6_tnl_dst_reset(t); 1157 ip6_tnl_dst_reset(t);
893 ip6ip6_tnl_link_config(t); 1158 ip6_tnl_link_config(t);
894 return 0; 1159 return 0;
895} 1160}
896 1161
897/** 1162/**
898 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace 1163 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
899 * @dev: virtual device associated with tunnel 1164 * @dev: virtual device associated with tunnel
900 * @ifr: parameters passed from userspace 1165 * @ifr: parameters passed from userspace
901 * @cmd: command to be performed 1166 * @cmd: command to be performed
902 * 1167 *
903 * Description: 1168 * Description:
904 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels 1169 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
905 * from userspace. 1170 * from userspace.
906 * 1171 *
907 * The possible commands are the following: 1172 * The possible commands are the following:
@@ -923,7 +1188,7 @@ ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
923 **/ 1188 **/
924 1189
925static int 1190static int
926ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 1191ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
927{ 1192{
928 int err = 0; 1193 int err = 0;
929 struct ip6_tnl_parm p; 1194 struct ip6_tnl_parm p;
@@ -931,12 +1196,12 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
931 1196
932 switch (cmd) { 1197 switch (cmd) {
933 case SIOCGETTUNNEL: 1198 case SIOCGETTUNNEL:
934 if (dev == ip6ip6_fb_tnl_dev) { 1199 if (dev == ip6_fb_tnl_dev) {
935 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) { 1200 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
936 err = -EFAULT; 1201 err = -EFAULT;
937 break; 1202 break;
938 } 1203 }
939 t = ip6ip6_tnl_locate(&p, 0); 1204 t = ip6_tnl_locate(&p, 0);
940 } 1205 }
941 if (t == NULL) 1206 if (t == NULL)
942 t = netdev_priv(dev); 1207 t = netdev_priv(dev);
@@ -954,10 +1219,11 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
954 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) 1219 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
955 break; 1220 break;
956 err = -EINVAL; 1221 err = -EINVAL;
957 if (p.proto != IPPROTO_IPV6) 1222 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1223 p.proto != 0)
958 break; 1224 break;
959 t = ip6ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL); 1225 t = ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
960 if (dev != ip6ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) { 1226 if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
961 if (t != NULL) { 1227 if (t != NULL) {
962 if (t->dev != dev) { 1228 if (t->dev != dev) {
963 err = -EEXIST; 1229 err = -EEXIST;
@@ -966,9 +1232,9 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
966 } else 1232 } else
967 t = netdev_priv(dev); 1233 t = netdev_priv(dev);
968 1234
969 ip6ip6_tnl_unlink(t); 1235 ip6_tnl_unlink(t);
970 err = ip6ip6_tnl_change(t, &p); 1236 err = ip6_tnl_change(t, &p);
971 ip6ip6_tnl_link(t); 1237 ip6_tnl_link(t);
972 netdev_state_change(dev); 1238 netdev_state_change(dev);
973 } 1239 }
974 if (t) { 1240 if (t) {
@@ -984,15 +1250,15 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
984 if (!capable(CAP_NET_ADMIN)) 1250 if (!capable(CAP_NET_ADMIN))
985 break; 1251 break;
986 1252
987 if (dev == ip6ip6_fb_tnl_dev) { 1253 if (dev == ip6_fb_tnl_dev) {
988 err = -EFAULT; 1254 err = -EFAULT;
989 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) 1255 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
990 break; 1256 break;
991 err = -ENOENT; 1257 err = -ENOENT;
992 if ((t = ip6ip6_tnl_locate(&p, 0)) == NULL) 1258 if ((t = ip6_tnl_locate(&p, 0)) == NULL)
993 break; 1259 break;
994 err = -EPERM; 1260 err = -EPERM;
995 if (t->dev == ip6ip6_fb_tnl_dev) 1261 if (t->dev == ip6_fb_tnl_dev)
996 break; 1262 break;
997 dev = t->dev; 1263 dev = t->dev;
998 } 1264 }
@@ -1006,20 +1272,20 @@ ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1006} 1272}
1007 1273
1008/** 1274/**
1009 * ip6ip6_tnl_get_stats - return the stats for tunnel device 1275 * ip6_tnl_get_stats - return the stats for tunnel device
1010 * @dev: virtual device associated with tunnel 1276 * @dev: virtual device associated with tunnel
1011 * 1277 *
1012 * Return: stats for device 1278 * Return: stats for device
1013 **/ 1279 **/
1014 1280
1015static struct net_device_stats * 1281static struct net_device_stats *
1016ip6ip6_tnl_get_stats(struct net_device *dev) 1282ip6_tnl_get_stats(struct net_device *dev)
1017{ 1283{
1018 return &(((struct ip6_tnl *)netdev_priv(dev))->stat); 1284 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
1019} 1285}
1020 1286
1021/** 1287/**
1022 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device 1288 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1023 * @dev: virtual device associated with tunnel 1289 * @dev: virtual device associated with tunnel
1024 * @new_mtu: the new mtu 1290 * @new_mtu: the new mtu
1025 * 1291 *
@@ -1029,7 +1295,7 @@ ip6ip6_tnl_get_stats(struct net_device *dev)
1029 **/ 1295 **/
1030 1296
1031static int 1297static int
1032ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) 1298ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1033{ 1299{
1034 if (new_mtu < IPV6_MIN_MTU) { 1300 if (new_mtu < IPV6_MIN_MTU) {
1035 return -EINVAL; 1301 return -EINVAL;
@@ -1039,22 +1305,22 @@ ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1039} 1305}
1040 1306
1041/** 1307/**
1042 * ip6ip6_tnl_dev_setup - setup virtual tunnel device 1308 * ip6_tnl_dev_setup - setup virtual tunnel device
1043 * @dev: virtual device associated with tunnel 1309 * @dev: virtual device associated with tunnel
1044 * 1310 *
1045 * Description: 1311 * Description:
1046 * Initialize function pointers and device parameters 1312 * Initialize function pointers and device parameters
1047 **/ 1313 **/
1048 1314
1049static void ip6ip6_tnl_dev_setup(struct net_device *dev) 1315static void ip6_tnl_dev_setup(struct net_device *dev)
1050{ 1316{
1051 SET_MODULE_OWNER(dev); 1317 SET_MODULE_OWNER(dev);
1052 dev->uninit = ip6ip6_tnl_dev_uninit; 1318 dev->uninit = ip6_tnl_dev_uninit;
1053 dev->destructor = free_netdev; 1319 dev->destructor = free_netdev;
1054 dev->hard_start_xmit = ip6ip6_tnl_xmit; 1320 dev->hard_start_xmit = ip6_tnl_xmit;
1055 dev->get_stats = ip6ip6_tnl_get_stats; 1321 dev->get_stats = ip6_tnl_get_stats;
1056 dev->do_ioctl = ip6ip6_tnl_ioctl; 1322 dev->do_ioctl = ip6_tnl_ioctl;
1057 dev->change_mtu = ip6ip6_tnl_change_mtu; 1323 dev->change_mtu = ip6_tnl_change_mtu;
1058 1324
1059 dev->type = ARPHRD_TUNNEL6; 1325 dev->type = ARPHRD_TUNNEL6;
1060 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr); 1326 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
@@ -1065,50 +1331,56 @@ static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1065 1331
1066 1332
1067/** 1333/**
1068 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices 1334 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1069 * @dev: virtual device associated with tunnel 1335 * @dev: virtual device associated with tunnel
1070 **/ 1336 **/
1071 1337
1072static inline void 1338static inline void
1073ip6ip6_tnl_dev_init_gen(struct net_device *dev) 1339ip6_tnl_dev_init_gen(struct net_device *dev)
1074{ 1340{
1075 struct ip6_tnl *t = netdev_priv(dev); 1341 struct ip6_tnl *t = netdev_priv(dev);
1076 t->fl.proto = IPPROTO_IPV6;
1077 t->dev = dev; 1342 t->dev = dev;
1078 strcpy(t->parms.name, dev->name); 1343 strcpy(t->parms.name, dev->name);
1079} 1344}
1080 1345
1081/** 1346/**
1082 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices 1347 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1083 * @dev: virtual device associated with tunnel 1348 * @dev: virtual device associated with tunnel
1084 **/ 1349 **/
1085 1350
1086static int 1351static int
1087ip6ip6_tnl_dev_init(struct net_device *dev) 1352ip6_tnl_dev_init(struct net_device *dev)
1088{ 1353{
1089 struct ip6_tnl *t = netdev_priv(dev); 1354 struct ip6_tnl *t = netdev_priv(dev);
1090 ip6ip6_tnl_dev_init_gen(dev); 1355 ip6_tnl_dev_init_gen(dev);
1091 ip6ip6_tnl_link_config(t); 1356 ip6_tnl_link_config(t);
1092 return 0; 1357 return 0;
1093} 1358}
1094 1359
1095/** 1360/**
1096 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device 1361 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1097 * @dev: fallback device 1362 * @dev: fallback device
1098 * 1363 *
1099 * Return: 0 1364 * Return: 0
1100 **/ 1365 **/
1101 1366
1102static int 1367static int
1103ip6ip6_fb_tnl_dev_init(struct net_device *dev) 1368ip6_fb_tnl_dev_init(struct net_device *dev)
1104{ 1369{
1105 struct ip6_tnl *t = netdev_priv(dev); 1370 struct ip6_tnl *t = netdev_priv(dev);
1106 ip6ip6_tnl_dev_init_gen(dev); 1371 ip6_tnl_dev_init_gen(dev);
1372 t->parms.proto = IPPROTO_IPV6;
1107 dev_hold(dev); 1373 dev_hold(dev);
1108 tnls_wc[0] = t; 1374 tnls_wc[0] = t;
1109 return 0; 1375 return 0;
1110} 1376}
1111 1377
1378static struct xfrm6_tunnel ip4ip6_handler = {
1379 .handler = ip4ip6_rcv,
1380 .err_handler = ip4ip6_err,
1381 .priority = 1,
1382};
1383
1112static struct xfrm6_tunnel ip6ip6_handler = { 1384static struct xfrm6_tunnel ip6ip6_handler = {
1113 .handler = ip6ip6_rcv, 1385 .handler = ip6ip6_rcv,
1114 .err_handler = ip6ip6_err, 1386 .err_handler = ip6ip6_err,
@@ -1125,30 +1397,40 @@ static int __init ip6_tunnel_init(void)
1125{ 1397{
1126 int err; 1398 int err;
1127 1399
1400 if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
1401 printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
1402 err = -EAGAIN;
1403 goto out;
1404 }
1405
1128 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) { 1406 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
1129 printk(KERN_ERR "ip6ip6 init: can't register tunnel\n"); 1407 printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
1130 return -EAGAIN; 1408 err = -EAGAIN;
1409 goto unreg_ip4ip6;
1131 } 1410 }
1132 ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", 1411 ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1133 ip6ip6_tnl_dev_setup); 1412 ip6_tnl_dev_setup);
1134 1413
1135 if (!ip6ip6_fb_tnl_dev) { 1414 if (!ip6_fb_tnl_dev) {
1136 err = -ENOMEM; 1415 err = -ENOMEM;
1137 goto fail; 1416 goto fail;
1138 } 1417 }
1139 ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init; 1418 ip6_fb_tnl_dev->init = ip6_fb_tnl_dev_init;
1140 1419
1141 if ((err = register_netdev(ip6ip6_fb_tnl_dev))) { 1420 if ((err = register_netdev(ip6_fb_tnl_dev))) {
1142 free_netdev(ip6ip6_fb_tnl_dev); 1421 free_netdev(ip6_fb_tnl_dev);
1143 goto fail; 1422 goto fail;
1144 } 1423 }
1145 return 0; 1424 return 0;
1146fail: 1425fail:
1147 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6); 1426 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
1427unreg_ip4ip6:
1428 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1429out:
1148 return err; 1430 return err;
1149} 1431}
1150 1432
1151static void __exit ip6ip6_destroy_tunnels(void) 1433static void __exit ip6_tnl_destroy_tunnels(void)
1152{ 1434{
1153 int h; 1435 int h;
1154 struct ip6_tnl *t; 1436 struct ip6_tnl *t;
@@ -1168,11 +1450,14 @@ static void __exit ip6ip6_destroy_tunnels(void)
1168 1450
1169static void __exit ip6_tunnel_cleanup(void) 1451static void __exit ip6_tunnel_cleanup(void)
1170{ 1452{
1453 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1454 printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
1455
1171 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6)) 1456 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
1172 printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n"); 1457 printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
1173 1458
1174 rtnl_lock(); 1459 rtnl_lock();
1175 ip6ip6_destroy_tunnels(); 1460 ip6_tnl_destroy_tunnels();
1176 rtnl_unlock(); 1461 rtnl_unlock();
1177} 1462}
1178 1463