blob: 6578c3080f47738fb1a2a4d35e4320c2c4d285a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
9 * $Id: sit.c,v 1.53 2001/09/25 05:09:53 davem Exp $
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 * Changes:
17 * Roger Venning <r.venning@telstra.com>: 6to4 support
18 * Nate Thompson <nate@thebog.net>: 6to4 support
19 */
20
21#include <linux/config.h>
22#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080023#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
28#include <linux/sched.h>
29#include <linux/net.h>
30#include <linux/in6.h>
31#include <linux/netdevice.h>
32#include <linux/if_arp.h>
33#include <linux/icmp.h>
34#include <asm/uaccess.h>
35#include <linux/init.h>
36#include <linux/netfilter_ipv4.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080037#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <net/sock.h>
40#include <net/snmp.h>
41
42#include <net/ipv6.h>
43#include <net/protocol.h>
44#include <net/transp_v6.h>
45#include <net/ip6_fib.h>
46#include <net/ip6_route.h>
47#include <net/ndisc.h>
48#include <net/addrconf.h>
49#include <net/ip.h>
50#include <net/udp.h>
51#include <net/icmp.h>
52#include <net/ipip.h>
53#include <net/inet_ecn.h>
54#include <net/xfrm.h>
55#include <net/dsfield.h>
56
57/*
58 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
59
60 For comments look at net/ipv4/ip_gre.c --ANK
61 */
62
63#define HASH_SIZE 16
64#define HASH(addr) ((addr^(addr>>4))&0xF)
65
66static int ipip6_fb_tunnel_init(struct net_device *dev);
67static int ipip6_tunnel_init(struct net_device *dev);
68static void ipip6_tunnel_setup(struct net_device *dev);
69
70static struct net_device *ipip6_fb_tunnel_dev;
71
72static struct ip_tunnel *tunnels_r_l[HASH_SIZE];
73static struct ip_tunnel *tunnels_r[HASH_SIZE];
74static struct ip_tunnel *tunnels_l[HASH_SIZE];
75static struct ip_tunnel *tunnels_wc[1];
76static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
77
78static DEFINE_RWLOCK(ipip6_lock);
79
80static struct ip_tunnel * ipip6_tunnel_lookup(u32 remote, u32 local)
81{
82 unsigned h0 = HASH(remote);
83 unsigned h1 = HASH(local);
84 struct ip_tunnel *t;
85
86 for (t = tunnels_r_l[h0^h1]; t; t = t->next) {
87 if (local == t->parms.iph.saddr &&
88 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
89 return t;
90 }
91 for (t = tunnels_r[h0]; t; t = t->next) {
92 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
93 return t;
94 }
95 for (t = tunnels_l[h1]; t; t = t->next) {
96 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
97 return t;
98 }
99 if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
100 return t;
101 return NULL;
102}
103
104static struct ip_tunnel ** ipip6_bucket(struct ip_tunnel *t)
105{
106 u32 remote = t->parms.iph.daddr;
107 u32 local = t->parms.iph.saddr;
108 unsigned h = 0;
109 int prio = 0;
110
111 if (remote) {
112 prio |= 2;
113 h ^= HASH(remote);
114 }
115 if (local) {
116 prio |= 1;
117 h ^= HASH(local);
118 }
119 return &tunnels[prio][h];
120}
121
122static void ipip6_tunnel_unlink(struct ip_tunnel *t)
123{
124 struct ip_tunnel **tp;
125
126 for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) {
127 if (t == *tp) {
128 write_lock_bh(&ipip6_lock);
129 *tp = t->next;
130 write_unlock_bh(&ipip6_lock);
131 break;
132 }
133 }
134}
135
136static void ipip6_tunnel_link(struct ip_tunnel *t)
137{
138 struct ip_tunnel **tp = ipip6_bucket(t);
139
140 t->next = *tp;
141 write_lock_bh(&ipip6_lock);
142 *tp = t;
143 write_unlock_bh(&ipip6_lock);
144}
145
146static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create)
147{
148 u32 remote = parms->iph.daddr;
149 u32 local = parms->iph.saddr;
150 struct ip_tunnel *t, **tp, *nt;
151 struct net_device *dev;
152 unsigned h = 0;
153 int prio = 0;
154 char name[IFNAMSIZ];
155
156 if (remote) {
157 prio |= 2;
158 h ^= HASH(remote);
159 }
160 if (local) {
161 prio |= 1;
162 h ^= HASH(local);
163 }
164 for (tp = &tunnels[prio][h]; (t = *tp) != NULL; tp = &t->next) {
165 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
166 return t;
167 }
168 if (!create)
169 goto failed;
170
171 if (parms->name[0])
172 strlcpy(name, parms->name, IFNAMSIZ);
173 else {
174 int i;
175 for (i=1; i<100; i++) {
176 sprintf(name, "sit%d", i);
177 if (__dev_get_by_name(name) == NULL)
178 break;
179 }
180 if (i==100)
181 goto failed;
182 }
183
184 dev = alloc_netdev(sizeof(*t), name, ipip6_tunnel_setup);
185 if (dev == NULL)
186 return NULL;
187
Patrick McHardy2941a482006-01-08 22:05:26 -0800188 nt = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 dev->init = ipip6_tunnel_init;
190 nt->parms = *parms;
191
192 if (register_netdevice(dev) < 0) {
193 free_netdev(dev);
194 goto failed;
195 }
196
197 dev_hold(dev);
198
199 ipip6_tunnel_link(nt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return nt;
201
202failed:
203 return NULL;
204}
205
206static void ipip6_tunnel_uninit(struct net_device *dev)
207{
208 if (dev == ipip6_fb_tunnel_dev) {
209 write_lock_bh(&ipip6_lock);
210 tunnels_wc[0] = NULL;
211 write_unlock_bh(&ipip6_lock);
212 dev_put(dev);
213 } else {
Patrick McHardy2941a482006-01-08 22:05:26 -0800214 ipip6_tunnel_unlink(netdev_priv(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dev_put(dev);
216 }
217}
218
219
220static void ipip6_err(struct sk_buff *skb, u32 info)
221{
222#ifndef I_WISH_WORLD_WERE_PERFECT
223
224/* It is not :-( All the routers (except for Linux) return only
225 8 bytes of packet payload. It means, that precise relaying of
226 ICMP in the real Internet is absolutely infeasible.
227 */
228 struct iphdr *iph = (struct iphdr*)skb->data;
229 int type = skb->h.icmph->type;
230 int code = skb->h.icmph->code;
231 struct ip_tunnel *t;
232
233 switch (type) {
234 default:
235 case ICMP_PARAMETERPROB:
236 return;
237
238 case ICMP_DEST_UNREACH:
239 switch (code) {
240 case ICMP_SR_FAILED:
241 case ICMP_PORT_UNREACH:
242 /* Impossible event. */
243 return;
244 case ICMP_FRAG_NEEDED:
245 /* Soft state for pmtu is maintained by IP core. */
246 return;
247 default:
248 /* All others are translated to HOST_UNREACH.
249 rfc2003 contains "deep thoughts" about NET_UNREACH,
250 I believe they are just ether pollution. --ANK
251 */
252 break;
253 }
254 break;
255 case ICMP_TIME_EXCEEDED:
256 if (code != ICMP_EXC_TTL)
257 return;
258 break;
259 }
260
261 read_lock(&ipip6_lock);
262 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr);
263 if (t == NULL || t->parms.iph.daddr == 0)
264 goto out;
265 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
266 goto out;
267
268 if (jiffies - t->err_time < IPTUNNEL_ERR_TIMEO)
269 t->err_count++;
270 else
271 t->err_count = 1;
272 t->err_time = jiffies;
273out:
274 read_unlock(&ipip6_lock);
275 return;
276#else
277 struct iphdr *iph = (struct iphdr*)dp;
278 int hlen = iph->ihl<<2;
279 struct ipv6hdr *iph6;
280 int type = skb->h.icmph->type;
281 int code = skb->h.icmph->code;
282 int rel_type = 0;
283 int rel_code = 0;
284 int rel_info = 0;
285 struct sk_buff *skb2;
286 struct rt6_info *rt6i;
287
288 if (len < hlen + sizeof(struct ipv6hdr))
289 return;
290 iph6 = (struct ipv6hdr*)(dp + hlen);
291
292 switch (type) {
293 default:
294 return;
295 case ICMP_PARAMETERPROB:
296 if (skb->h.icmph->un.gateway < hlen)
297 return;
298
299 /* So... This guy found something strange INSIDE encapsulated
300 packet. Well, he is fool, but what can we do ?
301 */
302 rel_type = ICMPV6_PARAMPROB;
303 rel_info = skb->h.icmph->un.gateway - hlen;
304 break;
305
306 case ICMP_DEST_UNREACH:
307 switch (code) {
308 case ICMP_SR_FAILED:
309 case ICMP_PORT_UNREACH:
310 /* Impossible event. */
311 return;
312 case ICMP_FRAG_NEEDED:
313 /* Too complicated case ... */
314 return;
315 default:
316 /* All others are translated to HOST_UNREACH.
317 rfc2003 contains "deep thoughts" about NET_UNREACH,
318 I believe, it is just ether pollution. --ANK
319 */
320 rel_type = ICMPV6_DEST_UNREACH;
321 rel_code = ICMPV6_ADDR_UNREACH;
322 break;
323 }
324 break;
325 case ICMP_TIME_EXCEEDED:
326 if (code != ICMP_EXC_TTL)
327 return;
328 rel_type = ICMPV6_TIME_EXCEED;
329 rel_code = ICMPV6_EXC_HOPLIMIT;
330 break;
331 }
332
333 /* Prepare fake skb to feed it to icmpv6_send */
334 skb2 = skb_clone(skb, GFP_ATOMIC);
335 if (skb2 == NULL)
336 return;
337 dst_release(skb2->dst);
338 skb2->dst = NULL;
339 skb_pull(skb2, skb->data - (u8*)iph6);
340 skb2->nh.raw = skb2->data;
341
342 /* Try to guess incoming interface */
343 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0);
344 if (rt6i && rt6i->rt6i_dev) {
345 skb2->dev = rt6i->rt6i_dev;
346
347 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0);
348
349 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
Patrick McHardy2941a482006-01-08 22:05:26 -0800350 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (rel_type == ICMPV6_TIME_EXCEED && t->parms.iph.ttl) {
352 rel_type = ICMPV6_DEST_UNREACH;
353 rel_code = ICMPV6_ADDR_UNREACH;
354 }
355 icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev);
356 }
357 }
358 kfree_skb(skb2);
359 return;
360#endif
361}
362
363static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
364{
365 if (INET_ECN_is_ce(iph->tos))
366 IP6_ECN_set_ce(skb->nh.ipv6h);
367}
368
369static int ipip6_rcv(struct sk_buff *skb)
370{
371 struct iphdr *iph;
372 struct ip_tunnel *tunnel;
373
374 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
375 goto out;
376
377 iph = skb->nh.iph;
378
379 read_lock(&ipip6_lock);
380 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) {
381 secpath_reset(skb);
382 skb->mac.raw = skb->nh.raw;
383 skb->nh.raw = skb->data;
384 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
Patrick McHardy8cdfab82006-01-06 23:04:01 -0800385 IPCB(skb)->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 skb->protocol = htons(ETH_P_IPV6);
387 skb->pkt_type = PACKET_HOST;
388 tunnel->stat.rx_packets++;
389 tunnel->stat.rx_bytes += skb->len;
390 skb->dev = tunnel->dev;
391 dst_release(skb->dst);
392 skb->dst = NULL;
393 nf_reset(skb);
394 ipip6_ecn_decapsulate(iph, skb);
395 netif_rx(skb);
396 read_unlock(&ipip6_lock);
397 return 0;
398 }
399
Herbert Xu45af08b2006-04-05 22:31:19 -0700400 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 kfree_skb(skb);
402 read_unlock(&ipip6_lock);
403out:
404 return 0;
405}
406
407/* Returns the embedded IPv4 address if the IPv6 address
408 comes from 6to4 (RFC 3056) addr space */
409
410static inline u32 try_6to4(struct in6_addr *v6dst)
411{
412 u32 dst = 0;
413
414 if (v6dst->s6_addr16[0] == htons(0x2002)) {
415 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
416 memcpy(&dst, &v6dst->s6_addr16[1], 4);
417 }
418 return dst;
419}
420
421/*
422 * This function assumes it is being called from dev_queue_xmit()
423 * and that skb is filled properly by that function.
424 */
425
426static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
427{
Patrick McHardy2941a482006-01-08 22:05:26 -0800428 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct net_device_stats *stats = &tunnel->stat;
430 struct iphdr *tiph = &tunnel->parms.iph;
431 struct ipv6hdr *iph6 = skb->nh.ipv6h;
432 u8 tos = tunnel->parms.iph.tos;
433 struct rtable *rt; /* Route to the other host */
434 struct net_device *tdev; /* Device to other host */
435 struct iphdr *iph; /* Our new IP header */
436 int max_headroom; /* The extra header space needed */
437 u32 dst = tiph->daddr;
438 int mtu;
439 struct in6_addr *addr6;
440 int addr_type;
441
442 if (tunnel->recursion++) {
443 tunnel->stat.collisions++;
444 goto tx_error;
445 }
446
447 if (skb->protocol != htons(ETH_P_IPV6))
448 goto tx_error;
449
450 if (!dst)
451 dst = try_6to4(&iph6->daddr);
452
453 if (!dst) {
454 struct neighbour *neigh = NULL;
455
456 if (skb->dst)
457 neigh = skb->dst->neighbour;
458
459 if (neigh == NULL) {
460 if (net_ratelimit())
461 printk(KERN_DEBUG "sit: nexthop == NULL\n");
462 goto tx_error;
463 }
464
465 addr6 = (struct in6_addr*)&neigh->primary_key;
466 addr_type = ipv6_addr_type(addr6);
467
468 if (addr_type == IPV6_ADDR_ANY) {
469 addr6 = &skb->nh.ipv6h->daddr;
470 addr_type = ipv6_addr_type(addr6);
471 }
472
473 if ((addr_type & IPV6_ADDR_COMPATv4) == 0)
474 goto tx_error_icmp;
475
476 dst = addr6->s6_addr32[3];
477 }
478
479 {
480 struct flowi fl = { .nl_u = { .ip4_u =
481 { .daddr = dst,
482 .saddr = tiph->saddr,
483 .tos = RT_TOS(tos) } },
484 .oif = tunnel->parms.link,
485 .proto = IPPROTO_IPV6 };
486 if (ip_route_output_key(&rt, &fl)) {
487 tunnel->stat.tx_carrier_errors++;
488 goto tx_error_icmp;
489 }
490 }
491 if (rt->rt_type != RTN_UNICAST) {
492 ip_rt_put(rt);
493 tunnel->stat.tx_carrier_errors++;
494 goto tx_error_icmp;
495 }
496 tdev = rt->u.dst.dev;
497
498 if (tdev == dev) {
499 ip_rt_put(rt);
500 tunnel->stat.collisions++;
501 goto tx_error;
502 }
503
504 if (tiph->frag_off)
505 mtu = dst_mtu(&rt->u.dst) - sizeof(struct iphdr);
506 else
507 mtu = skb->dst ? dst_mtu(skb->dst) : dev->mtu;
508
509 if (mtu < 68) {
510 tunnel->stat.collisions++;
511 ip_rt_put(rt);
512 goto tx_error;
513 }
514 if (mtu < IPV6_MIN_MTU)
515 mtu = IPV6_MIN_MTU;
516 if (tunnel->parms.iph.daddr && skb->dst)
517 skb->dst->ops->update_pmtu(skb->dst, mtu);
518
519 if (skb->len > mtu) {
520 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev);
521 ip_rt_put(rt);
522 goto tx_error;
523 }
524
525 if (tunnel->err_count > 0) {
526 if (jiffies - tunnel->err_time < IPTUNNEL_ERR_TIMEO) {
527 tunnel->err_count--;
528 dst_link_failure(skb);
529 } else
530 tunnel->err_count = 0;
531 }
532
533 /*
534 * Okay, now see if we can stuff it in the buffer as-is.
535 */
536 max_headroom = LL_RESERVED_SPACE(tdev)+sizeof(struct iphdr);
537
538 if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
539 struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
540 if (!new_skb) {
541 ip_rt_put(rt);
542 stats->tx_dropped++;
543 dev_kfree_skb(skb);
544 tunnel->recursion--;
545 return 0;
546 }
547 if (skb->sk)
548 skb_set_owner_w(new_skb, skb->sk);
549 dev_kfree_skb(skb);
550 skb = new_skb;
551 iph6 = skb->nh.ipv6h;
552 }
553
554 skb->h.raw = skb->nh.raw;
555 skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
556 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Patrick McHardy8cdfab82006-01-06 23:04:01 -0800557 IPCB(skb)->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 dst_release(skb->dst);
559 skb->dst = &rt->u.dst;
560
561 /*
562 * Push down and install the IPIP header.
563 */
564
565 iph = skb->nh.iph;
566 iph->version = 4;
567 iph->ihl = sizeof(struct iphdr)>>2;
568 if (mtu > IPV6_MIN_MTU)
569 iph->frag_off = htons(IP_DF);
570 else
571 iph->frag_off = 0;
572
573 iph->protocol = IPPROTO_IPV6;
574 iph->tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
575 iph->daddr = rt->rt_dst;
576 iph->saddr = rt->rt_src;
577
578 if ((iph->ttl = tiph->ttl) == 0)
579 iph->ttl = iph6->hop_limit;
580
581 nf_reset(skb);
582
583 IPTUNNEL_XMIT();
584 tunnel->recursion--;
585 return 0;
586
587tx_error_icmp:
588 dst_link_failure(skb);
589tx_error:
590 stats->tx_errors++;
591 dev_kfree_skb(skb);
592 tunnel->recursion--;
593 return 0;
594}
595
596static int
597ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
598{
599 int err = 0;
600 struct ip_tunnel_parm p;
601 struct ip_tunnel *t;
602
603 switch (cmd) {
604 case SIOCGETTUNNEL:
605 t = NULL;
606 if (dev == ipip6_fb_tunnel_dev) {
607 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
608 err = -EFAULT;
609 break;
610 }
611 t = ipip6_tunnel_locate(&p, 0);
612 }
613 if (t == NULL)
Patrick McHardy2941a482006-01-08 22:05:26 -0800614 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 memcpy(&p, &t->parms, sizeof(p));
616 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
617 err = -EFAULT;
618 break;
619
620 case SIOCADDTUNNEL:
621 case SIOCCHGTUNNEL:
622 err = -EPERM;
623 if (!capable(CAP_NET_ADMIN))
624 goto done;
625
626 err = -EFAULT;
627 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
628 goto done;
629
630 err = -EINVAL;
631 if (p.iph.version != 4 || p.iph.protocol != IPPROTO_IPV6 ||
632 p.iph.ihl != 5 || (p.iph.frag_off&htons(~IP_DF)))
633 goto done;
634 if (p.iph.ttl)
635 p.iph.frag_off |= htons(IP_DF);
636
637 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL);
638
639 if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
640 if (t != NULL) {
641 if (t->dev != dev) {
642 err = -EEXIST;
643 break;
644 }
645 } else {
646 if (((dev->flags&IFF_POINTOPOINT) && !p.iph.daddr) ||
647 (!(dev->flags&IFF_POINTOPOINT) && p.iph.daddr)) {
648 err = -EINVAL;
649 break;
650 }
Patrick McHardy2941a482006-01-08 22:05:26 -0800651 t = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ipip6_tunnel_unlink(t);
653 t->parms.iph.saddr = p.iph.saddr;
654 t->parms.iph.daddr = p.iph.daddr;
655 memcpy(dev->dev_addr, &p.iph.saddr, 4);
656 memcpy(dev->broadcast, &p.iph.daddr, 4);
657 ipip6_tunnel_link(t);
658 netdev_state_change(dev);
659 }
660 }
661
662 if (t) {
663 err = 0;
664 if (cmd == SIOCCHGTUNNEL) {
665 t->parms.iph.ttl = p.iph.ttl;
666 t->parms.iph.tos = p.iph.tos;
667 }
668 if (copy_to_user(ifr->ifr_ifru.ifru_data, &t->parms, sizeof(p)))
669 err = -EFAULT;
670 } else
671 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
672 break;
673
674 case SIOCDELTUNNEL:
675 err = -EPERM;
676 if (!capable(CAP_NET_ADMIN))
677 goto done;
678
679 if (dev == ipip6_fb_tunnel_dev) {
680 err = -EFAULT;
681 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
682 goto done;
683 err = -ENOENT;
684 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL)
685 goto done;
686 err = -EPERM;
Patrick McHardy2941a482006-01-08 22:05:26 -0800687 if (t == netdev_priv(ipip6_fb_tunnel_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 goto done;
689 dev = t->dev;
690 }
691 err = unregister_netdevice(dev);
692 break;
693
694 default:
695 err = -EINVAL;
696 }
697
698done:
699 return err;
700}
701
702static struct net_device_stats *ipip6_tunnel_get_stats(struct net_device *dev)
703{
Patrick McHardy2941a482006-01-08 22:05:26 -0800704 return &(((struct ip_tunnel*)netdev_priv(dev))->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707static int ipip6_tunnel_change_mtu(struct net_device *dev, int new_mtu)
708{
709 if (new_mtu < IPV6_MIN_MTU || new_mtu > 0xFFF8 - sizeof(struct iphdr))
710 return -EINVAL;
711 dev->mtu = new_mtu;
712 return 0;
713}
714
715static void ipip6_tunnel_setup(struct net_device *dev)
716{
717 SET_MODULE_OWNER(dev);
718 dev->uninit = ipip6_tunnel_uninit;
719 dev->destructor = free_netdev;
720 dev->hard_start_xmit = ipip6_tunnel_xmit;
721 dev->get_stats = ipip6_tunnel_get_stats;
722 dev->do_ioctl = ipip6_tunnel_ioctl;
723 dev->change_mtu = ipip6_tunnel_change_mtu;
724
725 dev->type = ARPHRD_SIT;
726 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800727 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 dev->flags = IFF_NOARP;
729 dev->iflink = 0;
730 dev->addr_len = 4;
731}
732
733static int ipip6_tunnel_init(struct net_device *dev)
734{
735 struct net_device *tdev = NULL;
736 struct ip_tunnel *tunnel;
737 struct iphdr *iph;
738
Patrick McHardy2941a482006-01-08 22:05:26 -0800739 tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 iph = &tunnel->parms.iph;
741
742 tunnel->dev = dev;
743 strcpy(tunnel->parms.name, dev->name);
744
745 memcpy(dev->dev_addr, &tunnel->parms.iph.saddr, 4);
746 memcpy(dev->broadcast, &tunnel->parms.iph.daddr, 4);
747
748 if (iph->daddr) {
749 struct flowi fl = { .nl_u = { .ip4_u =
750 { .daddr = iph->daddr,
751 .saddr = iph->saddr,
752 .tos = RT_TOS(iph->tos) } },
753 .oif = tunnel->parms.link,
754 .proto = IPPROTO_IPV6 };
755 struct rtable *rt;
756 if (!ip_route_output_key(&rt, &fl)) {
757 tdev = rt->u.dst.dev;
758 ip_rt_put(rt);
759 }
760 dev->flags |= IFF_POINTOPOINT;
761 }
762
763 if (!tdev && tunnel->parms.link)
764 tdev = __dev_get_by_index(tunnel->parms.link);
765
766 if (tdev) {
767 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
768 dev->mtu = tdev->mtu - sizeof(struct iphdr);
769 if (dev->mtu < IPV6_MIN_MTU)
770 dev->mtu = IPV6_MIN_MTU;
771 }
772 dev->iflink = tunnel->parms.link;
773
774 return 0;
775}
776
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -0300777static int __init ipip6_fb_tunnel_init(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Patrick McHardy2941a482006-01-08 22:05:26 -0800779 struct ip_tunnel *tunnel = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct iphdr *iph = &tunnel->parms.iph;
781
782 tunnel->dev = dev;
783 strcpy(tunnel->parms.name, dev->name);
784
785 iph->version = 4;
786 iph->protocol = IPPROTO_IPV6;
787 iph->ihl = 5;
788 iph->ttl = 64;
789
790 dev_hold(dev);
791 tunnels_wc[0] = tunnel;
792 return 0;
793}
794
795static struct net_protocol sit_protocol = {
796 .handler = ipip6_rcv,
797 .err_handler = ipip6_err,
798};
799
Alexey Kuznetsovdb445752005-07-30 17:46:44 -0700800static void __exit sit_destroy_tunnels(void)
801{
802 int prio;
803
804 for (prio = 1; prio < 4; prio++) {
805 int h;
806 for (h = 0; h < HASH_SIZE; h++) {
807 struct ip_tunnel *t;
808 while ((t = tunnels[prio][h]) != NULL)
809 unregister_netdevice(t->dev);
810 }
811 }
812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814void __exit sit_cleanup(void)
815{
816 inet_del_protocol(&sit_protocol, IPPROTO_IPV6);
Alexey Kuznetsovdb445752005-07-30 17:46:44 -0700817
818 rtnl_lock();
819 sit_destroy_tunnels();
820 unregister_netdevice(ipip6_fb_tunnel_dev);
821 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824int __init sit_init(void)
825{
826 int err;
827
828 printk(KERN_INFO "IPv6 over IPv4 tunneling driver\n");
829
830 if (inet_add_protocol(&sit_protocol, IPPROTO_IPV6) < 0) {
831 printk(KERN_INFO "sit init: Can't add protocol\n");
832 return -EAGAIN;
833 }
834
835 ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
836 ipip6_tunnel_setup);
837 if (!ipip6_fb_tunnel_dev) {
838 err = -ENOMEM;
839 goto err1;
840 }
841
842 ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
843
844 if ((err = register_netdev(ipip6_fb_tunnel_dev)))
845 goto err2;
846
847 out:
848 return err;
849 err2:
850 free_netdev(ipip6_fb_tunnel_dev);
851 err1:
852 inet_del_protocol(&sit_protocol, IPPROTO_IPV6);
853 goto out;
854}