blob: 2a124e9a1b2d5b46bcd70c18364ce1e9775b5cbc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Yasuyuki Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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 Kozakaic4d3efa2007-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
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090077static int ip6_fb_tnl_dev_init(struct net_device *dev);
78static int ip6_tnl_dev_init(struct net_device *dev);
79static void ip6_tnl_dev_setup(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* the IPv6 tunnel fallback device */
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090082static struct net_device *ip6_fb_tnl_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
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 */
Yasuyuki Kozakai31445812007-02-10 00:30:33 +090091static DEFINE_RWLOCK(ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
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/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900122 * ip6_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 *
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900133ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
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/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900152 * ip6_tnl_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:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900156 * ip6_tnl_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 **
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900163ip6_tnl_bucket(struct ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
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/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900178 * ip6_tnl_link - add tunnel to hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * @t: tunnel to be added
180 **/
181
182static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900183ip6_tnl_link(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900185 struct ip6_tnl **tp = ip6_tnl_bucket(&t->parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 t->next = *tp;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900188 write_lock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *tp = t;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900190 write_unlock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900194 * ip6_tnl_unlink - remove tunnel from hash table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * @t: tunnel to be removed
196 **/
197
198static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900199ip6_tnl_unlink(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 struct ip6_tnl **tp;
202
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900203 for (tp = ip6_tnl_bucket(&t->parms); *tp; tp = &(*tp)->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (t == *tp) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900205 write_lock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *tp = t->next;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900207 write_unlock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 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
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800232 if (p->name[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 strlcpy(name, p->name, IFNAMSIZ);
Pavel Emelyanov34cc7ba2008-02-23 20:19:20 -0800234 else
235 sprintf(name, "ip6tnl%%d");
236
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900237 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (dev == NULL)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800239 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Patrick McHardy2941a482006-01-08 22:05:26 -0800241 t = netdev_priv(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900242 dev->init = ip6_tnl_dev_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 t->parms = *p;
244
245 if ((err = register_netdevice(dev)) < 0) {
246 free_netdev(dev);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800247 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 dev_hold(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900250 ip6_tnl_link(t);
Ville Nuorvala567131a2006-11-24 17:05:41 -0800251 return t;
252failed:
253 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900257 * ip6_tnl_locate - find or create tunnel matching given parameters
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900258 * @p: tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * @create: != 0 if allowed to create new tunnel if no match found
260 *
261 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900262 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * based on @parms. If this is unsuccessful, but @create is set a new
264 * tunnel device is created and registered for use.
265 *
266 * Return:
Ville Nuorvala567131a2006-11-24 17:05:41 -0800267 * matching tunnel or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 **/
269
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900270static struct ip6_tnl *ip6_tnl_locate(struct ip6_tnl_parm *p, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 struct in6_addr *remote = &p->raddr;
273 struct in6_addr *local = &p->laddr;
274 struct ip6_tnl *t;
275
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900276 for (t = *ip6_tnl_bucket(p); t; t = t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (ipv6_addr_equal(local, &t->parms.laddr) &&
Ville Nuorvala567131a2006-11-24 17:05:41 -0800278 ipv6_addr_equal(remote, &t->parms.raddr))
279 return t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 if (!create)
Ville Nuorvala567131a2006-11-24 17:05:41 -0800282 return NULL;
283 return ip6_tnl_create(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900287 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900289 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900291 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 **/
293
294static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900295ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Patrick McHardy2941a482006-01-08 22:05:26 -0800297 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900299 if (dev == ip6_fb_tnl_dev) {
300 write_lock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 tnls_wc[0] = NULL;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900302 write_unlock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 } else {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900304 ip6_tnl_unlink(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306 ip6_tnl_dst_reset(t);
307 dev_put(dev);
308}
309
310/**
311 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
312 * @skb: received socket buffer
313 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900314 * Return:
315 * 0 if none was found,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * else index to encapsulation limit
317 **/
318
319static __u16
320parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw)
321{
322 struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw;
323 __u8 nexthdr = ipv6h->nexthdr;
324 __u16 off = sizeof (*ipv6h);
325
326 while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) {
327 __u16 optlen = 0;
328 struct ipv6_opt_hdr *hdr;
329 if (raw + off + sizeof (*hdr) > skb->data &&
330 !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr)))
331 break;
332
333 hdr = (struct ipv6_opt_hdr *) (raw + off);
334 if (nexthdr == NEXTHDR_FRAGMENT) {
335 struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr;
336 if (frag_hdr->frag_off)
337 break;
338 optlen = 8;
339 } else if (nexthdr == NEXTHDR_AUTH) {
340 optlen = (hdr->hdrlen + 2) << 2;
341 } else {
342 optlen = ipv6_optlen(hdr);
343 }
344 if (nexthdr == NEXTHDR_DEST) {
345 __u16 i = off + 2;
346 while (1) {
347 struct ipv6_tlv_tnl_enc_lim *tel;
348
349 /* No more room for encapsulation limit */
350 if (i + sizeof (*tel) > off + optlen)
351 break;
352
353 tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i];
354 /* return index of option if found and valid */
355 if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT &&
356 tel->length == 1)
357 return i;
358 /* else jump to next option */
359 if (tel->type)
360 i += tel->length + 2;
361 else
362 i++;
363 }
364 }
365 nexthdr = hdr->nexthdr;
366 off += optlen;
367 }
368 return 0;
369}
370
371/**
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900372 * ip6_tnl_err - tunnel error handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
374 * Description:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900375 * ip6_tnl_err() should handle errors in the tunnel according
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * to the specifications in RFC 2473.
377 **/
378
Herbert Xud2acc342006-03-28 01:12:13 -0800379static int
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900380ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Al Viro704eae12007-07-26 17:33:29 +0100381 int *type, int *code, int *msg, __u32 *info, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data;
384 struct ip6_tnl *t;
385 int rel_msg = 0;
386 int rel_type = ICMPV6_DEST_UNREACH;
387 int rel_code = ICMPV6_ADDR_UNREACH;
388 __u32 rel_info = 0;
389 __u16 len;
Herbert Xud2acc342006-03-28 01:12:13 -0800390 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900392 /* If the packet doesn't contain the original IPv6 header we are
393 in trouble since we might need the source address for further
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 processing of the error. */
395
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900396 read_lock(&ip6_tnl_lock);
397 if ((t = ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto out;
399
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900400 if (t->parms.proto != ipproto && t->parms.proto != 0)
401 goto out;
402
Herbert Xud2acc342006-03-28 01:12:13 -0800403 err = 0;
404
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900405 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 __u32 teli;
407 struct ipv6_tlv_tnl_enc_lim *tel;
408 __u32 mtu;
409 case ICMPV6_DEST_UNREACH:
410 if (net_ratelimit())
411 printk(KERN_WARNING
412 "%s: Path to destination invalid "
413 "or inactive!\n", t->parms.name);
414 rel_msg = 1;
415 break;
416 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900417 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (net_ratelimit())
419 printk(KERN_WARNING
420 "%s: Too small hop limit or "
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900421 "routing loop in tunnel!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 t->parms.name);
423 rel_msg = 1;
424 }
425 break;
426 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800427 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900428 if ((*code) == ICMPV6_HDR_FIELD)
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800429 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Al Viro704eae12007-07-26 17:33:29 +0100431 if (teli && teli == *info - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
433 if (tel->encap_limit == 0) {
434 if (net_ratelimit())
435 printk(KERN_WARNING
436 "%s: Too small encapsulation "
437 "limit or routing loop in "
438 "tunnel!\n", t->parms.name);
439 rel_msg = 1;
440 }
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800441 } else if (net_ratelimit()) {
442 printk(KERN_WARNING
443 "%s: Recipient unable to parse tunneled "
444 "packet!\n ", t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 break;
447 case ICMPV6_PKT_TOOBIG:
Al Viro704eae12007-07-26 17:33:29 +0100448 mtu = *info - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (mtu < IPV6_MIN_MTU)
450 mtu = IPV6_MIN_MTU;
451 t->dev->mtu = mtu;
452
Al Virocc6cdac2006-02-18 16:02:18 -0500453 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 rel_type = ICMPV6_PKT_TOOBIG;
455 rel_code = 0;
456 rel_info = mtu;
457 rel_msg = 1;
458 }
459 break;
460 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900461
462 *type = rel_type;
463 *code = rel_code;
464 *info = rel_info;
465 *msg = rel_msg;
466
467out:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900468 read_unlock(&ip6_tnl_lock);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900469 return err;
470}
471
472static int
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900473ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Al Viro704eae12007-07-26 17:33:29 +0100474 int type, int code, int offset, __be32 info)
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900475{
476 int rel_msg = 0;
477 int rel_type = type;
478 int rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100479 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900480 int err;
481 struct sk_buff *skb2;
482 struct iphdr *eiph;
483 struct flowi fl;
484 struct rtable *rt;
485
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900486 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
487 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900488 if (err < 0)
489 return err;
490
491 if (rel_msg == 0)
492 return 0;
493
494 switch (rel_type) {
495 case ICMPV6_DEST_UNREACH:
496 if (rel_code != ICMPV6_ADDR_UNREACH)
497 return 0;
498 rel_type = ICMP_DEST_UNREACH;
499 rel_code = ICMP_HOST_UNREACH;
500 break;
501 case ICMPV6_PKT_TOOBIG:
502 if (rel_code != 0)
503 return 0;
504 rel_type = ICMP_DEST_UNREACH;
505 rel_code = ICMP_FRAG_NEEDED;
506 break;
507 default:
508 return 0;
509 }
510
511 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
512 return 0;
513
514 skb2 = skb_clone(skb, GFP_ATOMIC);
515 if (!skb2)
516 return 0;
517
518 dst_release(skb2->dst);
519 skb2->dst = NULL;
520 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700521 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700522 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900523
524 /* Try to guess incoming interface */
525 memset(&fl, 0, sizeof(fl));
526 fl.fl4_dst = eiph->saddr;
527 fl.fl4_tos = RT_TOS(eiph->tos);
528 fl.proto = IPPROTO_IPIP;
Denis V. Lunevf2063512008-01-22 22:07:34 -0800529 if (ip_route_output_key(&init_net, &rt, &fl))
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900530 goto out;
531
532 skb2->dev = rt->u.dst.dev;
533
534 /* route "incoming" packet */
535 if (rt->rt_flags & RTCF_LOCAL) {
536 ip_rt_put(rt);
537 rt = NULL;
538 fl.fl4_dst = eiph->daddr;
539 fl.fl4_src = eiph->saddr;
540 fl.fl4_tos = eiph->tos;
Denis V. Lunevf2063512008-01-22 22:07:34 -0800541 if (ip_route_output_key(&init_net, &rt, &fl) ||
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900542 rt->u.dst.dev->type != ARPHRD_TUNNEL) {
543 ip_rt_put(rt);
544 goto out;
545 }
Denis V. Lunev9937ded2008-02-18 20:49:36 -0800546 skb2->dst = (struct dst_entry *)rt;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900547 } else {
548 ip_rt_put(rt);
549 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
550 skb2->dev) ||
551 skb2->dst->dev->type != ARPHRD_TUNNEL)
552 goto out;
553 }
554
555 /* change mtu on this route */
556 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
557 if (rel_info > dst_mtu(skb2->dst))
558 goto out;
559
560 skb2->dst->ops->update_pmtu(skb2->dst, rel_info);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900561 }
562
Al Viro704eae12007-07-26 17:33:29 +0100563 icmp_send(skb2, rel_type, rel_code, htonl(rel_info));
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900564
565out:
566 kfree_skb(skb2);
567 return 0;
568}
569
570static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900571ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Al Viro704eae12007-07-26 17:33:29 +0100572 int type, int code, int offset, __be32 info)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900573{
574 int rel_msg = 0;
575 int rel_type = type;
576 int rel_code = code;
Al Viro704eae12007-07-26 17:33:29 +0100577 __u32 rel_info = ntohl(info);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900578 int err;
579
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900580 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
581 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900582 if (err < 0)
583 return err;
584
585 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct rt6_info *rt;
587 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 dst_release(skb2->dst);
593 skb2->dst = NULL;
594 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700595 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Try to guess incoming interface */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700598 rt = rt6_lookup(&ipv6_hdr(skb2)->saddr, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (rt && rt->rt6i_dev)
601 skb2->dev = rt->rt6i_dev;
602
603 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
604
605 if (rt)
606 dst_release(&rt->u.dst);
607
608 kfree_skb(skb2);
609 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900610
611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900614static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
615 struct ipv6hdr *ipv6h,
616 struct sk_buff *skb)
617{
618 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
619
620 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700621 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900622
623 if (INET_ECN_is_ce(dsfield))
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700624 IP_ECN_set_ce(ip_hdr(skb));
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900625}
626
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900627static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
628 struct ipv6hdr *ipv6h,
629 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900631 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Herbert Xu29bb43b42007-11-13 21:40:13 -0800632 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h), ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900634 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700635 IP6_ECN_set_ce(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900637
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800638static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
639{
640 struct ip6_tnl_parm *p = &t->parms;
641 int ret = 0;
642
643 if (p->flags & IP6_TNL_F_CAP_RCV) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900644 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800645
646 if (p->link)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700647 ldev = dev_get_by_index(&init_net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800648
649 if ((ipv6_addr_is_multicast(&p->laddr) ||
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800650 likely(ipv6_chk_addr(&init_net, &p->laddr, ldev, 0))) &&
651 likely(!ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800652 ret = 1;
653
654 if (ldev)
655 dev_put(ldev);
656 }
657 return ret;
658}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900661 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900663 * @protocol: ethernet protocol ID
664 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 *
666 * Return: 0
667 **/
668
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900669static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900670 __u8 ipproto,
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900671 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
672 struct ipv6hdr *ipv6h,
673 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct ip6_tnl *t;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700676 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900678 read_lock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900680 if ((t = ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900681 if (t->parms.proto != ipproto && t->parms.proto != 0) {
682 read_unlock(&ip6_tnl_lock);
683 goto discard;
684 }
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900687 read_unlock(&ip6_tnl_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++;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900693 read_unlock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto discard;
695 }
696 secpath_reset(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700697 skb->mac_header = skb->network_header;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700698 skb_reset_network_header(skb);
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);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900712 read_unlock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 0;
714 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900715 read_unlock(&ip6_tnl_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 Kozakaic4d3efa2007-02-15 00:43:16 +0900723static int ip4ip6_rcv(struct sk_buff *skb)
724{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900725 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
726 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900727}
728
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900729static int ip6ip6_rcv(struct sk_buff *skb)
730{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900731 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
732 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900733}
734
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800735struct ipv6_tel_txoption {
736 struct ipv6_txoptions ops;
737 __u8 dst_opt[8];
738};
739
740static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800742 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800744 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
745 opt->dst_opt[3] = 1;
746 opt->dst_opt[4] = encap_limit;
747 opt->dst_opt[5] = IPV6_TLV_PADN;
748 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800750 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
751 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900755 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900757 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
759 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900760 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * doesn't match source of incoming packet.
762 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900763 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * 1 if conflict,
765 * 0 else
766 **/
767
768static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900769ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
772}
773
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800774static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
775{
776 struct ip6_tnl_parm *p = &t->parms;
777 int ret = 0;
778
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900779 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800780 struct net_device *ldev = NULL;
781
782 if (p->link)
Eric W. Biederman881d9662007-09-17 11:56:21 -0700783 ldev = dev_get_by_index(&init_net, p->link);
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800784
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800785 if (unlikely(!ipv6_chk_addr(&init_net, &p->laddr, ldev, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800786 printk(KERN_WARNING
787 "%s xmit: Local address not yet configured!\n",
788 p->name);
789 else if (!ipv6_addr_is_multicast(&p->raddr) &&
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800790 unlikely(ipv6_chk_addr(&init_net, &p->raddr, NULL, 0)))
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800791 printk(KERN_WARNING
792 "%s xmit: Routing loop! "
793 "Remote address found on this node!\n",
794 p->name);
795 else
796 ret = 1;
797 if (ldev)
798 dev_put(ldev);
799 }
800 return ret;
801}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900803 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900805 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900806 * @dsfield: dscp code for outer header
807 * @fl: flow of tunneled packet
808 * @encap_limit: encapsulation limit
809 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
811 * Description:
812 * Build new header and do some sanity checks on the packet before sending
813 * it.
814 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900815 * Return:
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900816 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900817 * -1 fail
818 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 **/
820
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900821static int ip6_tnl_xmit2(struct sk_buff *skb,
822 struct net_device *dev,
823 __u8 dsfield,
824 struct flowi *fl,
825 int encap_limit,
826 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Patrick McHardy2941a482006-01-08 22:05:26 -0800828 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct net_device_stats *stats = &t->stat;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700830 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800831 struct ipv6_tel_txoption opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct dst_entry *dst;
833 struct net_device *tdev;
834 int mtu;
Chuck Leverc2636b42007-10-23 21:07:32 -0700835 unsigned int max_headroom = sizeof(struct ipv6hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900837 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 int pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if ((dst = ip6_tnl_dst_check(t)) != NULL)
841 dst_hold(dst);
Patrick McHardya57ebc92005-09-08 14:27:47 -0700842 else {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900843 dst = ip6_route_output(NULL, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900845 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
Patrick McHardya57ebc92005-09-08 14:27:47 -0700846 goto tx_err_link_failure;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 tdev = dst->dev;
850
851 if (tdev == dev) {
852 stats->collisions++;
853 if (net_ratelimit())
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900854 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 "%s: Local routing loop detected!\n",
856 t->parms.name);
857 goto tx_err_dst_release;
858 }
859 mtu = dst_mtu(dst) - sizeof (*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800860 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 max_headroom += 8;
862 mtu -= 8;
863 }
864 if (mtu < IPV6_MIN_MTU)
865 mtu = IPV6_MIN_MTU;
Yasuyuki Kozakai26892052006-09-10 03:59:17 +0900866 if (skb->dst)
867 skb->dst->ops->update_pmtu(skb->dst, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900869 *pmtu = mtu;
870 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 goto tx_err_dst_release;
872 }
873
874 /*
875 * Okay, now see if we can stuff it in the buffer as-is.
876 */
877 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900878
Patrick McHardycfbba492007-07-09 15:33:40 -0700879 if (skb_headroom(skb) < max_headroom || skb_shared(skb) ||
880 (skb_cloned(skb) && !skb_clone_writable(skb, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
884 goto tx_err_dst_release;
885
886 if (skb->sk)
887 skb_set_owner_w(new_skb, skb->sk);
888 kfree_skb(skb);
889 skb = new_skb;
890 }
891 dst_release(skb->dst);
892 skb->dst = dst_clone(dst);
893
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700894 skb->transport_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900896 proto = fl->proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800897 if (encap_limit >= 0) {
898 init_tel_txopt(&opt, encap_limit);
899 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
900 }
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700901 skb_push(skb, sizeof(struct ipv6hdr));
902 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700903 ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900904 *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 dsfield = INET_ECN_encapsulate(0, dsfield);
906 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ipv6h->hop_limit = t->parms.hop_limit;
908 ipv6h->nexthdr = proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900909 ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
910 ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 nf_reset(skb);
912 pkt_len = skb->len;
Herbert Xuef76bc22008-01-11 19:15:08 -0800913 err = ip6_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200915 if (net_xmit_eval(err) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 stats->tx_bytes += pkt_len;
917 stats->tx_packets++;
918 } else {
919 stats->tx_errors++;
920 stats->tx_aborted_errors++;
921 }
922 ip6_tnl_dst_store(t, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 0;
924tx_err_link_failure:
925 stats->tx_carrier_errors++;
926 dst_link_failure(skb);
927tx_err_dst_release:
928 dst_release(dst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900929 return err;
930}
931
932static inline int
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900933ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
934{
935 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700936 struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900937 int encap_limit = -1;
938 struct flowi fl;
939 __u8 dsfield;
940 __u32 mtu;
941 int err;
942
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900943 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
944 !ip6_tnl_xmit_ctl(t))
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900945 return -1;
946
947 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
948 encap_limit = t->parms.encap_limit;
949
950 memcpy(&fl, &t->fl, sizeof (fl));
951 fl.proto = IPPROTO_IPIP;
952
953 dsfield = ipv4_get_dsfield(iph);
954
955 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
Al Virob77f2fa2007-07-21 19:09:41 -0700956 fl.fl6_flowlabel |= htonl((__u32)iph->tos << IPV6_TCLASS_SHIFT)
957 & IPV6_TCLASS_MASK;
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +0900958
959 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
960 if (err != 0) {
961 /* XXX: send ICMP error even if DF is not set. */
962 if (err == -EMSGSIZE)
963 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
964 htonl(mtu));
965 return -1;
966 }
967
968 return 0;
969}
970
971static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900972ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
973{
974 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700975 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900976 int encap_limit = -1;
977 __u16 offset;
978 struct flowi fl;
979 __u8 dsfield;
980 __u32 mtu;
981 int err;
982
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900983 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
984 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900985 return -1;
986
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700987 offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
988 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900989 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700990 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900991 if (tel->encap_limit == 0) {
992 icmpv6_send(skb, ICMPV6_PARAMPROB,
993 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
994 return -1;
995 }
996 encap_limit = tel->encap_limit - 1;
997 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
998 encap_limit = t->parms.encap_limit;
999
1000 memcpy(&fl, &t->fl, sizeof (fl));
1001 fl.proto = IPPROTO_IPV6;
1002
1003 dsfield = ipv6_get_dsfield(ipv6h);
1004 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
1005 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1006 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
1007 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1008
1009 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1010 if (err != 0) {
1011 if (err == -EMSGSIZE)
1012 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1013 return -1;
1014 }
1015
1016 return 0;
1017}
1018
1019static int
1020ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1021{
1022 struct ip6_tnl *t = netdev_priv(dev);
1023 struct net_device_stats *stats = &t->stat;
1024 int ret;
1025
1026 if (t->recursion++) {
1027 t->stat.collisions++;
1028 goto tx_err;
1029 }
1030
1031 switch (skb->protocol) {
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001032 case __constant_htons(ETH_P_IP):
1033 ret = ip4ip6_tnl_xmit(skb, dev);
1034 break;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001035 case __constant_htons(ETH_P_IPV6):
1036 ret = ip6ip6_tnl_xmit(skb, dev);
1037 break;
1038 default:
1039 goto tx_err;
1040 }
1041
1042 if (ret < 0)
1043 goto tx_err;
1044
1045 t->recursion--;
1046 return 0;
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048tx_err:
1049 stats->tx_errors++;
1050 stats->tx_dropped++;
1051 kfree_skb(skb);
1052 t->recursion--;
1053 return 0;
1054}
1055
1056static void ip6_tnl_set_cap(struct ip6_tnl *t)
1057{
1058 struct ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001059 int ltype = ipv6_addr_type(&p->laddr);
1060 int rtype = ipv6_addr_type(&p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
1063
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001064 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1065 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1066 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001067 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001068 if (ltype&IPV6_ADDR_UNICAST)
1069 p->flags |= IP6_TNL_F_CAP_XMIT;
1070 if (rtype&IPV6_ADDR_UNICAST)
1071 p->flags |= IP6_TNL_F_CAP_RCV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073}
1074
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001075static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct net_device *dev = t->dev;
1078 struct ip6_tnl_parm *p = &t->parms;
1079 struct flowi *fl = &t->fl;
1080
1081 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1082 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1083
1084 /* Set up flowi template */
1085 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
1086 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
1087 fl->oif = p->link;
1088 fl->fl6_flowlabel = 0;
1089
1090 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1091 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1092 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1093 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1094
1095 ip6_tnl_set_cap(t);
1096
1097 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1098 dev->flags |= IFF_POINTOPOINT;
1099 else
1100 dev->flags &= ~IFF_POINTOPOINT;
1101
1102 dev->iflink = p->link;
1103
1104 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001105 int strict = (ipv6_addr_type(&p->raddr) &
1106 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001109 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (rt == NULL)
1112 return;
1113
1114 if (rt->rt6i_dev) {
1115 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
1116 sizeof (struct ipv6hdr);
1117
1118 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
1119
1120 if (dev->mtu < IPV6_MIN_MTU)
1121 dev->mtu = IPV6_MIN_MTU;
1122 }
1123 dst_release(&rt->u.dst);
1124 }
1125}
1126
1127/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001128 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 * @t: tunnel to be changed
1130 * @p: tunnel configuration parameters
1131 * @active: != 0 if tunnel is ready for use
1132 *
1133 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001134 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 **/
1136
1137static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001138ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
1140 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
1141 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
1142 t->parms.flags = p->flags;
1143 t->parms.hop_limit = p->hop_limit;
1144 t->parms.encap_limit = p->encap_limit;
1145 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001146 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001147 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001148 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001149 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return 0;
1151}
1152
1153/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001154 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 * @dev: virtual device associated with tunnel
1156 * @ifr: parameters passed from userspace
1157 * @cmd: command to be performed
1158 *
1159 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001160 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001161 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 *
1163 * The possible commands are the following:
1164 * %SIOCGETTUNNEL: get tunnel parameters for device
1165 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1166 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1167 * %SIOCDELTUNNEL: delete tunnel
1168 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001169 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 * initialization, can be used for creating other tunnel devices.
1171 *
1172 * Return:
1173 * 0 on success,
1174 * %-EFAULT if unable to copy data to or from userspace,
1175 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1176 * %-EINVAL if passed tunnel parameters are invalid,
1177 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1178 * %-ENODEV if attempting to change or delete a nonexisting device
1179 **/
1180
1181static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001182ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 struct ip6_tnl_parm p;
1186 struct ip6_tnl *t = NULL;
1187
1188 switch (cmd) {
1189 case SIOCGETTUNNEL:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001190 if (dev == ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001191 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 err = -EFAULT;
1193 break;
1194 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001195 t = ip6_tnl_locate(&p, 0);
Ville Nuorvala567131a2006-11-24 17:05:41 -08001196 }
1197 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -08001198 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 memcpy(&p, &t->parms, sizeof (p));
1200 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1201 err = -EFAULT;
1202 }
1203 break;
1204 case SIOCADDTUNNEL:
1205 case SIOCCHGTUNNEL:
1206 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (!capable(CAP_NET_ADMIN))
1208 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001209 err = -EFAULT;
1210 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001212 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001213 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1214 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 break;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001216 t = ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
1217 if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001218 if (t != NULL) {
1219 if (t->dev != dev) {
1220 err = -EEXIST;
1221 break;
1222 }
1223 } else
1224 t = netdev_priv(dev);
1225
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001226 ip6_tnl_unlink(t);
1227 err = ip6_tnl_change(t, &p);
1228 ip6_tnl_link(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 netdev_state_change(dev);
1230 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001231 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 err = 0;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001233 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1234 err = -EFAULT;
1235
1236 } else
1237 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 break;
1239 case SIOCDELTUNNEL:
1240 err = -EPERM;
1241 if (!capable(CAP_NET_ADMIN))
1242 break;
1243
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001244 if (dev == ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001245 err = -EFAULT;
1246 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001248 err = -ENOENT;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001249 if ((t = ip6_tnl_locate(&p, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001251 err = -EPERM;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001252 if (t->dev == ip6_fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001254 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001256 err = 0;
1257 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 break;
1259 default:
1260 err = -EINVAL;
1261 }
1262 return err;
1263}
1264
1265/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001266 * ip6_tnl_get_stats - return the stats for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 * @dev: virtual device associated with tunnel
1268 *
1269 * Return: stats for device
1270 **/
1271
1272static struct net_device_stats *
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001273ip6_tnl_get_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Patrick McHardy2941a482006-01-08 22:05:26 -08001275 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001279 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 * @dev: virtual device associated with tunnel
1281 * @new_mtu: the new mtu
1282 *
1283 * Return:
1284 * 0 on success,
1285 * %-EINVAL if mtu too small
1286 **/
1287
1288static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001289ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 if (new_mtu < IPV6_MIN_MTU) {
1292 return -EINVAL;
1293 }
1294 dev->mtu = new_mtu;
1295 return 0;
1296}
1297
1298/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001299 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 * @dev: virtual device associated with tunnel
1301 *
1302 * Description:
1303 * Initialize function pointers and device parameters
1304 **/
1305
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001306static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001308 dev->uninit = ip6_tnl_dev_uninit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 dev->destructor = free_netdev;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001310 dev->hard_start_xmit = ip6_tnl_xmit;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001311 dev->get_stats = ip6_tnl_get_stats;
1312 dev->do_ioctl = ip6_tnl_ioctl;
1313 dev->change_mtu = ip6_tnl_change_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 dev->type = ARPHRD_TUNNEL6;
1316 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1317 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1318 dev->flags |= IFF_NOARP;
1319 dev->addr_len = sizeof(struct in6_addr);
1320}
1321
1322
1323/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001324 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 * @dev: virtual device associated with tunnel
1326 **/
1327
1328static inline void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001329ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Patrick McHardy2941a482006-01-08 22:05:26 -08001331 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 t->dev = dev;
1333 strcpy(t->parms.name, dev->name);
1334}
1335
1336/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001337 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 * @dev: virtual device associated with tunnel
1339 **/
1340
1341static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001342ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Patrick McHardy2941a482006-01-08 22:05:26 -08001344 struct ip6_tnl *t = netdev_priv(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001345 ip6_tnl_dev_init_gen(dev);
1346 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return 0;
1348}
1349
1350/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001351 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 * @dev: fallback device
1353 *
1354 * Return: 0
1355 **/
1356
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001357static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001358ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Patrick McHardy2941a482006-01-08 22:05:26 -08001360 struct ip6_tnl *t = netdev_priv(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001361 ip6_tnl_dev_init_gen(dev);
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001362 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 dev_hold(dev);
1364 tnls_wc[0] = t;
1365 return 0;
1366}
1367
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001368static struct xfrm6_tunnel ip4ip6_handler = {
1369 .handler = ip4ip6_rcv,
1370 .err_handler = ip4ip6_err,
1371 .priority = 1,
1372};
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374static struct xfrm6_tunnel ip6ip6_handler = {
Patrick McHardy03037702005-07-19 14:03:34 -07001375 .handler = ip6ip6_rcv,
1376 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001377 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378};
1379
1380/**
1381 * ip6_tunnel_init - register protocol and reserve needed resources
1382 *
1383 * Return: 0 on success
1384 **/
1385
1386static int __init ip6_tunnel_init(void)
1387{
1388 int err;
1389
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001390 if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001391 printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001392 err = -EAGAIN;
1393 goto out;
1394 }
1395
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001396 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001397 printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001398 err = -EAGAIN;
1399 goto unreg_ip4ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001401 ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1402 ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001404 if (!ip6_fb_tnl_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 err = -ENOMEM;
1406 goto fail;
1407 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001408 ip6_fb_tnl_dev->init = ip6_fb_tnl_dev_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001410 if ((err = register_netdev(ip6_fb_tnl_dev))) {
1411 free_netdev(ip6_fb_tnl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 goto fail;
1413 }
1414 return 0;
1415fail:
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001416 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001417unreg_ip4ip6:
1418 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1419out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return err;
1421}
1422
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001423static void __exit ip6_tnl_destroy_tunnels(void)
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001424{
1425 int h;
1426 struct ip6_tnl *t;
1427
1428 for (h = 0; h < HASH_SIZE; h++) {
1429 while ((t = tnls_r_l[h]) != NULL)
1430 unregister_netdevice(t->dev);
1431 }
1432
1433 t = tnls_wc[0];
1434 unregister_netdevice(t->dev);
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/**
1438 * ip6_tunnel_cleanup - free resources and unregister protocol
1439 **/
1440
1441static void __exit ip6_tunnel_cleanup(void)
1442{
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001443 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001444 printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
Yasuyuki Kozakaic4d3efa2007-02-15 00:43:16 +09001445
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001446 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001447 printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001449 rtnl_lock();
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001450 ip6_tnl_destroy_tunnels();
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001451 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454module_init(ip6_tunnel_init);
1455module_exit(ip6_tunnel_cleanup);