blob: a6541495deab57e7c46dc39dfac1e0dc57047240 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09002 * IPv6 tunneling device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Ville Nuorvala <vnuorval@tcs.hut.fi>
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09007 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * $Id$
10 *
11 * Based on:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090012 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * RFC 2473
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 *
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080024#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
26#include <linux/types.h>
27#include <linux/sockios.h>
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090028#include <linux/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/if.h>
30#include <linux/in.h>
31#include <linux/ip.h>
32#include <linux/if_tunnel.h>
33#include <linux/net.h>
34#include <linux/in6.h>
35#include <linux/netdevice.h>
36#include <linux/if_arp.h>
37#include <linux/icmpv6.h>
38#include <linux/init.h>
39#include <linux/route.h>
40#include <linux/rtnetlink.h>
41#include <linux/netfilter_ipv6.h>
42
43#include <asm/uaccess.h>
44#include <asm/atomic.h>
45
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090046#include <net/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/ip.h>
48#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/ip6_route.h>
50#include <net/addrconf.h>
51#include <net/ip6_tunnel.h>
52#include <net/xfrm.h>
53#include <net/dsfield.h>
54#include <net/inet_ecn.h>
55
56MODULE_AUTHOR("Ville Nuorvala");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090057MODULE_DESCRIPTION("IPv6 tunneling device");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_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)
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +090069#define IPV6_TCLASS_SHIFT 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#define HASH_SIZE 32
72
Al Viroe69a4ad2006-11-14 20:56:00 -080073#define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090074 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
75 (HASH_SIZE - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);
78static int ip6ip6_tnl_dev_init(struct net_device *dev);
79static void ip6ip6_tnl_dev_setup(struct net_device *dev);
80
81/* the IPv6 tunnel fallback device */
82static struct net_device *ip6ip6_fb_tnl_dev;
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 */
91static DEFINE_RWLOCK(ip6ip6_lock);
92
93static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
94{
95 struct dst_entry *dst = t->dst_cache;
96
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090097 if (dst && dst->obsolete &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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/**
122 * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900123 * @remote: the address of the tunnel exit-point
124 * @local: the address of the tunnel entry-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900126 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * tunnel matching given end-points if found,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900128 * else fallback tunnel if its device is up,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * else %NULL
130 **/
131
132static struct ip6_tnl *
133ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
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/**
152 * ip6ip6_bucket - get head of list matching given tunnel parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900153 * @p: parameters containing tunnel end-points
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *
155 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900156 * ip6ip6_bucket() returns the head of the list matching the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * &struct in6_addr entries laddr and raddr in @p.
158 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900159 * Return: head of IPv6 tunnel list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 **/
161
162static struct ip6_tnl **
163ip6ip6_bucket(struct ip6_tnl_parm *p)
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/**
178 * ip6ip6_tnl_link - add tunnel to hash table
179 * @t: tunnel to be added
180 **/
181
182static void
183ip6ip6_tnl_link(struct ip6_tnl *t)
184{
185 struct ip6_tnl **tp = ip6ip6_bucket(&t->parms);
186
187 t->next = *tp;
188 write_lock_bh(&ip6ip6_lock);
189 *tp = t;
190 write_unlock_bh(&ip6ip6_lock);
191}
192
193/**
194 * ip6ip6_tnl_unlink - remove tunnel from hash table
195 * @t: tunnel to be removed
196 **/
197
198static void
199ip6ip6_tnl_unlink(struct ip6_tnl *t)
200{
201 struct ip6_tnl **tp;
202
203 for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) {
204 if (t == *tp) {
205 write_lock_bh(&ip6ip6_lock);
206 *tp = t->next;
207 write_unlock_bh(&ip6ip6_lock);
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.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900220 *
221 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800222 * created tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 **/
224
Ville Nuorvala567131a2006-11-24 17:05:41 -0800225static struct ip6_tnl *ip6_tnl_create(struct ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
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);
238 if (__dev_get_by_name(name) == NULL)
239 break;
240 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900241 if (i == IP6_TNL_MAX)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800242 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup);
245 if (dev == NULL)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800246 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Patrick McHardy2941a482006-01-08 22:05:26 -0800248 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dev->init = ip6ip6_tnl_dev_init;
250 t->parms = *p;
251
252 if ((err = register_netdevice(dev)) < 0) {
253 free_netdev(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800254 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 ip6ip6_tnl_link(t);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800258 return t;
259failed:
260 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/**
264 * ip6ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900265 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * @create: != 0 if allowed to create new tunnel if no match found
267 *
268 * Description:
269 * ip6ip6_tnl_locate() first tries to locate an existing tunnel
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:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800274 * matching tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 **/
276
Ville Nuorvala567131a2006-11-24 17:05:41 -0800277static struct ip6_tnl *ip6ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
279 struct in6_addr *remote = &p->raddr;
280 struct in6_addr *local = &p->laddr;
281 struct ip6_tnl *t;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 for (t = *ip6ip6_bucket(p); t; t = t->next) {
284 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Ville Nuorvala567131a2006-11-24 17:05:41 -0800285 ipv6_addr_equal(remote, &t->parms.raddr))
286 return t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 if (!create)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800289 return NULL;
290 return ip6_tnl_create(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/**
294 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
295 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900296 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Description:
298 * ip6ip6_tnl_dev_uninit() removes tunnel from its list
299 **/
300
301static void
302ip6ip6_tnl_dev_uninit(struct net_device *dev)
303{
Patrick McHardy2941a482006-01-08 22:05:26 -0800304 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (dev == ip6ip6_fb_tnl_dev) {
307 write_lock_bh(&ip6ip6_lock);
308 tnls_wc[0] = NULL;
309 write_unlock_bh(&ip6ip6_lock);
310 } else {
311 ip6ip6_tnl_unlink(t);
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 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900321 * Return:
322 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * 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/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900379 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
381 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900382 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 * to the specifications in RFC 2473.
384 **/
385
Herbert Xud2acc342006-03-28 01:12:13 -0800386static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900387ip6_tnl_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
388 int *type, int *code, int *msg, __be32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
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;
Herbert Xud2acc342006-03-28 01:12:13 -0800397 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900399 /* If the packet doesn't contain the original IPv6 header we are
400 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 processing of the error. */
402
403 read_lock(&ip6ip6_lock);
404 if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
405 goto out;
406
Herbert Xud2acc342006-03-28 01:12:13 -0800407 err = 0;
408
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900409 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 __u32 teli;
411 struct ipv6_tlv_tnl_enc_lim *tel;
412 __u32 mtu;
413 case ICMPV6_DEST_UNREACH:
414 if (net_ratelimit())
415 printk(KERN_WARNING
416 "%s: Path to destination invalid "
417 "or inactive!\n", t->parms.name);
418 rel_msg = 1;
419 break;
420 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900421 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (net_ratelimit())
423 printk(KERN_WARNING
424 "%s: Too small hop limit or "
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900425 "routing loop in tunnel!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 t->parms.name);
427 rel_msg = 1;
428 }
429 break;
430 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800431 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900432 if ((*code) == ICMPV6_HDR_FIELD)
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800433 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900435 if (teli && teli == ntohl(*info) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
437 if (tel->encap_limit == 0) {
438 if (net_ratelimit())
439 printk(KERN_WARNING
440 "%s: Too small encapsulation "
441 "limit or routing loop in "
442 "tunnel!\n", t->parms.name);
443 rel_msg = 1;
444 }
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800445 } else if (net_ratelimit()) {
446 printk(KERN_WARNING
447 "%s: Recipient unable to parse tunneled "
448 "packet!\n ", t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450 break;
451 case ICMPV6_PKT_TOOBIG:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900452 mtu = ntohl(*info) - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (mtu < IPV6_MIN_MTU)
454 mtu = IPV6_MIN_MTU;
455 t->dev->mtu = mtu;
456
Al Virocc6cdac2006-02-18 16:02:18 -0500457 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 rel_type = ICMPV6_PKT_TOOBIG;
459 rel_code = 0;
460 rel_info = mtu;
461 rel_msg = 1;
462 }
463 break;
464 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900465
466 *type = rel_type;
467 *code = rel_code;
468 *info = rel_info;
469 *msg = rel_msg;
470
471out:
472 read_unlock(&ip6ip6_lock);
473 return err;
474}
475
476static int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900477ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
478 int type, int code, int offset, __u32 info)
479{
480 int rel_msg = 0;
481 int rel_type = type;
482 int rel_code = code;
483 __u32 rel_info = info;
484 int err;
485 struct sk_buff *skb2;
486 struct iphdr *eiph;
487 struct flowi fl;
488 struct rtable *rt;
489
490 err = ip6_tnl_err(skb, opt, &rel_type, &rel_code, &rel_msg, &rel_info,
491 offset);
492 if (err < 0)
493 return err;
494
495 if (rel_msg == 0)
496 return 0;
497
498 switch (rel_type) {
499 case ICMPV6_DEST_UNREACH:
500 if (rel_code != ICMPV6_ADDR_UNREACH)
501 return 0;
502 rel_type = ICMP_DEST_UNREACH;
503 rel_code = ICMP_HOST_UNREACH;
504 break;
505 case ICMPV6_PKT_TOOBIG:
506 if (rel_code != 0)
507 return 0;
508 rel_type = ICMP_DEST_UNREACH;
509 rel_code = ICMP_FRAG_NEEDED;
510 break;
511 default:
512 return 0;
513 }
514
515 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
516 return 0;
517
518 skb2 = skb_clone(skb, GFP_ATOMIC);
519 if (!skb2)
520 return 0;
521
522 dst_release(skb2->dst);
523 skb2->dst = NULL;
524 skb_pull(skb2, offset);
525 skb2->nh.raw = skb2->data;
526 eiph = skb2->nh.iph;
527
528 /* Try to guess incoming interface */
529 memset(&fl, 0, sizeof(fl));
530 fl.fl4_dst = eiph->saddr;
531 fl.fl4_tos = RT_TOS(eiph->tos);
532 fl.proto = IPPROTO_IPIP;
533 if (ip_route_output_key(&rt, &fl))
534 goto out;
535
536 skb2->dev = rt->u.dst.dev;
537
538 /* route "incoming" packet */
539 if (rt->rt_flags & RTCF_LOCAL) {
540 ip_rt_put(rt);
541 rt = NULL;
542 fl.fl4_dst = eiph->daddr;
543 fl.fl4_src = eiph->saddr;
544 fl.fl4_tos = eiph->tos;
545 if (ip_route_output_key(&rt, &fl) ||
546 rt->u.dst.dev->type != ARPHRD_TUNNEL) {
547 ip_rt_put(rt);
548 goto out;
549 }
550 } else {
551 ip_rt_put(rt);
552 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
553 skb2->dev) ||
554 skb2->dst->dev->type != ARPHRD_TUNNEL)
555 goto out;
556 }
557
558 /* change mtu on this route */
559 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
560 if (rel_info > dst_mtu(skb2->dst))
561 goto out;
562
563 skb2->dst->ops->update_pmtu(skb2->dst, rel_info);
564 rel_info = htonl(rel_info);
565 }
566
567 icmp_send(skb2, rel_type, rel_code, rel_info);
568
569out:
570 kfree_skb(skb2);
571 return 0;
572}
573
574static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900575ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
576 int type, int code, int offset, __u32 info)
577{
578 int rel_msg = 0;
579 int rel_type = type;
580 int rel_code = code;
581 __u32 rel_info = info;
582 int err;
583
584 err = ip6_tnl_err(skb, opt, &rel_type, &rel_code, &rel_msg, &rel_info,
585 offset);
586 if (err < 0)
587 return err;
588
589 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct rt6_info *rt;
591 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900594 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 dst_release(skb2->dst);
597 skb2->dst = NULL;
598 skb_pull(skb2, offset);
599 skb2->nh.raw = skb2->data;
600
601 /* Try to guess incoming interface */
602 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
603
604 if (rt && rt->rt6i_dev)
605 skb2->dev = rt->rt6i_dev;
606
607 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
608
609 if (rt)
610 dst_release(&rt->u.dst);
611
612 kfree_skb(skb2);
613 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900614
615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900618static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
619 struct ipv6hdr *ipv6h,
620 struct sk_buff *skb)
621{
622 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
623
624 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
625 ipv4_change_dsfield(skb->nh.iph, INET_ECN_MASK, dsfield);
626
627 if (INET_ECN_is_ce(dsfield))
628 IP_ECN_set_ce(skb->nh.iph);
629}
630
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900631static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
632 struct ipv6hdr *ipv6h,
633 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900635 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
636 ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900638 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
639 IP6_ECN_set_ce(skb->nh.ipv6h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900641
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800642static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
643{
644 struct ip6_tnl_parm *p = &t->parms;
645 int ret = 0;
646
647 if (p->flags & IP6_TNL_F_CAP_RCV) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900648 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800649
650 if (p->link)
651 ldev = dev_get_by_index(p->link);
652
653 if ((ipv6_addr_is_multicast(&p->laddr) ||
654 likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
655 likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
656 ret = 1;
657
658 if (ldev)
659 dev_put(ldev);
660 }
661 return ret;
662}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664/**
665 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
666 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900667 * @protocol: ethernet protocol ID
668 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 *
670 * Return: 0
671 **/
672
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900673static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
674 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
675 struct ipv6hdr *ipv6h,
676 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct ipv6hdr *ipv6h;
679 struct ip6_tnl *t;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 ipv6h = skb->nh.ipv6h;
682
683 read_lock(&ip6ip6_lock);
684
685 if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
686 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Ken-ichirou MATSUZAWA9f0ede52005-11-09 13:08:29 -0800687 read_unlock(&ip6ip6_lock);
Herbert Xu50fba2a2006-04-04 13:50:45 -0700688 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800691 if (!ip6_tnl_rcv_ctl(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 t->stat.rx_dropped++;
693 read_unlock(&ip6ip6_lock);
694 goto discard;
695 }
696 secpath_reset(skb);
697 skb->mac.raw = skb->nh.raw;
698 skb->nh.raw = skb->data;
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900699 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 skb->pkt_type = PACKET_HOST;
701 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
702 skb->dev = t->dev;
703 dst_release(skb->dst);
704 skb->dst = NULL;
Yasuyuki Kozakai53ab61c2006-11-06 10:06:23 -0800705 nf_reset(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900706
707 dscp_ecn_decapsulate(t, ipv6h, skb);
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 t->stat.rx_packets++;
710 t->stat.rx_bytes += skb->len;
711 netif_rx(skb);
712 read_unlock(&ip6ip6_lock);
713 return 0;
714 }
715 read_unlock(&ip6ip6_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700717
718discard:
719 kfree_skb(skb);
720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900723static int ip4ip6_rcv(struct sk_buff *skb)
724{
725 return ip6_tnl_rcv(skb, ETH_P_IP, ip4ip6_dscp_ecn_decapsulate);
726}
727
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900728static int ip6ip6_rcv(struct sk_buff *skb)
729{
730 return ip6_tnl_rcv(skb, ETH_P_IPV6, ip6ip6_dscp_ecn_decapsulate);
731}
732
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800733struct ipv6_tel_txoption {
734 struct ipv6_txoptions ops;
735 __u8 dst_opt[8];
736};
737
738static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800740 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800742 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
743 opt->dst_opt[3] = 1;
744 opt->dst_opt[4] = encap_limit;
745 opt->dst_opt[5] = IPV6_TLV_PADN;
746 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800748 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
749 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752/**
753 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
754 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900755 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 *
757 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900758 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * doesn't match source of incoming packet.
760 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900761 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 * 1 if conflict,
763 * 0 else
764 **/
765
766static inline int
767ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
768{
769 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
770}
771
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800772static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
773{
774 struct ip6_tnl_parm *p = &t->parms;
775 int ret = 0;
776
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900777 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800778 struct net_device *ldev = NULL;
779
780 if (p->link)
781 ldev = dev_get_by_index(p->link);
782
783 if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
784 printk(KERN_WARNING
785 "%s xmit: Local address not yet configured!\n",
786 p->name);
787 else if (!ipv6_addr_is_multicast(&p->raddr) &&
788 unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
789 printk(KERN_WARNING
790 "%s xmit: Routing loop! "
791 "Remote address found on this node!\n",
792 p->name);
793 else
794 ret = 1;
795 if (ldev)
796 dev_put(ldev);
797 }
798 return ret;
799}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900801 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900803 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900804 * @dsfield: dscp code for outer header
805 * @fl: flow of tunneled packet
806 * @encap_limit: encapsulation limit
807 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 *
809 * Description:
810 * Build new header and do some sanity checks on the packet before sending
811 * it.
812 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900813 * Return:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900814 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900815 * -1 fail
816 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 **/
818
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900819static int ip6_tnl_xmit2(struct sk_buff *skb,
820 struct net_device *dev,
821 __u8 dsfield,
822 struct flowi *fl,
823 int encap_limit,
824 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Patrick McHardy2941a482006-01-08 22:05:26 -0800826 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct net_device_stats *stats = &t->stat;
828 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800829 struct ipv6_tel_txoption opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct dst_entry *dst;
831 struct net_device *tdev;
832 int mtu;
833 int max_headroom = sizeof(struct ipv6hdr);
834 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900835 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if ((dst = ip6_tnl_dst_check(t)) != NULL)
839 dst_hold(dst);
Patrick McHardya57ebc92005-09-08 14:27:47 -0700840 else {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900841 dst = ip6_route_output(NULL, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900843 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
Patrick McHardya57ebc92005-09-08 14:27:47 -0700844 goto tx_err_link_failure;
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 tdev = dst->dev;
848
849 if (tdev == dev) {
850 stats->collisions++;
851 if (net_ratelimit())
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900852 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 "%s: Local routing loop detected!\n",
854 t->parms.name);
855 goto tx_err_dst_release;
856 }
857 mtu = dst_mtu(dst) - sizeof (*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800858 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 max_headroom += 8;
860 mtu -= 8;
861 }
862 if (mtu < IPV6_MIN_MTU)
863 mtu = IPV6_MIN_MTU;
Yasuyuki Kozakai26892052006-09-10 03:59:17 +0900864 if (skb->dst)
865 skb->dst->ops->update_pmtu(skb->dst, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900867 *pmtu = mtu;
868 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 goto tx_err_dst_release;
870 }
871
872 /*
873 * Okay, now see if we can stuff it in the buffer as-is.
874 */
875 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900876
877 if (skb_headroom(skb) < max_headroom ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 skb_cloned(skb) || skb_shared(skb)) {
879 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
882 goto tx_err_dst_release;
883
884 if (skb->sk)
885 skb_set_owner_w(new_skb, skb->sk);
886 kfree_skb(skb);
887 skb = new_skb;
888 }
889 dst_release(skb->dst);
890 skb->dst = dst_clone(dst);
891
892 skb->h.raw = skb->nh.raw;
893
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900894 proto = fl->proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800895 if (encap_limit >= 0) {
896 init_tel_txopt(&opt, encap_limit);
897 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr));
900 ipv6h = skb->nh.ipv6h;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900901 *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 dsfield = INET_ECN_encapsulate(0, dsfield);
903 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
904 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
905 ipv6h->hop_limit = t->parms.hop_limit;
906 ipv6h->nexthdr = proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900907 ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
908 ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 nf_reset(skb);
910 pkt_len = skb->len;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900911 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 skb->dst->dev, dst_output);
913
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200914 if (net_xmit_eval(err) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 stats->tx_bytes += pkt_len;
916 stats->tx_packets++;
917 } else {
918 stats->tx_errors++;
919 stats->tx_aborted_errors++;
920 }
921 ip6_tnl_dst_store(t, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return 0;
923tx_err_link_failure:
924 stats->tx_carrier_errors++;
925 dst_link_failure(skb);
926tx_err_dst_release:
927 dst_release(dst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900928 return err;
929}
930
931static inline int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900932ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
933{
934 struct ip6_tnl *t = netdev_priv(dev);
935 struct iphdr *iph = skb->nh.iph;
936 int encap_limit = -1;
937 struct flowi fl;
938 __u8 dsfield;
939 __u32 mtu;
940 int err;
941
942 if (!ip6_tnl_xmit_ctl(t))
943 return -1;
944
945 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
946 encap_limit = t->parms.encap_limit;
947
948 memcpy(&fl, &t->fl, sizeof (fl));
949 fl.proto = IPPROTO_IPIP;
950
951 dsfield = ipv4_get_dsfield(iph);
952
953 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
954 fl.fl6_flowlabel |= ntohl(((__u32)iph->tos << IPV6_TCLASS_SHIFT)
955 & IPV6_TCLASS_MASK);
956
957 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
958 if (err != 0) {
959 /* XXX: send ICMP error even if DF is not set. */
960 if (err == -EMSGSIZE)
961 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
962 htonl(mtu));
963 return -1;
964 }
965
966 return 0;
967}
968
969static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900970ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
971{
972 struct ip6_tnl *t = netdev_priv(dev);
973 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
974 int encap_limit = -1;
975 __u16 offset;
976 struct flowi fl;
977 __u8 dsfield;
978 __u32 mtu;
979 int err;
980
981 if (!ip6_tnl_xmit_ctl(t) || ip6ip6_tnl_addr_conflict(t, ipv6h))
982 return -1;
983
984 if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) {
985 struct ipv6_tlv_tnl_enc_lim *tel;
986 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset];
987 if (tel->encap_limit == 0) {
988 icmpv6_send(skb, ICMPV6_PARAMPROB,
989 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
990 return -1;
991 }
992 encap_limit = tel->encap_limit - 1;
993 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
994 encap_limit = t->parms.encap_limit;
995
996 memcpy(&fl, &t->fl, sizeof (fl));
997 fl.proto = IPPROTO_IPV6;
998
999 dsfield = ipv6_get_dsfield(ipv6h);
1000 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
1001 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1002 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
1003 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1004
1005 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1006 if (err != 0) {
1007 if (err == -EMSGSIZE)
1008 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1009 return -1;
1010 }
1011
1012 return 0;
1013}
1014
1015static int
1016ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1017{
1018 struct ip6_tnl *t = netdev_priv(dev);
1019 struct net_device_stats *stats = &t->stat;
1020 int ret;
1021
1022 if (t->recursion++) {
1023 t->stat.collisions++;
1024 goto tx_err;
1025 }
1026
1027 switch (skb->protocol) {
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001028 case __constant_htons(ETH_P_IP):
1029 ret = ip4ip6_tnl_xmit(skb, dev);
1030 break;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001031 case __constant_htons(ETH_P_IPV6):
1032 ret = ip6ip6_tnl_xmit(skb, dev);
1033 break;
1034 default:
1035 goto tx_err;
1036 }
1037
1038 if (ret < 0)
1039 goto tx_err;
1040
1041 t->recursion--;
1042 return 0;
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044tx_err:
1045 stats->tx_errors++;
1046 stats->tx_dropped++;
1047 kfree_skb(skb);
1048 t->recursion--;
1049 return 0;
1050}
1051
1052static void ip6_tnl_set_cap(struct ip6_tnl *t)
1053{
1054 struct ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001055 int ltype = ipv6_addr_type(&p->laddr);
1056 int rtype = ipv6_addr_type(&p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
1059
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001060 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1061 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1062 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001063 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001064 if (ltype&IPV6_ADDR_UNICAST)
1065 p->flags |= IP6_TNL_F_CAP_XMIT;
1066 if (rtype&IPV6_ADDR_UNICAST)
1067 p->flags |= IP6_TNL_F_CAP_RCV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069}
1070
1071static void ip6ip6_tnl_link_config(struct ip6_tnl *t)
1072{
1073 struct net_device *dev = t->dev;
1074 struct ip6_tnl_parm *p = &t->parms;
1075 struct flowi *fl = &t->fl;
1076
1077 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1078 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1079
1080 /* Set up flowi template */
1081 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
1082 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
1083 fl->oif = p->link;
1084 fl->fl6_flowlabel = 0;
1085
1086 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1087 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1088 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1089 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1090
1091 ip6_tnl_set_cap(t);
1092
1093 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1094 dev->flags |= IFF_POINTOPOINT;
1095 else
1096 dev->flags &= ~IFF_POINTOPOINT;
1097
1098 dev->iflink = p->link;
1099
1100 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001101 int strict = (ipv6_addr_type(&p->raddr) &
1102 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001105 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (rt == NULL)
1108 return;
1109
1110 if (rt->rt6i_dev) {
1111 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
1112 sizeof (struct ipv6hdr);
1113
1114 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
1115
1116 if (dev->mtu < IPV6_MIN_MTU)
1117 dev->mtu = IPV6_MIN_MTU;
1118 }
1119 dst_release(&rt->u.dst);
1120 }
1121}
1122
1123/**
1124 * ip6ip6_tnl_change - update the tunnel parameters
1125 * @t: tunnel to be changed
1126 * @p: tunnel configuration parameters
1127 * @active: != 0 if tunnel is ready for use
1128 *
1129 * Description:
1130 * ip6ip6_tnl_change() updates the tunnel parameters
1131 **/
1132
1133static int
1134ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
1135{
1136 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
1137 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
1138 t->parms.flags = p->flags;
1139 t->parms.hop_limit = p->hop_limit;
1140 t->parms.encap_limit = p->encap_limit;
1141 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001142 t->parms.link = p->link;
Hugo Santos0c088892006-02-24 13:16:25 -08001143 ip6_tnl_dst_reset(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 ip6ip6_tnl_link_config(t);
1145 return 0;
1146}
1147
1148/**
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001149 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * @dev: virtual device associated with tunnel
1151 * @ifr: parameters passed from userspace
1152 * @cmd: command to be performed
1153 *
1154 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001155 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
1156 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 *
1158 * The possible commands are the following:
1159 * %SIOCGETTUNNEL: get tunnel parameters for device
1160 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1161 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1162 * %SIOCDELTUNNEL: delete tunnel
1163 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001164 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 * initialization, can be used for creating other tunnel devices.
1166 *
1167 * Return:
1168 * 0 on success,
1169 * %-EFAULT if unable to copy data to or from userspace,
1170 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1171 * %-EINVAL if passed tunnel parameters are invalid,
1172 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1173 * %-ENODEV if attempting to change or delete a nonexisting device
1174 **/
1175
1176static int
1177ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1178{
1179 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 struct ip6_tnl_parm p;
1181 struct ip6_tnl *t = NULL;
1182
1183 switch (cmd) {
1184 case SIOCGETTUNNEL:
1185 if (dev == ip6ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001186 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 err = -EFAULT;
1188 break;
1189 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001190 t = ip6ip6_tnl_locate(&p, 0);
1191 }
1192 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -08001193 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 memcpy(&p, &t->parms, sizeof (p));
1195 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1196 err = -EFAULT;
1197 }
1198 break;
1199 case SIOCADDTUNNEL:
1200 case SIOCCHGTUNNEL:
1201 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (!capable(CAP_NET_ADMIN))
1203 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001204 err = -EFAULT;
1205 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001207 err = -EINVAL;
1208 if (p.proto != IPPROTO_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001210 t = ip6ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
1211 if (dev != ip6ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
1212 if (t != NULL) {
1213 if (t->dev != dev) {
1214 err = -EEXIST;
1215 break;
1216 }
1217 } else
1218 t = netdev_priv(dev);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 ip6ip6_tnl_unlink(t);
1221 err = ip6ip6_tnl_change(t, &p);
1222 ip6ip6_tnl_link(t);
1223 netdev_state_change(dev);
1224 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001225 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 err = 0;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001227 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1228 err = -EFAULT;
1229
1230 } else
1231 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 break;
1233 case SIOCDELTUNNEL:
1234 err = -EPERM;
1235 if (!capable(CAP_NET_ADMIN))
1236 break;
1237
1238 if (dev == ip6ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001239 err = -EFAULT;
1240 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001242 err = -ENOENT;
1243 if ((t = ip6ip6_tnl_locate(&p, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001245 err = -EPERM;
1246 if (t->dev == ip6ip6_fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001248 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001250 err = 0;
1251 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 break;
1253 default:
1254 err = -EINVAL;
1255 }
1256 return err;
1257}
1258
1259/**
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001260 * ip6ip6_tnl_get_stats - return the stats for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * @dev: virtual device associated with tunnel
1262 *
1263 * Return: stats for device
1264 **/
1265
1266static struct net_device_stats *
1267ip6ip6_tnl_get_stats(struct net_device *dev)
1268{
Patrick McHardy2941a482006-01-08 22:05:26 -08001269 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272/**
1273 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1274 * @dev: virtual device associated with tunnel
1275 * @new_mtu: the new mtu
1276 *
1277 * Return:
1278 * 0 on success,
1279 * %-EINVAL if mtu too small
1280 **/
1281
1282static int
1283ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1284{
1285 if (new_mtu < IPV6_MIN_MTU) {
1286 return -EINVAL;
1287 }
1288 dev->mtu = new_mtu;
1289 return 0;
1290}
1291
1292/**
1293 * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1294 * @dev: virtual device associated with tunnel
1295 *
1296 * Description:
1297 * Initialize function pointers and device parameters
1298 **/
1299
1300static void ip6ip6_tnl_dev_setup(struct net_device *dev)
1301{
1302 SET_MODULE_OWNER(dev);
1303 dev->uninit = ip6ip6_tnl_dev_uninit;
1304 dev->destructor = free_netdev;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001305 dev->hard_start_xmit = ip6_tnl_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 dev->get_stats = ip6ip6_tnl_get_stats;
1307 dev->do_ioctl = ip6ip6_tnl_ioctl;
1308 dev->change_mtu = ip6ip6_tnl_change_mtu;
1309
1310 dev->type = ARPHRD_TUNNEL6;
1311 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1312 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1313 dev->flags |= IFF_NOARP;
1314 dev->addr_len = sizeof(struct in6_addr);
1315}
1316
1317
1318/**
1319 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1320 * @dev: virtual device associated with tunnel
1321 **/
1322
1323static inline void
1324ip6ip6_tnl_dev_init_gen(struct net_device *dev)
1325{
Patrick McHardy2941a482006-01-08 22:05:26 -08001326 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 t->fl.proto = IPPROTO_IPV6;
1328 t->dev = dev;
1329 strcpy(t->parms.name, dev->name);
1330}
1331
1332/**
1333 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1334 * @dev: virtual device associated with tunnel
1335 **/
1336
1337static int
1338ip6ip6_tnl_dev_init(struct net_device *dev)
1339{
Patrick McHardy2941a482006-01-08 22:05:26 -08001340 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 ip6ip6_tnl_dev_init_gen(dev);
1342 ip6ip6_tnl_link_config(t);
1343 return 0;
1344}
1345
1346/**
1347 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1348 * @dev: fallback device
1349 *
1350 * Return: 0
1351 **/
1352
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001353static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354ip6ip6_fb_tnl_dev_init(struct net_device *dev)
1355{
Patrick McHardy2941a482006-01-08 22:05:26 -08001356 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 ip6ip6_tnl_dev_init_gen(dev);
1358 dev_hold(dev);
1359 tnls_wc[0] = t;
1360 return 0;
1361}
1362
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001363static struct xfrm6_tunnel ip4ip6_handler = {
1364 .handler = ip4ip6_rcv,
1365 .err_handler = ip4ip6_err,
1366 .priority = 1,
1367};
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369static struct xfrm6_tunnel ip6ip6_handler = {
Patrick McHardy03037702005-07-19 14:03:34 -07001370 .handler = ip6ip6_rcv,
1371 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001372 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373};
1374
1375/**
1376 * ip6_tunnel_init - register protocol and reserve needed resources
1377 *
1378 * Return: 0 on success
1379 **/
1380
1381static int __init ip6_tunnel_init(void)
1382{
1383 int err;
1384
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001385 if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
1386 printk(KERN_ERR "ip4ip6 init: can't register tunnel\n");
1387 err = -EAGAIN;
1388 goto out;
1389 }
1390
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001391 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 printk(KERN_ERR "ip6ip6 init: can't register tunnel\n");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001393 err = -EAGAIN;
1394 goto unreg_ip4ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396 ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1397 ip6ip6_tnl_dev_setup);
1398
1399 if (!ip6ip6_fb_tnl_dev) {
1400 err = -ENOMEM;
1401 goto fail;
1402 }
1403 ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init;
1404
1405 if ((err = register_netdev(ip6ip6_fb_tnl_dev))) {
1406 free_netdev(ip6ip6_fb_tnl_dev);
1407 goto fail;
1408 }
1409 return 0;
1410fail:
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001411 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001412unreg_ip4ip6:
1413 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1414out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return err;
1416}
1417
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001418static void __exit ip6ip6_destroy_tunnels(void)
1419{
1420 int h;
1421 struct ip6_tnl *t;
1422
1423 for (h = 0; h < HASH_SIZE; h++) {
1424 while ((t = tnls_r_l[h]) != NULL)
1425 unregister_netdevice(t->dev);
1426 }
1427
1428 t = tnls_wc[0];
1429 unregister_netdevice(t->dev);
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432/**
1433 * ip6_tunnel_cleanup - free resources and unregister protocol
1434 **/
1435
1436static void __exit ip6_tunnel_cleanup(void)
1437{
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001438 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
1439 printk(KERN_INFO "ip4ip6 close: can't deregister tunnel\n");
1440
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001441 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n");
1443
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001444 rtnl_lock();
1445 ip6ip6_destroy_tunnels();
1446 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449module_init(ip6_tunnel_init);
1450module_exit(ip6_tunnel_cleanup);