blob: bb65779be7a6072e70d8ab32497d4fb08cea58a3 [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 Viroe69a4adc2006-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
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 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900244 dev = alloc_netdev(sizeof (*t), name, ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 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);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900249 dev->init = ip6_tnl_dev_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 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);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900257 ip6_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/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900264 * ip6_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:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900269 * ip6_tnl_locate() first tries to locate an existing tunnel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * 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
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900277static struct ip6_tnl *ip6_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
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900283 for (t = *ip6_tnl_bucket(p); t; t = t->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 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/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900294 * ip6_tnl_dev_uninit - tunnel device uninitializer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * @dev: the device to be destroyed
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900296 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900298 * ip6_tnl_dev_uninit() removes tunnel from its list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 **/
300
301static void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900302ip6_tnl_dev_uninit(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Patrick McHardy2941a482006-01-08 22:05:26 -0800304 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900306 if (dev == ip6_fb_tnl_dev) {
307 write_lock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 tnls_wc[0] = NULL;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900309 write_unlock_bh(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 } else {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900311 ip6_tnl_unlink(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
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 Kozakai502b0932006-11-30 14:43:28 +0900387ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900388 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
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900403 read_lock(&ip6_tnl_lock);
404 if ((t = ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 goto out;
406
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900407 if (t->parms.proto != ipproto && t->parms.proto != 0)
408 goto out;
409
Herbert Xud2acc342006-03-28 01:12:13 -0800410 err = 0;
411
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900412 switch (*type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 __u32 teli;
414 struct ipv6_tlv_tnl_enc_lim *tel;
415 __u32 mtu;
416 case ICMPV6_DEST_UNREACH:
417 if (net_ratelimit())
418 printk(KERN_WARNING
419 "%s: Path to destination invalid "
420 "or inactive!\n", t->parms.name);
421 rel_msg = 1;
422 break;
423 case ICMPV6_TIME_EXCEED:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900424 if ((*code) == ICMPV6_EXC_HOPLIMIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (net_ratelimit())
426 printk(KERN_WARNING
427 "%s: Too small hop limit or "
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900428 "routing loop in tunnel!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 t->parms.name);
430 rel_msg = 1;
431 }
432 break;
433 case ICMPV6_PARAMPROB:
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800434 teli = 0;
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900435 if ((*code) == ICMPV6_HDR_FIELD)
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800436 teli = parse_tlv_tnl_enc_lim(skb, skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900438 if (teli && teli == ntohl(*info) - 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli];
440 if (tel->encap_limit == 0) {
441 if (net_ratelimit())
442 printk(KERN_WARNING
443 "%s: Too small encapsulation "
444 "limit or routing loop in "
445 "tunnel!\n", t->parms.name);
446 rel_msg = 1;
447 }
Ville Nuorvala107a5fe2006-11-24 17:08:58 -0800448 } else if (net_ratelimit()) {
449 printk(KERN_WARNING
450 "%s: Recipient unable to parse tunneled "
451 "packet!\n ", t->parms.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 break;
454 case ICMPV6_PKT_TOOBIG:
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900455 mtu = ntohl(*info) - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (mtu < IPV6_MIN_MTU)
457 mtu = IPV6_MIN_MTU;
458 t->dev->mtu = mtu;
459
Al Virocc6cdac2006-02-18 16:02:18 -0500460 if ((len = sizeof (*ipv6h) + ntohs(ipv6h->payload_len)) > mtu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 rel_type = ICMPV6_PKT_TOOBIG;
462 rel_code = 0;
463 rel_info = mtu;
464 rel_msg = 1;
465 }
466 break;
467 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900468
469 *type = rel_type;
470 *code = rel_code;
471 *info = rel_info;
472 *msg = rel_msg;
473
474out:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900475 read_unlock(&ip6_tnl_lock);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900476 return err;
477}
478
479static int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900480ip4ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
481 int type, int code, int offset, __u32 info)
482{
483 int rel_msg = 0;
484 int rel_type = type;
485 int rel_code = code;
486 __u32 rel_info = info;
487 int err;
488 struct sk_buff *skb2;
489 struct iphdr *eiph;
490 struct flowi fl;
491 struct rtable *rt;
492
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900493 err = ip6_tnl_err(skb, IPPROTO_IPIP, opt, &rel_type, &rel_code,
494 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900495 if (err < 0)
496 return err;
497
498 if (rel_msg == 0)
499 return 0;
500
501 switch (rel_type) {
502 case ICMPV6_DEST_UNREACH:
503 if (rel_code != ICMPV6_ADDR_UNREACH)
504 return 0;
505 rel_type = ICMP_DEST_UNREACH;
506 rel_code = ICMP_HOST_UNREACH;
507 break;
508 case ICMPV6_PKT_TOOBIG:
509 if (rel_code != 0)
510 return 0;
511 rel_type = ICMP_DEST_UNREACH;
512 rel_code = ICMP_FRAG_NEEDED;
513 break;
514 default:
515 return 0;
516 }
517
518 if (!pskb_may_pull(skb, offset + sizeof(struct iphdr)))
519 return 0;
520
521 skb2 = skb_clone(skb, GFP_ATOMIC);
522 if (!skb2)
523 return 0;
524
525 dst_release(skb2->dst);
526 skb2->dst = NULL;
527 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700528 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700529 eiph = ip_hdr(skb2);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900530
531 /* Try to guess incoming interface */
532 memset(&fl, 0, sizeof(fl));
533 fl.fl4_dst = eiph->saddr;
534 fl.fl4_tos = RT_TOS(eiph->tos);
535 fl.proto = IPPROTO_IPIP;
536 if (ip_route_output_key(&rt, &fl))
537 goto out;
538
539 skb2->dev = rt->u.dst.dev;
540
541 /* route "incoming" packet */
542 if (rt->rt_flags & RTCF_LOCAL) {
543 ip_rt_put(rt);
544 rt = NULL;
545 fl.fl4_dst = eiph->daddr;
546 fl.fl4_src = eiph->saddr;
547 fl.fl4_tos = eiph->tos;
548 if (ip_route_output_key(&rt, &fl) ||
549 rt->u.dst.dev->type != ARPHRD_TUNNEL) {
550 ip_rt_put(rt);
551 goto out;
552 }
553 } else {
554 ip_rt_put(rt);
555 if (ip_route_input(skb2, eiph->daddr, eiph->saddr, eiph->tos,
556 skb2->dev) ||
557 skb2->dst->dev->type != ARPHRD_TUNNEL)
558 goto out;
559 }
560
561 /* change mtu on this route */
562 if (rel_type == ICMP_DEST_UNREACH && rel_code == ICMP_FRAG_NEEDED) {
563 if (rel_info > dst_mtu(skb2->dst))
564 goto out;
565
566 skb2->dst->ops->update_pmtu(skb2->dst, rel_info);
567 rel_info = htonl(rel_info);
568 }
569
570 icmp_send(skb2, rel_type, rel_code, rel_info);
571
572out:
573 kfree_skb(skb2);
574 return 0;
575}
576
577static int
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900578ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
579 int type, int code, int offset, __u32 info)
580{
581 int rel_msg = 0;
582 int rel_type = type;
583 int rel_code = code;
584 __u32 rel_info = info;
585 int err;
586
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900587 err = ip6_tnl_err(skb, IPPROTO_IPV6, opt, &rel_type, &rel_code,
588 &rel_msg, &rel_info, offset);
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900589 if (err < 0)
590 return err;
591
592 if (rel_msg && pskb_may_pull(skb, offset + sizeof(struct ipv6hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct rt6_info *rt;
594 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Ville Nuorvala305d4b32006-11-24 17:06:53 -0800595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (!skb2)
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 dst_release(skb2->dst);
600 skb2->dst = NULL;
601 skb_pull(skb2, offset);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700602 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* Try to guess incoming interface */
605 rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0);
606
607 if (rt && rt->rt6i_dev)
608 skb2->dev = rt->rt6i_dev;
609
610 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
611
612 if (rt)
613 dst_release(&rt->u.dst);
614
615 kfree_skb(skb2);
616 }
Yasuyuki Kozakaie490d1d2006-10-31 23:11:25 +0900617
618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900621static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
622 struct ipv6hdr *ipv6h,
623 struct sk_buff *skb)
624{
625 __u8 dsfield = ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK;
626
627 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700628 ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, dsfield);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900629
630 if (INET_ECN_is_ce(dsfield))
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700631 IP_ECN_set_ce(ip_hdr(skb));
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900632}
633
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900634static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl *t,
635 struct ipv6hdr *ipv6h,
636 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900638 if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY)
639 ipv6_copy_dscp(ipv6h, skb->nh.ipv6h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900641 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h)))
642 IP6_ECN_set_ce(skb->nh.ipv6h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900644
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800645static inline int ip6_tnl_rcv_ctl(struct ip6_tnl *t)
646{
647 struct ip6_tnl_parm *p = &t->parms;
648 int ret = 0;
649
650 if (p->flags & IP6_TNL_F_CAP_RCV) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900651 struct net_device *ldev = NULL;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800652
653 if (p->link)
654 ldev = dev_get_by_index(p->link);
655
656 if ((ipv6_addr_is_multicast(&p->laddr) ||
657 likely(ipv6_chk_addr(&p->laddr, ldev, 0))) &&
658 likely(!ipv6_chk_addr(&p->raddr, NULL, 0)))
659 ret = 1;
660
661 if (ldev)
662 dev_put(ldev);
663 }
664 return ret;
665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900668 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * @skb: received socket buffer
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900670 * @protocol: ethernet protocol ID
671 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 *
673 * Return: 0
674 **/
675
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900676static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900677 __u8 ipproto,
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900678 void (*dscp_ecn_decapsulate)(struct ip6_tnl *t,
679 struct ipv6hdr *ipv6h,
680 struct sk_buff *skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 struct ipv6hdr *ipv6h;
683 struct ip6_tnl *t;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ipv6h = skb->nh.ipv6h;
686
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900687 read_lock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900689 if ((t = ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) {
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900690 if (t->parms.proto != ipproto && t->parms.proto != 0) {
691 read_unlock(&ip6_tnl_lock);
692 goto discard;
693 }
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900696 read_unlock(&ip6_tnl_lock);
Herbert Xu50fba2a2006-04-04 13:50:45 -0700697 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800700 if (!ip6_tnl_rcv_ctl(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 t->stat.rx_dropped++;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900702 read_unlock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto discard;
704 }
705 secpath_reset(skb);
706 skb->mac.raw = skb->nh.raw;
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700707 skb_reset_network_header(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900708 skb->protocol = htons(protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 skb->pkt_type = PACKET_HOST;
710 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
711 skb->dev = t->dev;
712 dst_release(skb->dst);
713 skb->dst = NULL;
Yasuyuki Kozakai53ab61c2006-11-06 10:06:23 -0800714 nf_reset(skb);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900715
716 dscp_ecn_decapsulate(t, ipv6h, skb);
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 t->stat.rx_packets++;
719 t->stat.rx_bytes += skb->len;
720 netif_rx(skb);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900721 read_unlock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900724 read_unlock(&ip6_tnl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 1;
Herbert Xu50fba2a2006-04-04 13:50:45 -0700726
727discard:
728 kfree_skb(skb);
729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900732static int ip4ip6_rcv(struct sk_buff *skb)
733{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900734 return ip6_tnl_rcv(skb, ETH_P_IP, IPPROTO_IPIP,
735 ip4ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900736}
737
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900738static int ip6ip6_rcv(struct sk_buff *skb)
739{
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900740 return ip6_tnl_rcv(skb, ETH_P_IPV6, IPPROTO_IPV6,
741 ip6ip6_dscp_ecn_decapsulate);
Yasuyuki Kozakai83599252006-11-03 09:39:14 +0900742}
743
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800744struct ipv6_tel_txoption {
745 struct ipv6_txoptions ops;
746 __u8 dst_opt[8];
747};
748
749static void init_tel_txopt(struct ipv6_tel_txoption *opt, __u8 encap_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800751 memset(opt, 0, sizeof(struct ipv6_tel_txoption));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800753 opt->dst_opt[2] = IPV6_TLV_TNL_ENCAP_LIMIT;
754 opt->dst_opt[3] = 1;
755 opt->dst_opt[4] = encap_limit;
756 opt->dst_opt[5] = IPV6_TLV_PADN;
757 opt->dst_opt[6] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800759 opt->ops.dst0opt = (struct ipv6_opt_hdr *) opt->dst_opt;
760 opt->ops.opt_nflen = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900764 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * @t: the outgoing tunnel device
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900766 * @hdr: IPv6 header from the incoming packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *
768 * Description:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900769 * Avoid trivial tunneling loop by checking that tunnel exit-point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * doesn't match source of incoming packet.
771 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900772 * Return:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * 1 if conflict,
774 * 0 else
775 **/
776
777static inline int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +0900778ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
781}
782
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800783static inline int ip6_tnl_xmit_ctl(struct ip6_tnl *t)
784{
785 struct ip6_tnl_parm *p = &t->parms;
786 int ret = 0;
787
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900788 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -0800789 struct net_device *ldev = NULL;
790
791 if (p->link)
792 ldev = dev_get_by_index(p->link);
793
794 if (unlikely(!ipv6_chk_addr(&p->laddr, ldev, 0)))
795 printk(KERN_WARNING
796 "%s xmit: Local address not yet configured!\n",
797 p->name);
798 else if (!ipv6_addr_is_multicast(&p->raddr) &&
799 unlikely(ipv6_chk_addr(&p->raddr, NULL, 0)))
800 printk(KERN_WARNING
801 "%s xmit: Routing loop! "
802 "Remote address found on this node!\n",
803 p->name);
804 else
805 ret = 1;
806 if (ldev)
807 dev_put(ldev);
808 }
809 return ret;
810}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/**
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900812 * ip6_tnl_xmit2 - encapsulate packet and send
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 * @skb: the outgoing socket buffer
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900814 * @dev: the outgoing tunnel device
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900815 * @dsfield: dscp code for outer header
816 * @fl: flow of tunneled packet
817 * @encap_limit: encapsulation limit
818 * @pmtu: Path MTU is stored if packet is too big
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
820 * Description:
821 * Build new header and do some sanity checks on the packet before sending
822 * it.
823 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900824 * Return:
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900825 * 0 on success
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900826 * -1 fail
827 * %-EMSGSIZE message too big. return mtu in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 **/
829
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900830static int ip6_tnl_xmit2(struct sk_buff *skb,
831 struct net_device *dev,
832 __u8 dsfield,
833 struct flowi *fl,
834 int encap_limit,
835 __u32 *pmtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Patrick McHardy2941a482006-01-08 22:05:26 -0800837 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct net_device_stats *stats = &t->stat;
839 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800840 struct ipv6_tel_txoption opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct dst_entry *dst;
842 struct net_device *tdev;
843 int mtu;
844 int max_headroom = sizeof(struct ipv6hdr);
845 u8 proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900846 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if ((dst = ip6_tnl_dst_check(t)) != NULL)
850 dst_hold(dst);
Patrick McHardya57ebc92005-09-08 14:27:47 -0700851 else {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900852 dst = ip6_route_output(NULL, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900854 if (dst->error || xfrm_lookup(&dst, fl, NULL, 0) < 0)
Patrick McHardya57ebc92005-09-08 14:27:47 -0700855 goto tx_err_link_failure;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 tdev = dst->dev;
859
860 if (tdev == dev) {
861 stats->collisions++;
862 if (net_ratelimit())
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900863 printk(KERN_WARNING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 "%s: Local routing loop detected!\n",
865 t->parms.name);
866 goto tx_err_dst_release;
867 }
868 mtu = dst_mtu(dst) - sizeof (*ipv6h);
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800869 if (encap_limit >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 max_headroom += 8;
871 mtu -= 8;
872 }
873 if (mtu < IPV6_MIN_MTU)
874 mtu = IPV6_MIN_MTU;
Yasuyuki Kozakai26892052006-09-10 03:59:17 +0900875 if (skb->dst)
876 skb->dst->ops->update_pmtu(skb->dst, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (skb->len > mtu) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900878 *pmtu = mtu;
879 err = -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 goto tx_err_dst_release;
881 }
882
883 /*
884 * Okay, now see if we can stuff it in the buffer as-is.
885 */
886 max_headroom += LL_RESERVED_SPACE(tdev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900887
888 if (skb_headroom(skb) < max_headroom ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 skb_cloned(skb) || skb_shared(skb)) {
890 struct sk_buff *new_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (!(new_skb = skb_realloc_headroom(skb, max_headroom)))
893 goto tx_err_dst_release;
894
895 if (skb->sk)
896 skb_set_owner_w(new_skb, skb->sk);
897 kfree_skb(skb);
898 skb = new_skb;
899 }
900 dst_release(skb->dst);
901 skb->dst = dst_clone(dst);
902
903 skb->h.raw = skb->nh.raw;
904
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900905 proto = fl->proto;
Ville Nuorvala6fb32dd2006-11-24 17:08:32 -0800906 if (encap_limit >= 0) {
907 init_tel_txopt(&opt, encap_limit);
908 ipv6_push_nfrag_opts(skb, &opt.ops, &proto, NULL);
909 }
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700910 skb_push(skb, sizeof(struct ipv6hdr));
911 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 ipv6h = skb->nh.ipv6h;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900913 *(__be32*)ipv6h = fl->fl6_flowlabel | htonl(0x60000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 dsfield = INET_ECN_encapsulate(0, dsfield);
915 ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield);
916 ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
917 ipv6h->hop_limit = t->parms.hop_limit;
918 ipv6h->nexthdr = proto;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900919 ipv6_addr_copy(&ipv6h->saddr, &fl->fl6_src);
920 ipv6_addr_copy(&ipv6h->daddr, &fl->fl6_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 nf_reset(skb);
922 pkt_len = skb->len;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900923 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 skb->dst->dev, dst_output);
925
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200926 if (net_xmit_eval(err) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 stats->tx_bytes += pkt_len;
928 stats->tx_packets++;
929 } else {
930 stats->tx_errors++;
931 stats->tx_aborted_errors++;
932 }
933 ip6_tnl_dst_store(t, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935tx_err_link_failure:
936 stats->tx_carrier_errors++;
937 dst_link_failure(skb);
938tx_err_dst_release:
939 dst_release(dst);
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900940 return err;
941}
942
943static inline int
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900944ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
945{
946 struct ip6_tnl *t = netdev_priv(dev);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700947 struct iphdr *iph = ip_hdr(skb);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900948 int encap_limit = -1;
949 struct flowi fl;
950 __u8 dsfield;
951 __u32 mtu;
952 int err;
953
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900954 if ((t->parms.proto != IPPROTO_IPIP && t->parms.proto != 0) ||
955 !ip6_tnl_xmit_ctl(t))
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +0900956 return -1;
957
958 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
959 encap_limit = t->parms.encap_limit;
960
961 memcpy(&fl, &t->fl, sizeof (fl));
962 fl.proto = IPPROTO_IPIP;
963
964 dsfield = ipv4_get_dsfield(iph);
965
966 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
967 fl.fl6_flowlabel |= ntohl(((__u32)iph->tos << IPV6_TCLASS_SHIFT)
968 & IPV6_TCLASS_MASK);
969
970 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
971 if (err != 0) {
972 /* XXX: send ICMP error even if DF is not set. */
973 if (err == -EMSGSIZE)
974 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
975 htonl(mtu));
976 return -1;
977 }
978
979 return 0;
980}
981
982static inline int
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900983ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
984{
985 struct ip6_tnl *t = netdev_priv(dev);
986 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
987 int encap_limit = -1;
988 __u16 offset;
989 struct flowi fl;
990 __u8 dsfield;
991 __u32 mtu;
992 int err;
993
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +0900994 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
995 !ip6_tnl_xmit_ctl(t) || ip6_tnl_addr_conflict(t, ipv6h))
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +0900996 return -1;
997
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700998 offset = parse_tlv_tnl_enc_lim(skb, skb_network_header(skb));
999 if (offset > 0) {
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001000 struct ipv6_tlv_tnl_enc_lim *tel;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001001 tel = (struct ipv6_tlv_tnl_enc_lim *)&skb_network_header(skb)[offset];
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001002 if (tel->encap_limit == 0) {
1003 icmpv6_send(skb, ICMPV6_PARAMPROB,
1004 ICMPV6_HDR_FIELD, offset + 2, skb->dev);
1005 return -1;
1006 }
1007 encap_limit = tel->encap_limit - 1;
1008 } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1009 encap_limit = t->parms.encap_limit;
1010
1011 memcpy(&fl, &t->fl, sizeof (fl));
1012 fl.proto = IPPROTO_IPV6;
1013
1014 dsfield = ipv6_get_dsfield(ipv6h);
1015 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS))
1016 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_TCLASS_MASK);
1017 if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL))
1018 fl.fl6_flowlabel |= (*(__be32 *) ipv6h & IPV6_FLOWLABEL_MASK);
1019
1020 err = ip6_tnl_xmit2(skb, dev, dsfield, &fl, encap_limit, &mtu);
1021 if (err != 0) {
1022 if (err == -EMSGSIZE)
1023 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
1024 return -1;
1025 }
1026
1027 return 0;
1028}
1029
1030static int
1031ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
1032{
1033 struct ip6_tnl *t = netdev_priv(dev);
1034 struct net_device_stats *stats = &t->stat;
1035 int ret;
1036
1037 if (t->recursion++) {
1038 t->stat.collisions++;
1039 goto tx_err;
1040 }
1041
1042 switch (skb->protocol) {
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001043 case __constant_htons(ETH_P_IP):
1044 ret = ip4ip6_tnl_xmit(skb, dev);
1045 break;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001046 case __constant_htons(ETH_P_IPV6):
1047 ret = ip6ip6_tnl_xmit(skb, dev);
1048 break;
1049 default:
1050 goto tx_err;
1051 }
1052
1053 if (ret < 0)
1054 goto tx_err;
1055
1056 t->recursion--;
1057 return 0;
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059tx_err:
1060 stats->tx_errors++;
1061 stats->tx_dropped++;
1062 kfree_skb(skb);
1063 t->recursion--;
1064 return 0;
1065}
1066
1067static void ip6_tnl_set_cap(struct ip6_tnl *t)
1068{
1069 struct ip6_tnl_parm *p = &t->parms;
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001070 int ltype = ipv6_addr_type(&p->laddr);
1071 int rtype = ipv6_addr_type(&p->raddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV);
1074
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001075 if (ltype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1076 rtype & (IPV6_ADDR_UNICAST|IPV6_ADDR_MULTICAST) &&
1077 !((ltype|rtype) & IPV6_ADDR_LOOPBACK) &&
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001078 (!((ltype|rtype) & IPV6_ADDR_LINKLOCAL) || p->link)) {
Ville Nuorvala09c6bbf2006-11-24 17:06:27 -08001079 if (ltype&IPV6_ADDR_UNICAST)
1080 p->flags |= IP6_TNL_F_CAP_XMIT;
1081 if (rtype&IPV6_ADDR_UNICAST)
1082 p->flags |= IP6_TNL_F_CAP_RCV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084}
1085
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001086static void ip6_tnl_link_config(struct ip6_tnl *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 struct net_device *dev = t->dev;
1089 struct ip6_tnl_parm *p = &t->parms;
1090 struct flowi *fl = &t->fl;
1091
1092 memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
1093 memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr));
1094
1095 /* Set up flowi template */
1096 ipv6_addr_copy(&fl->fl6_src, &p->laddr);
1097 ipv6_addr_copy(&fl->fl6_dst, &p->raddr);
1098 fl->oif = p->link;
1099 fl->fl6_flowlabel = 0;
1100
1101 if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS))
1102 fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo;
1103 if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL))
1104 fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo;
1105
1106 ip6_tnl_set_cap(t);
1107
1108 if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV)
1109 dev->flags |= IFF_POINTOPOINT;
1110 else
1111 dev->flags &= ~IFF_POINTOPOINT;
1112
1113 dev->iflink = p->link;
1114
1115 if (p->flags & IP6_TNL_F_CAP_XMIT) {
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001116 int strict = (ipv6_addr_type(&p->raddr) &
1117 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr,
Ville Nuorvala305d4b32006-11-24 17:06:53 -08001120 p->link, strict);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 if (rt == NULL)
1123 return;
1124
1125 if (rt->rt6i_dev) {
1126 dev->hard_header_len = rt->rt6i_dev->hard_header_len +
1127 sizeof (struct ipv6hdr);
1128
1129 dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr);
1130
1131 if (dev->mtu < IPV6_MIN_MTU)
1132 dev->mtu = IPV6_MIN_MTU;
1133 }
1134 dst_release(&rt->u.dst);
1135 }
1136}
1137
1138/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001139 * ip6_tnl_change - update the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * @t: tunnel to be changed
1141 * @p: tunnel configuration parameters
1142 * @active: != 0 if tunnel is ready for use
1143 *
1144 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001145 * ip6_tnl_change() updates the tunnel parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 **/
1147
1148static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001149ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 ipv6_addr_copy(&t->parms.laddr, &p->laddr);
1152 ipv6_addr_copy(&t->parms.raddr, &p->raddr);
1153 t->parms.flags = p->flags;
1154 t->parms.hop_limit = p->hop_limit;
1155 t->parms.encap_limit = p->encap_limit;
1156 t->parms.flowinfo = p->flowinfo;
Gabor Fekete8181b8c2005-06-08 14:54:38 -07001157 t->parms.link = p->link;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001158 t->parms.proto = p->proto;
Hugo Santos0c088892006-02-24 13:16:25 -08001159 ip6_tnl_dst_reset(t);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001160 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162}
1163
1164/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001165 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * @dev: virtual device associated with tunnel
1167 * @ifr: parameters passed from userspace
1168 * @cmd: command to be performed
1169 *
1170 * Description:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001171 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001172 * from userspace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 *
1174 * The possible commands are the following:
1175 * %SIOCGETTUNNEL: get tunnel parameters for device
1176 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1177 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1178 * %SIOCDELTUNNEL: delete tunnel
1179 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001180 * The fallback device "ip6tnl0", created during module
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 * initialization, can be used for creating other tunnel devices.
1182 *
1183 * Return:
1184 * 0 on success,
1185 * %-EFAULT if unable to copy data to or from userspace,
1186 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1187 * %-EINVAL if passed tunnel parameters are invalid,
1188 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1189 * %-ENODEV if attempting to change or delete a nonexisting device
1190 **/
1191
1192static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001193ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct ip6_tnl_parm p;
1197 struct ip6_tnl *t = NULL;
1198
1199 switch (cmd) {
1200 case SIOCGETTUNNEL:
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001201 if (dev == ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001202 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 err = -EFAULT;
1204 break;
1205 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001206 t = ip6_tnl_locate(&p, 0);
Ville Nuorvala567131a2006-11-24 17:05:41 -08001207 }
1208 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -08001209 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 memcpy(&p, &t->parms, sizeof (p));
1211 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) {
1212 err = -EFAULT;
1213 }
1214 break;
1215 case SIOCADDTUNNEL:
1216 case SIOCCHGTUNNEL:
1217 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (!capable(CAP_NET_ADMIN))
1219 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001220 err = -EFAULT;
1221 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001223 err = -EINVAL;
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001224 if (p.proto != IPPROTO_IPV6 && p.proto != IPPROTO_IPIP &&
1225 p.proto != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 break;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001227 t = ip6_tnl_locate(&p, cmd == SIOCADDTUNNEL);
1228 if (dev != ip6_fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001229 if (t != NULL) {
1230 if (t->dev != dev) {
1231 err = -EEXIST;
1232 break;
1233 }
1234 } else
1235 t = netdev_priv(dev);
1236
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001237 ip6_tnl_unlink(t);
1238 err = ip6_tnl_change(t, &p);
1239 ip6_tnl_link(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 netdev_state_change(dev);
1241 }
Ville Nuorvala567131a2006-11-24 17:05:41 -08001242 if (t) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 err = 0;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001244 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof (p)))
1245 err = -EFAULT;
1246
1247 } else
1248 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 break;
1250 case SIOCDELTUNNEL:
1251 err = -EPERM;
1252 if (!capable(CAP_NET_ADMIN))
1253 break;
1254
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001255 if (dev == ip6_fb_tnl_dev) {
Ville Nuorvala567131a2006-11-24 17:05:41 -08001256 err = -EFAULT;
1257 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001259 err = -ENOENT;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001260 if ((t = ip6_tnl_locate(&p, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001262 err = -EPERM;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001263 if (t->dev == ip6_fb_tnl_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 break;
Ville Nuorvala567131a2006-11-24 17:05:41 -08001265 dev = t->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Stephen Hemminger22f8cde2007-02-07 00:09:58 -08001267 err = 0;
1268 unregister_netdevice(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 break;
1270 default:
1271 err = -EINVAL;
1272 }
1273 return err;
1274}
1275
1276/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001277 * ip6_tnl_get_stats - return the stats for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 * @dev: virtual device associated with tunnel
1279 *
1280 * Return: stats for device
1281 **/
1282
1283static struct net_device_stats *
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001284ip6_tnl_get_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Patrick McHardy2941a482006-01-08 22:05:26 -08001286 return &(((struct ip6_tnl *)netdev_priv(dev))->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001290 * ip6_tnl_change_mtu - change mtu manually for tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 * @dev: virtual device associated with tunnel
1292 * @new_mtu: the new mtu
1293 *
1294 * Return:
1295 * 0 on success,
1296 * %-EINVAL if mtu too small
1297 **/
1298
1299static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001300ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 if (new_mtu < IPV6_MIN_MTU) {
1303 return -EINVAL;
1304 }
1305 dev->mtu = new_mtu;
1306 return 0;
1307}
1308
1309/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001310 * ip6_tnl_dev_setup - setup virtual tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * @dev: virtual device associated with tunnel
1312 *
1313 * Description:
1314 * Initialize function pointers and device parameters
1315 **/
1316
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001317static void ip6_tnl_dev_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
1319 SET_MODULE_OWNER(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001320 dev->uninit = ip6_tnl_dev_uninit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 dev->destructor = free_netdev;
Yasuyuki Kozakai61ec2ae2006-11-05 22:56:45 +09001322 dev->hard_start_xmit = ip6_tnl_xmit;
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001323 dev->get_stats = ip6_tnl_get_stats;
1324 dev->do_ioctl = ip6_tnl_ioctl;
1325 dev->change_mtu = ip6_tnl_change_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 dev->type = ARPHRD_TUNNEL6;
1328 dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr);
1329 dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr);
1330 dev->flags |= IFF_NOARP;
1331 dev->addr_len = sizeof(struct in6_addr);
1332}
1333
1334
1335/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001336 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * @dev: virtual device associated with tunnel
1338 **/
1339
1340static inline void
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001341ip6_tnl_dev_init_gen(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Patrick McHardy2941a482006-01-08 22:05:26 -08001343 struct ip6_tnl *t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 t->dev = dev;
1345 strcpy(t->parms.name, dev->name);
1346}
1347
1348/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001349 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * @dev: virtual device associated with tunnel
1351 **/
1352
1353static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001354ip6_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Patrick McHardy2941a482006-01-08 22:05:26 -08001356 struct ip6_tnl *t = netdev_priv(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001357 ip6_tnl_dev_init_gen(dev);
1358 ip6_tnl_link_config(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return 0;
1360}
1361
1362/**
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001363 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * @dev: fallback device
1365 *
1366 * Return: 0
1367 **/
1368
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001369static int
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001370ip6_fb_tnl_dev_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Patrick McHardy2941a482006-01-08 22:05:26 -08001372 struct ip6_tnl *t = netdev_priv(dev);
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001373 ip6_tnl_dev_init_gen(dev);
Yasuyuki Kozakai502b0932006-11-30 14:43:28 +09001374 t->parms.proto = IPPROTO_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 dev_hold(dev);
1376 tnls_wc[0] = t;
1377 return 0;
1378}
1379
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001380static struct xfrm6_tunnel ip4ip6_handler = {
1381 .handler = ip4ip6_rcv,
1382 .err_handler = ip4ip6_err,
1383 .priority = 1,
1384};
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386static struct xfrm6_tunnel ip6ip6_handler = {
Patrick McHardy03037702005-07-19 14:03:34 -07001387 .handler = ip6ip6_rcv,
1388 .err_handler = ip6ip6_err,
Herbert Xud2acc342006-03-28 01:12:13 -08001389 .priority = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390};
1391
1392/**
1393 * ip6_tunnel_init - register protocol and reserve needed resources
1394 *
1395 * Return: 0 on success
1396 **/
1397
1398static int __init ip6_tunnel_init(void)
1399{
1400 int err;
1401
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001402 if (xfrm6_tunnel_register(&ip4ip6_handler, AF_INET)) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001403 printk(KERN_ERR "ip6_tunnel init: can't register ip4ip6\n");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001404 err = -EAGAIN;
1405 goto out;
1406 }
1407
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001408 if (xfrm6_tunnel_register(&ip6ip6_handler, AF_INET6)) {
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001409 printk(KERN_ERR "ip6_tunnel init: can't register ip6ip6\n");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001410 err = -EAGAIN;
1411 goto unreg_ip4ip6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001413 ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0",
1414 ip6_tnl_dev_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001416 if (!ip6_fb_tnl_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 err = -ENOMEM;
1418 goto fail;
1419 }
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001420 ip6_fb_tnl_dev->init = ip6_fb_tnl_dev_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001422 if ((err = register_netdev(ip6_fb_tnl_dev))) {
1423 free_netdev(ip6_fb_tnl_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 goto fail;
1425 }
1426 return 0;
1427fail:
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001428 xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6);
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001429unreg_ip4ip6:
1430 xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET);
1431out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return err;
1433}
1434
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001435static void __exit ip6_tnl_destroy_tunnels(void)
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001436{
1437 int h;
1438 struct ip6_tnl *t;
1439
1440 for (h = 0; h < HASH_SIZE; h++) {
1441 while ((t = tnls_r_l[h]) != NULL)
1442 unregister_netdevice(t->dev);
1443 }
1444
1445 t = tnls_wc[0];
1446 unregister_netdevice(t->dev);
1447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/**
1450 * ip6_tunnel_cleanup - free resources and unregister protocol
1451 **/
1452
1453static void __exit ip6_tunnel_cleanup(void)
1454{
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001455 if (xfrm6_tunnel_deregister(&ip4ip6_handler, AF_INET))
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001456 printk(KERN_INFO "ip6_tunnel close: can't deregister ip4ip6\n");
Yasuyuki Kozakaic4d3efaf2007-02-15 00:43:16 +09001457
Kazunori MIYAZAWA73d605d2007-02-13 12:55:55 -08001458 if (xfrm6_tunnel_deregister(&ip6ip6_handler, AF_INET6))
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001459 printk(KERN_INFO "ip6_tunnel close: can't deregister ip6ip6\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001461 rtnl_lock();
Yasuyuki Kozakai31445812007-02-10 00:30:33 +09001462 ip6_tnl_destroy_tunnels();
Yasuyuki Kozakaib3fdd9f2006-11-06 10:06:22 -08001463 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466module_init(ip6_tunnel_init);
1467module_exit(ip6_tunnel_cleanup);