2 * IPv6 over IPv4 tunnel device - Simple Internet Transition (SIT)
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
7 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
9 * $Id: sit.c,v 1.53 2001/09/25 05:09:53 davem Exp $
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.
17 * Roger Venning <r.venning@telstra.com>: 6to4 support
18 * Nate Thompson <nate@thebog.net>: 6to4 support
21 #include <linux/module.h>
22 #include <linux/capability.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/socket.h>
26 #include <linux/sockios.h>
27 #include <linux/sched.h>
28 #include <linux/net.h>
29 #include <linux/in6.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/icmp.h>
33 #include <asm/uaccess.h>
34 #include <linux/init.h>
35 #include <linux/netfilter_ipv4.h>
36 #include <linux/if_ether.h>
42 #include <net/protocol.h>
43 #include <net/transp_v6.h>
44 #include <net/ip6_fib.h>
45 #include <net/ip6_route.h>
46 #include <net/ndisc.h>
47 #include <net/addrconf.h>
52 #include <net/inet_ecn.h>
54 #include <net/dsfield.h>
57 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
59 For comments look at net/ipv4/ip_gre.c --ANK
63 #define HASH(addr) ((addr^(addr>>4))&0xF)
65 static int ipip6_fb_tunnel_init(struct net_device
*dev
);
66 static int ipip6_tunnel_init(struct net_device
*dev
);
67 static void ipip6_tunnel_setup(struct net_device
*dev
);
69 static struct net_device
*ipip6_fb_tunnel_dev
;
71 static struct ip_tunnel
*tunnels_r_l
[HASH_SIZE
];
72 static struct ip_tunnel
*tunnels_r
[HASH_SIZE
];
73 static struct ip_tunnel
*tunnels_l
[HASH_SIZE
];
74 static struct ip_tunnel
*tunnels_wc
[1];
75 static struct ip_tunnel
**tunnels
[4] = { tunnels_wc
, tunnels_l
, tunnels_r
, tunnels_r_l
};
77 static DEFINE_RWLOCK(ipip6_lock
);
79 static struct ip_tunnel
* ipip6_tunnel_lookup(u32 remote
, u32 local
)
81 unsigned h0
= HASH(remote
);
82 unsigned h1
= HASH(local
);
85 for (t
= tunnels_r_l
[h0
^h1
]; t
; t
= t
->next
) {
86 if (local
== t
->parms
.iph
.saddr
&&
87 remote
== t
->parms
.iph
.daddr
&& (t
->dev
->flags
&IFF_UP
))
90 for (t
= tunnels_r
[h0
]; t
; t
= t
->next
) {
91 if (remote
== t
->parms
.iph
.daddr
&& (t
->dev
->flags
&IFF_UP
))
94 for (t
= tunnels_l
[h1
]; t
; t
= t
->next
) {
95 if (local
== t
->parms
.iph
.saddr
&& (t
->dev
->flags
&IFF_UP
))
98 if ((t
= tunnels_wc
[0]) != NULL
&& (t
->dev
->flags
&IFF_UP
))
103 static struct ip_tunnel
** ipip6_bucket(struct ip_tunnel
*t
)
105 u32 remote
= t
->parms
.iph
.daddr
;
106 u32 local
= t
->parms
.iph
.saddr
;
118 return &tunnels
[prio
][h
];
121 static void ipip6_tunnel_unlink(struct ip_tunnel
*t
)
123 struct ip_tunnel
**tp
;
125 for (tp
= ipip6_bucket(t
); *tp
; tp
= &(*tp
)->next
) {
127 write_lock_bh(&ipip6_lock
);
129 write_unlock_bh(&ipip6_lock
);
135 static void ipip6_tunnel_link(struct ip_tunnel
*t
)
137 struct ip_tunnel
**tp
= ipip6_bucket(t
);
140 write_lock_bh(&ipip6_lock
);
142 write_unlock_bh(&ipip6_lock
);
145 static struct ip_tunnel
* ipip6_tunnel_locate(struct ip_tunnel_parm
*parms
, int create
)
147 u32 remote
= parms
->iph
.daddr
;
148 u32 local
= parms
->iph
.saddr
;
149 struct ip_tunnel
*t
, **tp
, *nt
;
150 struct net_device
*dev
;
163 for (tp
= &tunnels
[prio
][h
]; (t
= *tp
) != NULL
; tp
= &t
->next
) {
164 if (local
== t
->parms
.iph
.saddr
&& remote
== t
->parms
.iph
.daddr
)
171 strlcpy(name
, parms
->name
, IFNAMSIZ
);
174 for (i
=1; i
<100; i
++) {
175 sprintf(name
, "sit%d", i
);
176 if (__dev_get_by_name(name
) == NULL
)
183 dev
= alloc_netdev(sizeof(*t
), name
, ipip6_tunnel_setup
);
187 nt
= netdev_priv(dev
);
188 dev
->init
= ipip6_tunnel_init
;
191 if (register_netdevice(dev
) < 0) {
198 ipip6_tunnel_link(nt
);
205 static void ipip6_tunnel_uninit(struct net_device
*dev
)
207 if (dev
== ipip6_fb_tunnel_dev
) {
208 write_lock_bh(&ipip6_lock
);
209 tunnels_wc
[0] = NULL
;
210 write_unlock_bh(&ipip6_lock
);
213 ipip6_tunnel_unlink(netdev_priv(dev
));
219 static void ipip6_err(struct sk_buff
*skb
, u32 info
)
221 #ifndef I_WISH_WORLD_WERE_PERFECT
223 /* It is not :-( All the routers (except for Linux) return only
224 8 bytes of packet payload. It means, that precise relaying of
225 ICMP in the real Internet is absolutely infeasible.
227 struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
228 int type
= skb
->h
.icmph
->type
;
229 int code
= skb
->h
.icmph
->code
;
234 case ICMP_PARAMETERPROB
:
237 case ICMP_DEST_UNREACH
:
240 case ICMP_PORT_UNREACH
:
241 /* Impossible event. */
243 case ICMP_FRAG_NEEDED
:
244 /* Soft state for pmtu is maintained by IP core. */
247 /* All others are translated to HOST_UNREACH.
248 rfc2003 contains "deep thoughts" about NET_UNREACH,
249 I believe they are just ether pollution. --ANK
254 case ICMP_TIME_EXCEEDED
:
255 if (code
!= ICMP_EXC_TTL
)
260 read_lock(&ipip6_lock
);
261 t
= ipip6_tunnel_lookup(iph
->daddr
, iph
->saddr
);
262 if (t
== NULL
|| t
->parms
.iph
.daddr
== 0)
264 if (t
->parms
.iph
.ttl
== 0 && type
== ICMP_TIME_EXCEEDED
)
267 if (jiffies
- t
->err_time
< IPTUNNEL_ERR_TIMEO
)
271 t
->err_time
= jiffies
;
273 read_unlock(&ipip6_lock
);
276 struct iphdr
*iph
= (struct iphdr
*)dp
;
277 int hlen
= iph
->ihl
<<2;
278 struct ipv6hdr
*iph6
;
279 int type
= skb
->h
.icmph
->type
;
280 int code
= skb
->h
.icmph
->code
;
284 struct sk_buff
*skb2
;
285 struct rt6_info
*rt6i
;
287 if (len
< hlen
+ sizeof(struct ipv6hdr
))
289 iph6
= (struct ipv6hdr
*)(dp
+ hlen
);
294 case ICMP_PARAMETERPROB
:
295 if (skb
->h
.icmph
->un
.gateway
< hlen
)
298 /* So... This guy found something strange INSIDE encapsulated
299 packet. Well, he is fool, but what can we do ?
301 rel_type
= ICMPV6_PARAMPROB
;
302 rel_info
= skb
->h
.icmph
->un
.gateway
- hlen
;
305 case ICMP_DEST_UNREACH
:
308 case ICMP_PORT_UNREACH
:
309 /* Impossible event. */
311 case ICMP_FRAG_NEEDED
:
312 /* Too complicated case ... */
315 /* All others are translated to HOST_UNREACH.
316 rfc2003 contains "deep thoughts" about NET_UNREACH,
317 I believe, it is just ether pollution. --ANK
319 rel_type
= ICMPV6_DEST_UNREACH
;
320 rel_code
= ICMPV6_ADDR_UNREACH
;
324 case ICMP_TIME_EXCEEDED
:
325 if (code
!= ICMP_EXC_TTL
)
327 rel_type
= ICMPV6_TIME_EXCEED
;
328 rel_code
= ICMPV6_EXC_HOPLIMIT
;
332 /* Prepare fake skb to feed it to icmpv6_send */
333 skb2
= skb_clone(skb
, GFP_ATOMIC
);
336 dst_release(skb2
->dst
);
338 skb_pull(skb2
, skb
->data
- (u8
*)iph6
);
339 skb2
->nh
.raw
= skb2
->data
;
341 /* Try to guess incoming interface */
342 rt6i
= rt6_lookup(&iph6
->saddr
, NULL
, NULL
, 0);
343 if (rt6i
&& rt6i
->rt6i_dev
) {
344 skb2
->dev
= rt6i
->rt6i_dev
;
346 rt6i
= rt6_lookup(&iph6
->daddr
, &iph6
->saddr
, NULL
, 0);
348 if (rt6i
&& rt6i
->rt6i_dev
&& rt6i
->rt6i_dev
->type
== ARPHRD_SIT
) {
349 struct ip_tunnel
*t
= netdev_priv(rt6i
->rt6i_dev
);
350 if (rel_type
== ICMPV6_TIME_EXCEED
&& t
->parms
.iph
.ttl
) {
351 rel_type
= ICMPV6_DEST_UNREACH
;
352 rel_code
= ICMPV6_ADDR_UNREACH
;
354 icmpv6_send(skb2
, rel_type
, rel_code
, rel_info
, skb2
->dev
);
362 static inline void ipip6_ecn_decapsulate(struct iphdr
*iph
, struct sk_buff
*skb
)
364 if (INET_ECN_is_ce(iph
->tos
))
365 IP6_ECN_set_ce(skb
->nh
.ipv6h
);
368 static int ipip6_rcv(struct sk_buff
*skb
)
371 struct ip_tunnel
*tunnel
;
373 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
378 read_lock(&ipip6_lock
);
379 if ((tunnel
= ipip6_tunnel_lookup(iph
->saddr
, iph
->daddr
)) != NULL
) {
381 skb
->mac
.raw
= skb
->nh
.raw
;
382 skb
->nh
.raw
= skb
->data
;
383 IPCB(skb
)->flags
= 0;
384 skb
->protocol
= htons(ETH_P_IPV6
);
385 skb
->pkt_type
= PACKET_HOST
;
386 tunnel
->stat
.rx_packets
++;
387 tunnel
->stat
.rx_bytes
+= skb
->len
;
388 skb
->dev
= tunnel
->dev
;
389 dst_release(skb
->dst
);
392 ipip6_ecn_decapsulate(iph
, skb
);
394 read_unlock(&ipip6_lock
);
398 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_PORT_UNREACH
, 0);
400 read_unlock(&ipip6_lock
);
405 /* Returns the embedded IPv4 address if the IPv6 address
406 comes from 6to4 (RFC 3056) addr space */
408 static inline u32
try_6to4(struct in6_addr
*v6dst
)
412 if (v6dst
->s6_addr16
[0] == htons(0x2002)) {
413 /* 6to4 v6 addr has 16 bits prefix, 32 v4addr, 16 SLA, ... */
414 memcpy(&dst
, &v6dst
->s6_addr16
[1], 4);
420 * This function assumes it is being called from dev_queue_xmit()
421 * and that skb is filled properly by that function.
424 static int ipip6_tunnel_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
426 struct ip_tunnel
*tunnel
= netdev_priv(dev
);
427 struct net_device_stats
*stats
= &tunnel
->stat
;
428 struct iphdr
*tiph
= &tunnel
->parms
.iph
;
429 struct ipv6hdr
*iph6
= skb
->nh
.ipv6h
;
430 u8 tos
= tunnel
->parms
.iph
.tos
;
431 struct rtable
*rt
; /* Route to the other host */
432 struct net_device
*tdev
; /* Device to other host */
433 struct iphdr
*iph
; /* Our new IP header */
434 int max_headroom
; /* The extra header space needed */
435 u32 dst
= tiph
->daddr
;
437 struct in6_addr
*addr6
;
440 if (tunnel
->recursion
++) {
441 tunnel
->stat
.collisions
++;
445 if (skb
->protocol
!= htons(ETH_P_IPV6
))
449 dst
= try_6to4(&iph6
->daddr
);
452 struct neighbour
*neigh
= NULL
;
455 neigh
= skb
->dst
->neighbour
;
459 printk(KERN_DEBUG
"sit: nexthop == NULL\n");
463 addr6
= (struct in6_addr
*)&neigh
->primary_key
;
464 addr_type
= ipv6_addr_type(addr6
);
466 if (addr_type
== IPV6_ADDR_ANY
) {
467 addr6
= &skb
->nh
.ipv6h
->daddr
;
468 addr_type
= ipv6_addr_type(addr6
);
471 if ((addr_type
& IPV6_ADDR_COMPATv4
) == 0)
474 dst
= addr6
->s6_addr32
[3];
478 struct flowi fl
= { .nl_u
= { .ip4_u
=
480 .saddr
= tiph
->saddr
,
481 .tos
= RT_TOS(tos
) } },
482 .oif
= tunnel
->parms
.link
,
483 .proto
= IPPROTO_IPV6
};
484 if (ip_route_output_key(&rt
, &fl
)) {
485 tunnel
->stat
.tx_carrier_errors
++;
489 if (rt
->rt_type
!= RTN_UNICAST
) {
491 tunnel
->stat
.tx_carrier_errors
++;
494 tdev
= rt
->u
.dst
.dev
;
498 tunnel
->stat
.collisions
++;
503 mtu
= dst_mtu(&rt
->u
.dst
) - sizeof(struct iphdr
);
505 mtu
= skb
->dst
? dst_mtu(skb
->dst
) : dev
->mtu
;
508 tunnel
->stat
.collisions
++;
512 if (mtu
< IPV6_MIN_MTU
)
514 if (tunnel
->parms
.iph
.daddr
&& skb
->dst
)
515 skb
->dst
->ops
->update_pmtu(skb
->dst
, mtu
);
517 if (skb
->len
> mtu
) {
518 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
, dev
);
523 if (tunnel
->err_count
> 0) {
524 if (jiffies
- tunnel
->err_time
< IPTUNNEL_ERR_TIMEO
) {
526 dst_link_failure(skb
);
528 tunnel
->err_count
= 0;
532 * Okay, now see if we can stuff it in the buffer as-is.
534 max_headroom
= LL_RESERVED_SPACE(tdev
)+sizeof(struct iphdr
);
536 if (skb_headroom(skb
) < max_headroom
|| skb_cloned(skb
) || skb_shared(skb
)) {
537 struct sk_buff
*new_skb
= skb_realloc_headroom(skb
, max_headroom
);
546 skb_set_owner_w(new_skb
, skb
->sk
);
549 iph6
= skb
->nh
.ipv6h
;
552 skb
->h
.raw
= skb
->nh
.raw
;
553 skb
->nh
.raw
= skb_push(skb
, sizeof(struct iphdr
));
554 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
555 IPCB(skb
)->flags
= 0;
556 dst_release(skb
->dst
);
557 skb
->dst
= &rt
->u
.dst
;
560 * Push down and install the IPIP header.
565 iph
->ihl
= sizeof(struct iphdr
)>>2;
566 if (mtu
> IPV6_MIN_MTU
)
567 iph
->frag_off
= htons(IP_DF
);
571 iph
->protocol
= IPPROTO_IPV6
;
572 iph
->tos
= INET_ECN_encapsulate(tos
, ipv6_get_dsfield(iph6
));
573 iph
->daddr
= rt
->rt_dst
;
574 iph
->saddr
= rt
->rt_src
;
576 if ((iph
->ttl
= tiph
->ttl
) == 0)
577 iph
->ttl
= iph6
->hop_limit
;
586 dst_link_failure(skb
);
595 ipip6_tunnel_ioctl (struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
598 struct ip_tunnel_parm p
;
604 if (dev
== ipip6_fb_tunnel_dev
) {
605 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
))) {
609 t
= ipip6_tunnel_locate(&p
, 0);
612 t
= netdev_priv(dev
);
613 memcpy(&p
, &t
->parms
, sizeof(p
));
614 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
621 if (!capable(CAP_NET_ADMIN
))
625 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
629 if (p
.iph
.version
!= 4 || p
.iph
.protocol
!= IPPROTO_IPV6
||
630 p
.iph
.ihl
!= 5 || (p
.iph
.frag_off
&htons(~IP_DF
)))
633 p
.iph
.frag_off
|= htons(IP_DF
);
635 t
= ipip6_tunnel_locate(&p
, cmd
== SIOCADDTUNNEL
);
637 if (dev
!= ipip6_fb_tunnel_dev
&& cmd
== SIOCCHGTUNNEL
) {
644 if (((dev
->flags
&IFF_POINTOPOINT
) && !p
.iph
.daddr
) ||
645 (!(dev
->flags
&IFF_POINTOPOINT
) && p
.iph
.daddr
)) {
649 t
= netdev_priv(dev
);
650 ipip6_tunnel_unlink(t
);
651 t
->parms
.iph
.saddr
= p
.iph
.saddr
;
652 t
->parms
.iph
.daddr
= p
.iph
.daddr
;
653 memcpy(dev
->dev_addr
, &p
.iph
.saddr
, 4);
654 memcpy(dev
->broadcast
, &p
.iph
.daddr
, 4);
655 ipip6_tunnel_link(t
);
656 netdev_state_change(dev
);
662 if (cmd
== SIOCCHGTUNNEL
) {
663 t
->parms
.iph
.ttl
= p
.iph
.ttl
;
664 t
->parms
.iph
.tos
= p
.iph
.tos
;
666 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &t
->parms
, sizeof(p
)))
669 err
= (cmd
== SIOCADDTUNNEL
? -ENOBUFS
: -ENOENT
);
674 if (!capable(CAP_NET_ADMIN
))
677 if (dev
== ipip6_fb_tunnel_dev
) {
679 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
682 if ((t
= ipip6_tunnel_locate(&p
, 0)) == NULL
)
685 if (t
== netdev_priv(ipip6_fb_tunnel_dev
))
689 err
= unregister_netdevice(dev
);
700 static struct net_device_stats
*ipip6_tunnel_get_stats(struct net_device
*dev
)
702 return &(((struct ip_tunnel
*)netdev_priv(dev
))->stat
);
705 static int ipip6_tunnel_change_mtu(struct net_device
*dev
, int new_mtu
)
707 if (new_mtu
< IPV6_MIN_MTU
|| new_mtu
> 0xFFF8 - sizeof(struct iphdr
))
713 static void ipip6_tunnel_setup(struct net_device
*dev
)
715 SET_MODULE_OWNER(dev
);
716 dev
->uninit
= ipip6_tunnel_uninit
;
717 dev
->destructor
= free_netdev
;
718 dev
->hard_start_xmit
= ipip6_tunnel_xmit
;
719 dev
->get_stats
= ipip6_tunnel_get_stats
;
720 dev
->do_ioctl
= ipip6_tunnel_ioctl
;
721 dev
->change_mtu
= ipip6_tunnel_change_mtu
;
723 dev
->type
= ARPHRD_SIT
;
724 dev
->hard_header_len
= LL_MAX_HEADER
+ sizeof(struct iphdr
);
725 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct iphdr
);
726 dev
->flags
= IFF_NOARP
;
731 static int ipip6_tunnel_init(struct net_device
*dev
)
733 struct net_device
*tdev
= NULL
;
734 struct ip_tunnel
*tunnel
;
737 tunnel
= netdev_priv(dev
);
738 iph
= &tunnel
->parms
.iph
;
741 strcpy(tunnel
->parms
.name
, dev
->name
);
743 memcpy(dev
->dev_addr
, &tunnel
->parms
.iph
.saddr
, 4);
744 memcpy(dev
->broadcast
, &tunnel
->parms
.iph
.daddr
, 4);
747 struct flowi fl
= { .nl_u
= { .ip4_u
=
748 { .daddr
= iph
->daddr
,
750 .tos
= RT_TOS(iph
->tos
) } },
751 .oif
= tunnel
->parms
.link
,
752 .proto
= IPPROTO_IPV6
};
754 if (!ip_route_output_key(&rt
, &fl
)) {
755 tdev
= rt
->u
.dst
.dev
;
758 dev
->flags
|= IFF_POINTOPOINT
;
761 if (!tdev
&& tunnel
->parms
.link
)
762 tdev
= __dev_get_by_index(tunnel
->parms
.link
);
765 dev
->hard_header_len
= tdev
->hard_header_len
+ sizeof(struct iphdr
);
766 dev
->mtu
= tdev
->mtu
- sizeof(struct iphdr
);
767 if (dev
->mtu
< IPV6_MIN_MTU
)
768 dev
->mtu
= IPV6_MIN_MTU
;
770 dev
->iflink
= tunnel
->parms
.link
;
775 static int __init
ipip6_fb_tunnel_init(struct net_device
*dev
)
777 struct ip_tunnel
*tunnel
= netdev_priv(dev
);
778 struct iphdr
*iph
= &tunnel
->parms
.iph
;
781 strcpy(tunnel
->parms
.name
, dev
->name
);
784 iph
->protocol
= IPPROTO_IPV6
;
789 tunnels_wc
[0] = tunnel
;
793 static struct net_protocol sit_protocol
= {
794 .handler
= ipip6_rcv
,
795 .err_handler
= ipip6_err
,
798 static void __exit
sit_destroy_tunnels(void)
802 for (prio
= 1; prio
< 4; prio
++) {
804 for (h
= 0; h
< HASH_SIZE
; h
++) {
806 while ((t
= tunnels
[prio
][h
]) != NULL
)
807 unregister_netdevice(t
->dev
);
812 void __exit
sit_cleanup(void)
814 inet_del_protocol(&sit_protocol
, IPPROTO_IPV6
);
817 sit_destroy_tunnels();
818 unregister_netdevice(ipip6_fb_tunnel_dev
);
822 int __init
sit_init(void)
826 printk(KERN_INFO
"IPv6 over IPv4 tunneling driver\n");
828 if (inet_add_protocol(&sit_protocol
, IPPROTO_IPV6
) < 0) {
829 printk(KERN_INFO
"sit init: Can't add protocol\n");
833 ipip6_fb_tunnel_dev
= alloc_netdev(sizeof(struct ip_tunnel
), "sit0",
835 if (!ipip6_fb_tunnel_dev
) {
840 ipip6_fb_tunnel_dev
->init
= ipip6_fb_tunnel_init
;
842 if ((err
= register_netdev(ipip6_fb_tunnel_dev
)))
848 free_netdev(ipip6_fb_tunnel_dev
);
850 inet_del_protocol(&sit_protocol
, IPPROTO_IPV6
);
854 module_init(sit_init
);
855 module_exit(sit_cleanup
);
856 MODULE_LICENSE("GPL");