2 * IPv6 tunneling device
3 * Linux INET6 implementation
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
12 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
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.
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/errno.h>
26 #include <linux/types.h>
27 #include <linux/sockios.h>
28 #include <linux/icmp.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>
43 #include <asm/uaccess.h>
44 #include <asm/atomic.h>
49 #include <net/ip6_route.h>
50 #include <net/addrconf.h>
51 #include <net/ip6_tunnel.h>
53 #include <net/dsfield.h>
54 #include <net/inet_ecn.h>
56 MODULE_AUTHOR("Ville Nuorvala");
57 MODULE_DESCRIPTION("IPv6 tunneling device");
58 MODULE_LICENSE("GPL");
60 #define IPV6_TLV_TEL_DST_SIZE 8
63 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
65 #define IP6_TNL_TRACE(x...) do {;} while(0)
68 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
69 #define IPV6_TCLASS_SHIFT 20
73 #define HASH(addr) ((__force u32)((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
74 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
77 static int ip6_fb_tnl_dev_init(struct net_device
*dev
);
78 static int ip6_tnl_dev_init(struct net_device
*dev
);
79 static void ip6_tnl_dev_setup(struct net_device
*dev
);
81 /* the IPv6 tunnel fallback device */
82 static struct net_device
*ip6_fb_tnl_dev
;
85 /* lists for storing tunnels in use */
86 static struct ip6_tnl
*tnls_r_l
[HASH_SIZE
];
87 static struct ip6_tnl
*tnls_wc
[1];
88 static struct ip6_tnl
**tnls
[2] = { tnls_wc
, tnls_r_l
};
90 /* lock for the tunnel lists */
91 static DEFINE_RWLOCK(ip6_tnl_lock
);
93 static inline struct dst_entry
*ip6_tnl_dst_check(struct ip6_tnl
*t
)
95 struct dst_entry
*dst
= t
->dst_cache
;
97 if (dst
&& dst
->obsolete
&&
98 dst
->ops
->check(dst
, t
->dst_cookie
) == NULL
) {
107 static inline void ip6_tnl_dst_reset(struct ip6_tnl
*t
)
109 dst_release(t
->dst_cache
);
113 static inline void ip6_tnl_dst_store(struct ip6_tnl
*t
, struct dst_entry
*dst
)
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
);
122 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
123 * @remote: the address of the tunnel exit-point
124 * @local: the address of the tunnel entry-point
127 * tunnel matching given end-points if found,
128 * else fallback tunnel if its device is up,
132 static struct ip6_tnl
*
133 ip6_tnl_lookup(struct in6_addr
*remote
, struct in6_addr
*local
)
135 unsigned h0
= HASH(remote
);
136 unsigned h1
= HASH(local
);
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
))
145 if ((t
= tnls_wc
[0]) != NULL
&& (t
->dev
->flags
& IFF_UP
))
152 * ip6_tnl_bucket - get head of list matching given tunnel parameters
153 * @p: parameters containing tunnel end-points
156 * ip6_tnl_bucket() returns the head of the list matching the
157 * &struct in6_addr entries laddr and raddr in @p.
159 * Return: head of IPv6 tunnel list
162 static struct ip6_tnl
**
163 ip6_tnl_bucket(struct ip6_tnl_parm
*p
)
165 struct in6_addr
*remote
= &p
->raddr
;
166 struct in6_addr
*local
= &p
->laddr
;
170 if (!ipv6_addr_any(remote
) || !ipv6_addr_any(local
)) {
172 h
= HASH(remote
) ^ HASH(local
);
174 return &tnls
[prio
][h
];
178 * ip6_tnl_link - add tunnel to hash table
179 * @t: tunnel to be added
183 ip6_tnl_link(struct ip6_tnl
*t
)
185 struct ip6_tnl
**tp
= ip6_tnl_bucket(&t
->parms
);
188 write_lock_bh(&ip6_tnl_lock
);
190 write_unlock_bh(&ip6_tnl_lock
);
194 * ip6_tnl_unlink - remove tunnel from hash table
195 * @t: tunnel to be removed
199 ip6_tnl_unlink(struct ip6_tnl
*t
)
203 for (tp
= ip6_tnl_bucket(&t
->parms
); *tp
; tp
= &(*tp
)->next
) {
205 write_lock_bh(&ip6_tnl_lock
);
207 write_unlock_bh(&ip6_tnl_lock
);
214 * ip6_tnl_create() - create a new tunnel
215 * @p: tunnel parameters
216 * @pt: pointer to new tunnel
219 * Create tunnel matching given parameters.
222 * created tunnel or NULL
225 static struct ip6_tnl
*ip6_tnl_create(struct ip6_tnl_parm
*p
)
227 struct net_device
*dev
;
233 strlcpy(name
, p
->name
, IFNAMSIZ
);
235 sprintf(name
, "ip6tnl%%d");
237 dev
= alloc_netdev(sizeof (*t
), name
, ip6_tnl_dev_setup
);
241 t
= netdev_priv(dev
);
242 dev
->init
= ip6_tnl_dev_init
;
245 if ((err
= register_netdevice(dev
)) < 0) {
257 * ip6_tnl_locate - find or create tunnel matching given parameters
258 * @p: tunnel parameters
259 * @create: != 0 if allowed to create new tunnel if no match found
262 * ip6_tnl_locate() first tries to locate an existing tunnel
263 * based on @parms. If this is unsuccessful, but @create is set a new
264 * tunnel device is created and registered for use.
267 * matching tunnel or NULL
270 static struct ip6_tnl
*ip6_tnl_locate(struct ip6_tnl_parm
*p
, int create
)
272 struct in6_addr
*remote
= &p
->raddr
;
273 struct in6_addr
*local
= &p
->laddr
;
276 for (t
= *ip6_tnl_bucket(p
); t
; t
= t
->next
) {
277 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
278 ipv6_addr_equal(remote
, &t
->parms
.raddr
))
283 return ip6_tnl_create(p
);
287 * ip6_tnl_dev_uninit - tunnel device uninitializer
288 * @dev: the device to be destroyed
291 * ip6_tnl_dev_uninit() removes tunnel from its list
295 ip6_tnl_dev_uninit(struct net_device
*dev
)
297 struct ip6_tnl
*t
= netdev_priv(dev
);
299 if (dev
== ip6_fb_tnl_dev
) {
300 write_lock_bh(&ip6_tnl_lock
);
302 write_unlock_bh(&ip6_tnl_lock
);
306 ip6_tnl_dst_reset(t
);
311 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
312 * @skb: received socket buffer
315 * 0 if none was found,
316 * else index to encapsulation limit
320 parse_tlv_tnl_enc_lim(struct sk_buff
*skb
, __u8
* raw
)
322 struct ipv6hdr
*ipv6h
= (struct ipv6hdr
*) raw
;
323 __u8 nexthdr
= ipv6h
->nexthdr
;
324 __u16 off
= sizeof (*ipv6h
);
326 while (ipv6_ext_hdr(nexthdr
) && nexthdr
!= NEXTHDR_NONE
) {
328 struct ipv6_opt_hdr
*hdr
;
329 if (raw
+ off
+ sizeof (*hdr
) > skb
->data
&&
330 !pskb_may_pull(skb
, raw
- skb
->data
+ off
+ sizeof (*hdr
)))
333 hdr
= (struct ipv6_opt_hdr
*) (raw
+ off
);
334 if (nexthdr
== NEXTHDR_FRAGMENT
) {
335 struct frag_hdr
*frag_hdr
= (struct frag_hdr
*) hdr
;
336 if (frag_hdr
->frag_off
)
339 } else if (nexthdr
== NEXTHDR_AUTH
) {
340 optlen
= (hdr
->hdrlen
+ 2) << 2;
342 optlen
= ipv6_optlen(hdr
);
344 if (nexthdr
== NEXTHDR_DEST
) {
347 struct ipv6_tlv_tnl_enc_lim
*tel
;
349 /* No more room for encapsulation limit */
350 if (i
+ sizeof (*tel
) > off
+ optlen
)
353 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &raw
[i
];
354 /* return index of option if found and valid */
355 if (tel
->type
== IPV6_TLV_TNL_ENCAP_LIMIT
&&
358 /* else jump to next option */
360 i
+= tel
->length
+ 2;
365 nexthdr
= hdr
->nexthdr
;
372 * ip6_tnl_err - tunnel error handler
375 * ip6_tnl_err() should handle errors in the tunnel according
376 * to the specifications in RFC 2473.
380 ip6_tnl_err(struct sk_buff
*skb
, __u8 ipproto
, struct inet6_skb_parm
*opt
,
381 int *type
, int *code
, int *msg
, __u32
*info
, int offset
)
383 struct ipv6hdr
*ipv6h
= (struct ipv6hdr
*) skb
->data
;
386 int rel_type
= ICMPV6_DEST_UNREACH
;
387 int rel_code
= ICMPV6_ADDR_UNREACH
;
392 /* If the packet doesn't contain the original IPv6 header we are
393 in trouble since we might need the source address for further
394 processing of the error. */
396 read_lock(&ip6_tnl_lock
);
397 if ((t
= ip6_tnl_lookup(&ipv6h
->daddr
, &ipv6h
->saddr
)) == NULL
)
400 if (t
->parms
.proto
!= ipproto
&& t
->parms
.proto
!= 0)
407 struct ipv6_tlv_tnl_enc_lim
*tel
;
409 case ICMPV6_DEST_UNREACH
:
412 "%s: Path to destination invalid "
413 "or inactive!\n", t
->parms
.name
);
416 case ICMPV6_TIME_EXCEED
:
417 if ((*code
) == ICMPV6_EXC_HOPLIMIT
) {
420 "%s: Too small hop limit or "
421 "routing loop in tunnel!\n",
426 case ICMPV6_PARAMPROB
:
428 if ((*code
) == ICMPV6_HDR_FIELD
)
429 teli
= parse_tlv_tnl_enc_lim(skb
, skb
->data
);
431 if (teli
&& teli
== *info
- 2) {
432 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &skb
->data
[teli
];
433 if (tel
->encap_limit
== 0) {
436 "%s: Too small encapsulation "
437 "limit or routing loop in "
438 "tunnel!\n", t
->parms
.name
);
441 } else if (net_ratelimit()) {
443 "%s: Recipient unable to parse tunneled "
444 "packet!\n ", t
->parms
.name
);
447 case ICMPV6_PKT_TOOBIG
:
448 mtu
= *info
- offset
;
449 if (mtu
< IPV6_MIN_MTU
)
453 if ((len
= sizeof (*ipv6h
) + ntohs(ipv6h
->payload_len
)) > mtu
) {
454 rel_type
= ICMPV6_PKT_TOOBIG
;
468 read_unlock(&ip6_tnl_lock
);
473 ip4ip6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
474 int type
, int code
, int offset
, __be32 info
)
479 __u32 rel_info
= ntohl(info
);
481 struct sk_buff
*skb2
;
486 err
= ip6_tnl_err(skb
, IPPROTO_IPIP
, opt
, &rel_type
, &rel_code
,
487 &rel_msg
, &rel_info
, offset
);
495 case ICMPV6_DEST_UNREACH
:
496 if (rel_code
!= ICMPV6_ADDR_UNREACH
)
498 rel_type
= ICMP_DEST_UNREACH
;
499 rel_code
= ICMP_HOST_UNREACH
;
501 case ICMPV6_PKT_TOOBIG
:
504 rel_type
= ICMP_DEST_UNREACH
;
505 rel_code
= ICMP_FRAG_NEEDED
;
511 if (!pskb_may_pull(skb
, offset
+ sizeof(struct iphdr
)))
514 skb2
= skb_clone(skb
, GFP_ATOMIC
);
518 dst_release(skb2
->dst
);
520 skb_pull(skb2
, offset
);
521 skb_reset_network_header(skb2
);
524 /* Try to guess incoming interface */
525 memset(&fl
, 0, sizeof(fl
));
526 fl
.fl4_dst
= eiph
->saddr
;
527 fl
.fl4_tos
= RT_TOS(eiph
->tos
);
528 fl
.proto
= IPPROTO_IPIP
;
529 if (ip_route_output_key(&init_net
, &rt
, &fl
))
532 skb2
->dev
= rt
->u
.dst
.dev
;
534 /* route "incoming" packet */
535 if (rt
->rt_flags
& RTCF_LOCAL
) {
538 fl
.fl4_dst
= eiph
->daddr
;
539 fl
.fl4_src
= eiph
->saddr
;
540 fl
.fl4_tos
= eiph
->tos
;
541 if (ip_route_output_key(&init_net
, &rt
, &fl
) ||
542 rt
->u
.dst
.dev
->type
!= ARPHRD_TUNNEL
) {
546 skb2
->dst
= (struct dst_entry
*)rt
;
549 if (ip_route_input(skb2
, eiph
->daddr
, eiph
->saddr
, eiph
->tos
,
551 skb2
->dst
->dev
->type
!= ARPHRD_TUNNEL
)
555 /* change mtu on this route */
556 if (rel_type
== ICMP_DEST_UNREACH
&& rel_code
== ICMP_FRAG_NEEDED
) {
557 if (rel_info
> dst_mtu(skb2
->dst
))
560 skb2
->dst
->ops
->update_pmtu(skb2
->dst
, rel_info
);
563 icmp_send(skb2
, rel_type
, rel_code
, htonl(rel_info
));
571 ip6ip6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
572 int type
, int code
, int offset
, __be32 info
)
577 __u32 rel_info
= ntohl(info
);
580 err
= ip6_tnl_err(skb
, IPPROTO_IPV6
, opt
, &rel_type
, &rel_code
,
581 &rel_msg
, &rel_info
, offset
);
585 if (rel_msg
&& pskb_may_pull(skb
, offset
+ sizeof(struct ipv6hdr
))) {
587 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
592 dst_release(skb2
->dst
);
594 skb_pull(skb2
, offset
);
595 skb_reset_network_header(skb2
);
597 /* Try to guess incoming interface */
598 rt
= rt6_lookup(&ipv6_hdr(skb2
)->saddr
, NULL
, 0, 0);
600 if (rt
&& rt
->rt6i_dev
)
601 skb2
->dev
= rt
->rt6i_dev
;
603 icmpv6_send(skb2
, rel_type
, rel_code
, rel_info
, skb2
->dev
);
606 dst_release(&rt
->u
.dst
);
614 static void ip4ip6_dscp_ecn_decapsulate(struct ip6_tnl
*t
,
615 struct ipv6hdr
*ipv6h
,
618 __u8 dsfield
= ipv6_get_dsfield(ipv6h
) & ~INET_ECN_MASK
;
620 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
621 ipv4_change_dsfield(ip_hdr(skb
), INET_ECN_MASK
, dsfield
);
623 if (INET_ECN_is_ce(dsfield
))
624 IP_ECN_set_ce(ip_hdr(skb
));
627 static void ip6ip6_dscp_ecn_decapsulate(struct ip6_tnl
*t
,
628 struct ipv6hdr
*ipv6h
,
631 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
632 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h
), ipv6_hdr(skb
));
634 if (INET_ECN_is_ce(ipv6_get_dsfield(ipv6h
)))
635 IP6_ECN_set_ce(ipv6_hdr(skb
));
638 static inline int ip6_tnl_rcv_ctl(struct ip6_tnl
*t
)
640 struct ip6_tnl_parm
*p
= &t
->parms
;
643 if (p
->flags
& IP6_TNL_F_CAP_RCV
) {
644 struct net_device
*ldev
= NULL
;
647 ldev
= dev_get_by_index(&init_net
, p
->link
);
649 if ((ipv6_addr_is_multicast(&p
->laddr
) ||
650 likely(ipv6_chk_addr(&init_net
, &p
->laddr
, ldev
, 0))) &&
651 likely(!ipv6_chk_addr(&init_net
, &p
->raddr
, NULL
, 0)))
661 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
662 * @skb: received socket buffer
663 * @protocol: ethernet protocol ID
664 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
669 static int ip6_tnl_rcv(struct sk_buff
*skb
, __u16 protocol
,
671 void (*dscp_ecn_decapsulate
)(struct ip6_tnl
*t
,
672 struct ipv6hdr
*ipv6h
,
673 struct sk_buff
*skb
))
676 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
678 read_lock(&ip6_tnl_lock
);
680 if ((t
= ip6_tnl_lookup(&ipv6h
->saddr
, &ipv6h
->daddr
)) != NULL
) {
681 if (t
->parms
.proto
!= ipproto
&& t
->parms
.proto
!= 0) {
682 read_unlock(&ip6_tnl_lock
);
686 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
)) {
687 read_unlock(&ip6_tnl_lock
);
691 if (!ip6_tnl_rcv_ctl(t
)) {
692 t
->stat
.rx_dropped
++;
693 read_unlock(&ip6_tnl_lock
);
697 skb
->mac_header
= skb
->network_header
;
698 skb_reset_network_header(skb
);
699 skb
->protocol
= htons(protocol
);
700 skb
->pkt_type
= PACKET_HOST
;
701 memset(skb
->cb
, 0, sizeof(struct inet6_skb_parm
));
703 dst_release(skb
->dst
);
707 dscp_ecn_decapsulate(t
, ipv6h
, skb
);
709 t
->stat
.rx_packets
++;
710 t
->stat
.rx_bytes
+= skb
->len
;
712 read_unlock(&ip6_tnl_lock
);
715 read_unlock(&ip6_tnl_lock
);
723 static int ip4ip6_rcv(struct sk_buff
*skb
)
725 return ip6_tnl_rcv(skb
, ETH_P_IP
, IPPROTO_IPIP
,
726 ip4ip6_dscp_ecn_decapsulate
);
729 static int ip6ip6_rcv(struct sk_buff
*skb
)
731 return ip6_tnl_rcv(skb
, ETH_P_IPV6
, IPPROTO_IPV6
,
732 ip6ip6_dscp_ecn_decapsulate
);
735 struct ipv6_tel_txoption
{
736 struct ipv6_txoptions ops
;
740 static void init_tel_txopt(struct ipv6_tel_txoption
*opt
, __u8 encap_limit
)
742 memset(opt
, 0, sizeof(struct ipv6_tel_txoption
));
744 opt
->dst_opt
[2] = IPV6_TLV_TNL_ENCAP_LIMIT
;
746 opt
->dst_opt
[4] = encap_limit
;
747 opt
->dst_opt
[5] = IPV6_TLV_PADN
;
750 opt
->ops
.dst0opt
= (struct ipv6_opt_hdr
*) opt
->dst_opt
;
751 opt
->ops
.opt_nflen
= 8;
755 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
756 * @t: the outgoing tunnel device
757 * @hdr: IPv6 header from the incoming packet
760 * Avoid trivial tunneling loop by checking that tunnel exit-point
761 * doesn't match source of incoming packet.
769 ip6_tnl_addr_conflict(struct ip6_tnl
*t
, struct ipv6hdr
*hdr
)
771 return ipv6_addr_equal(&t
->parms
.raddr
, &hdr
->saddr
);
774 static inline int ip6_tnl_xmit_ctl(struct ip6_tnl
*t
)
776 struct ip6_tnl_parm
*p
= &t
->parms
;
779 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
780 struct net_device
*ldev
= NULL
;
783 ldev
= dev_get_by_index(&init_net
, p
->link
);
785 if (unlikely(!ipv6_chk_addr(&init_net
, &p
->laddr
, ldev
, 0)))
787 "%s xmit: Local address not yet configured!\n",
789 else if (!ipv6_addr_is_multicast(&p
->raddr
) &&
790 unlikely(ipv6_chk_addr(&init_net
, &p
->raddr
, NULL
, 0)))
792 "%s xmit: Routing loop! "
793 "Remote address found on this node!\n",
803 * ip6_tnl_xmit2 - encapsulate packet and send
804 * @skb: the outgoing socket buffer
805 * @dev: the outgoing tunnel device
806 * @dsfield: dscp code for outer header
807 * @fl: flow of tunneled packet
808 * @encap_limit: encapsulation limit
809 * @pmtu: Path MTU is stored if packet is too big
812 * Build new header and do some sanity checks on the packet before sending
818 * %-EMSGSIZE message too big. return mtu in this case.
821 static int ip6_tnl_xmit2(struct sk_buff
*skb
,
822 struct net_device
*dev
,
828 struct ip6_tnl
*t
= netdev_priv(dev
);
829 struct net_device_stats
*stats
= &t
->stat
;
830 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
831 struct ipv6_tel_txoption opt
;
832 struct dst_entry
*dst
;
833 struct net_device
*tdev
;
835 unsigned int max_headroom
= sizeof(struct ipv6hdr
);
840 if ((dst
= ip6_tnl_dst_check(t
)) != NULL
)
843 dst
= ip6_route_output(NULL
, fl
);
845 if (dst
->error
|| xfrm_lookup(&dst
, fl
, NULL
, 0) < 0)
846 goto tx_err_link_failure
;
855 "%s: Local routing loop detected!\n",
857 goto tx_err_dst_release
;
859 mtu
= dst_mtu(dst
) - sizeof (*ipv6h
);
860 if (encap_limit
>= 0) {
864 if (mtu
< IPV6_MIN_MTU
)
867 skb
->dst
->ops
->update_pmtu(skb
->dst
, mtu
);
868 if (skb
->len
> mtu
) {
871 goto tx_err_dst_release
;
875 * Okay, now see if we can stuff it in the buffer as-is.
877 max_headroom
+= LL_RESERVED_SPACE(tdev
);
879 if (skb_headroom(skb
) < max_headroom
|| skb_shared(skb
) ||
880 (skb_cloned(skb
) && !skb_clone_writable(skb
, 0))) {
881 struct sk_buff
*new_skb
;
883 if (!(new_skb
= skb_realloc_headroom(skb
, max_headroom
)))
884 goto tx_err_dst_release
;
887 skb_set_owner_w(new_skb
, skb
->sk
);
891 dst_release(skb
->dst
);
892 skb
->dst
= dst_clone(dst
);
894 skb
->transport_header
= skb
->network_header
;
897 if (encap_limit
>= 0) {
898 init_tel_txopt(&opt
, encap_limit
);
899 ipv6_push_nfrag_opts(skb
, &opt
.ops
, &proto
, NULL
);
901 skb_push(skb
, sizeof(struct ipv6hdr
));
902 skb_reset_network_header(skb
);
903 ipv6h
= ipv6_hdr(skb
);
904 *(__be32
*)ipv6h
= fl
->fl6_flowlabel
| htonl(0x60000000);
905 dsfield
= INET_ECN_encapsulate(0, dsfield
);
906 ipv6_change_dsfield(ipv6h
, ~INET_ECN_MASK
, dsfield
);
907 ipv6h
->hop_limit
= t
->parms
.hop_limit
;
908 ipv6h
->nexthdr
= proto
;
909 ipv6_addr_copy(&ipv6h
->saddr
, &fl
->fl6_src
);
910 ipv6_addr_copy(&ipv6h
->daddr
, &fl
->fl6_dst
);
913 err
= ip6_local_out(skb
);
915 if (net_xmit_eval(err
) == 0) {
916 stats
->tx_bytes
+= pkt_len
;
920 stats
->tx_aborted_errors
++;
922 ip6_tnl_dst_store(t
, dst
);
925 stats
->tx_carrier_errors
++;
926 dst_link_failure(skb
);
933 ip4ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
935 struct ip6_tnl
*t
= netdev_priv(dev
);
936 struct iphdr
*iph
= ip_hdr(skb
);
937 int encap_limit
= -1;
943 if ((t
->parms
.proto
!= IPPROTO_IPIP
&& t
->parms
.proto
!= 0) ||
944 !ip6_tnl_xmit_ctl(t
))
947 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
948 encap_limit
= t
->parms
.encap_limit
;
950 memcpy(&fl
, &t
->fl
, sizeof (fl
));
951 fl
.proto
= IPPROTO_IPIP
;
953 dsfield
= ipv4_get_dsfield(iph
);
955 if ((t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
))
956 fl
.fl6_flowlabel
|= htonl((__u32
)iph
->tos
<< IPV6_TCLASS_SHIFT
)
959 err
= ip6_tnl_xmit2(skb
, dev
, dsfield
, &fl
, encap_limit
, &mtu
);
961 /* XXX: send ICMP error even if DF is not set. */
962 if (err
== -EMSGSIZE
)
963 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
972 ip6ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
974 struct ip6_tnl
*t
= netdev_priv(dev
);
975 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
976 int encap_limit
= -1;
983 if ((t
->parms
.proto
!= IPPROTO_IPV6
&& t
->parms
.proto
!= 0) ||
984 !ip6_tnl_xmit_ctl(t
) || ip6_tnl_addr_conflict(t
, ipv6h
))
987 offset
= parse_tlv_tnl_enc_lim(skb
, skb_network_header(skb
));
989 struct ipv6_tlv_tnl_enc_lim
*tel
;
990 tel
= (struct ipv6_tlv_tnl_enc_lim
*)&skb_network_header(skb
)[offset
];
991 if (tel
->encap_limit
== 0) {
992 icmpv6_send(skb
, ICMPV6_PARAMPROB
,
993 ICMPV6_HDR_FIELD
, offset
+ 2, skb
->dev
);
996 encap_limit
= tel
->encap_limit
- 1;
997 } else if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
998 encap_limit
= t
->parms
.encap_limit
;
1000 memcpy(&fl
, &t
->fl
, sizeof (fl
));
1001 fl
.proto
= IPPROTO_IPV6
;
1003 dsfield
= ipv6_get_dsfield(ipv6h
);
1004 if ((t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
))
1005 fl
.fl6_flowlabel
|= (*(__be32
*) ipv6h
& IPV6_TCLASS_MASK
);
1006 if ((t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
))
1007 fl
.fl6_flowlabel
|= (*(__be32
*) ipv6h
& IPV6_FLOWLABEL_MASK
);
1009 err
= ip6_tnl_xmit2(skb
, dev
, dsfield
, &fl
, encap_limit
, &mtu
);
1011 if (err
== -EMSGSIZE
)
1012 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
, dev
);
1020 ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1022 struct ip6_tnl
*t
= netdev_priv(dev
);
1023 struct net_device_stats
*stats
= &t
->stat
;
1026 if (t
->recursion
++) {
1027 t
->stat
.collisions
++;
1031 switch (skb
->protocol
) {
1032 case __constant_htons(ETH_P_IP
):
1033 ret
= ip4ip6_tnl_xmit(skb
, dev
);
1035 case __constant_htons(ETH_P_IPV6
):
1036 ret
= ip6ip6_tnl_xmit(skb
, dev
);
1050 stats
->tx_dropped
++;
1056 static void ip6_tnl_set_cap(struct ip6_tnl
*t
)
1058 struct ip6_tnl_parm
*p
= &t
->parms
;
1059 int ltype
= ipv6_addr_type(&p
->laddr
);
1060 int rtype
= ipv6_addr_type(&p
->raddr
);
1062 p
->flags
&= ~(IP6_TNL_F_CAP_XMIT
|IP6_TNL_F_CAP_RCV
);
1064 if (ltype
& (IPV6_ADDR_UNICAST
|IPV6_ADDR_MULTICAST
) &&
1065 rtype
& (IPV6_ADDR_UNICAST
|IPV6_ADDR_MULTICAST
) &&
1066 !((ltype
|rtype
) & IPV6_ADDR_LOOPBACK
) &&
1067 (!((ltype
|rtype
) & IPV6_ADDR_LINKLOCAL
) || p
->link
)) {
1068 if (ltype
&IPV6_ADDR_UNICAST
)
1069 p
->flags
|= IP6_TNL_F_CAP_XMIT
;
1070 if (rtype
&IPV6_ADDR_UNICAST
)
1071 p
->flags
|= IP6_TNL_F_CAP_RCV
;
1075 static void ip6_tnl_link_config(struct ip6_tnl
*t
)
1077 struct net_device
*dev
= t
->dev
;
1078 struct ip6_tnl_parm
*p
= &t
->parms
;
1079 struct flowi
*fl
= &t
->fl
;
1081 memcpy(&dev
->dev_addr
, &p
->laddr
, sizeof(struct in6_addr
));
1082 memcpy(&dev
->broadcast
, &p
->raddr
, sizeof(struct in6_addr
));
1084 /* Set up flowi template */
1085 ipv6_addr_copy(&fl
->fl6_src
, &p
->laddr
);
1086 ipv6_addr_copy(&fl
->fl6_dst
, &p
->raddr
);
1088 fl
->fl6_flowlabel
= 0;
1090 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_TCLASS
))
1091 fl
->fl6_flowlabel
|= IPV6_TCLASS_MASK
& p
->flowinfo
;
1092 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_FLOWLABEL
))
1093 fl
->fl6_flowlabel
|= IPV6_FLOWLABEL_MASK
& p
->flowinfo
;
1097 if (p
->flags
&IP6_TNL_F_CAP_XMIT
&& p
->flags
&IP6_TNL_F_CAP_RCV
)
1098 dev
->flags
|= IFF_POINTOPOINT
;
1100 dev
->flags
&= ~IFF_POINTOPOINT
;
1102 dev
->iflink
= p
->link
;
1104 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
1105 int strict
= (ipv6_addr_type(&p
->raddr
) &
1106 (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
));
1108 struct rt6_info
*rt
= rt6_lookup(&p
->raddr
, &p
->laddr
,
1115 dev
->hard_header_len
= rt
->rt6i_dev
->hard_header_len
+
1116 sizeof (struct ipv6hdr
);
1118 dev
->mtu
= rt
->rt6i_dev
->mtu
- sizeof (struct ipv6hdr
);
1120 if (dev
->mtu
< IPV6_MIN_MTU
)
1121 dev
->mtu
= IPV6_MIN_MTU
;
1123 dst_release(&rt
->u
.dst
);
1128 * ip6_tnl_change - update the tunnel parameters
1129 * @t: tunnel to be changed
1130 * @p: tunnel configuration parameters
1131 * @active: != 0 if tunnel is ready for use
1134 * ip6_tnl_change() updates the tunnel parameters
1138 ip6_tnl_change(struct ip6_tnl
*t
, struct ip6_tnl_parm
*p
)
1140 ipv6_addr_copy(&t
->parms
.laddr
, &p
->laddr
);
1141 ipv6_addr_copy(&t
->parms
.raddr
, &p
->raddr
);
1142 t
->parms
.flags
= p
->flags
;
1143 t
->parms
.hop_limit
= p
->hop_limit
;
1144 t
->parms
.encap_limit
= p
->encap_limit
;
1145 t
->parms
.flowinfo
= p
->flowinfo
;
1146 t
->parms
.link
= p
->link
;
1147 t
->parms
.proto
= p
->proto
;
1148 ip6_tnl_dst_reset(t
);
1149 ip6_tnl_link_config(t
);
1154 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1155 * @dev: virtual device associated with tunnel
1156 * @ifr: parameters passed from userspace
1157 * @cmd: command to be performed
1160 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1163 * The possible commands are the following:
1164 * %SIOCGETTUNNEL: get tunnel parameters for device
1165 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1166 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1167 * %SIOCDELTUNNEL: delete tunnel
1169 * The fallback device "ip6tnl0", created during module
1170 * initialization, can be used for creating other tunnel devices.
1174 * %-EFAULT if unable to copy data to or from userspace,
1175 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1176 * %-EINVAL if passed tunnel parameters are invalid,
1177 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1178 * %-ENODEV if attempting to change or delete a nonexisting device
1182 ip6_tnl_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1185 struct ip6_tnl_parm p
;
1186 struct ip6_tnl
*t
= NULL
;
1190 if (dev
== ip6_fb_tnl_dev
) {
1191 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof (p
))) {
1195 t
= ip6_tnl_locate(&p
, 0);
1198 t
= netdev_priv(dev
);
1199 memcpy(&p
, &t
->parms
, sizeof (p
));
1200 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof (p
))) {
1207 if (!capable(CAP_NET_ADMIN
))
1210 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof (p
)))
1213 if (p
.proto
!= IPPROTO_IPV6
&& p
.proto
!= IPPROTO_IPIP
&&
1216 t
= ip6_tnl_locate(&p
, cmd
== SIOCADDTUNNEL
);
1217 if (dev
!= ip6_fb_tnl_dev
&& cmd
== SIOCCHGTUNNEL
) {
1219 if (t
->dev
!= dev
) {
1224 t
= netdev_priv(dev
);
1227 err
= ip6_tnl_change(t
, &p
);
1229 netdev_state_change(dev
);
1233 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &t
->parms
, sizeof (p
)))
1237 err
= (cmd
== SIOCADDTUNNEL
? -ENOBUFS
: -ENOENT
);
1241 if (!capable(CAP_NET_ADMIN
))
1244 if (dev
== ip6_fb_tnl_dev
) {
1246 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof (p
)))
1249 if ((t
= ip6_tnl_locate(&p
, 0)) == NULL
)
1252 if (t
->dev
== ip6_fb_tnl_dev
)
1257 unregister_netdevice(dev
);
1266 * ip6_tnl_get_stats - return the stats for tunnel device
1267 * @dev: virtual device associated with tunnel
1269 * Return: stats for device
1272 static struct net_device_stats
*
1273 ip6_tnl_get_stats(struct net_device
*dev
)
1275 return &(((struct ip6_tnl
*)netdev_priv(dev
))->stat
);
1279 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1280 * @dev: virtual device associated with tunnel
1281 * @new_mtu: the new mtu
1285 * %-EINVAL if mtu too small
1289 ip6_tnl_change_mtu(struct net_device
*dev
, int new_mtu
)
1291 if (new_mtu
< IPV6_MIN_MTU
) {
1299 * ip6_tnl_dev_setup - setup virtual tunnel device
1300 * @dev: virtual device associated with tunnel
1303 * Initialize function pointers and device parameters
1306 static void ip6_tnl_dev_setup(struct net_device
*dev
)
1308 dev
->uninit
= ip6_tnl_dev_uninit
;
1309 dev
->destructor
= free_netdev
;
1310 dev
->hard_start_xmit
= ip6_tnl_xmit
;
1311 dev
->get_stats
= ip6_tnl_get_stats
;
1312 dev
->do_ioctl
= ip6_tnl_ioctl
;
1313 dev
->change_mtu
= ip6_tnl_change_mtu
;
1315 dev
->type
= ARPHRD_TUNNEL6
;
1316 dev
->hard_header_len
= LL_MAX_HEADER
+ sizeof (struct ipv6hdr
);
1317 dev
->mtu
= ETH_DATA_LEN
- sizeof (struct ipv6hdr
);
1318 dev
->flags
|= IFF_NOARP
;
1319 dev
->addr_len
= sizeof(struct in6_addr
);
1324 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1325 * @dev: virtual device associated with tunnel
1329 ip6_tnl_dev_init_gen(struct net_device
*dev
)
1331 struct ip6_tnl
*t
= netdev_priv(dev
);
1333 strcpy(t
->parms
.name
, dev
->name
);
1337 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1338 * @dev: virtual device associated with tunnel
1342 ip6_tnl_dev_init(struct net_device
*dev
)
1344 struct ip6_tnl
*t
= netdev_priv(dev
);
1345 ip6_tnl_dev_init_gen(dev
);
1346 ip6_tnl_link_config(t
);
1351 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1352 * @dev: fallback device
1358 ip6_fb_tnl_dev_init(struct net_device
*dev
)
1360 struct ip6_tnl
*t
= netdev_priv(dev
);
1361 ip6_tnl_dev_init_gen(dev
);
1362 t
->parms
.proto
= IPPROTO_IPV6
;
1368 static struct xfrm6_tunnel ip4ip6_handler
= {
1369 .handler
= ip4ip6_rcv
,
1370 .err_handler
= ip4ip6_err
,
1374 static struct xfrm6_tunnel ip6ip6_handler
= {
1375 .handler
= ip6ip6_rcv
,
1376 .err_handler
= ip6ip6_err
,
1381 * ip6_tunnel_init - register protocol and reserve needed resources
1383 * Return: 0 on success
1386 static int __init
ip6_tunnel_init(void)
1390 if (xfrm6_tunnel_register(&ip4ip6_handler
, AF_INET
)) {
1391 printk(KERN_ERR
"ip6_tunnel init: can't register ip4ip6\n");
1396 if (xfrm6_tunnel_register(&ip6ip6_handler
, AF_INET6
)) {
1397 printk(KERN_ERR
"ip6_tunnel init: can't register ip6ip6\n");
1401 ip6_fb_tnl_dev
= alloc_netdev(sizeof(struct ip6_tnl
), "ip6tnl0",
1404 if (!ip6_fb_tnl_dev
) {
1408 ip6_fb_tnl_dev
->init
= ip6_fb_tnl_dev_init
;
1410 if ((err
= register_netdev(ip6_fb_tnl_dev
))) {
1411 free_netdev(ip6_fb_tnl_dev
);
1416 xfrm6_tunnel_deregister(&ip6ip6_handler
, AF_INET6
);
1418 xfrm6_tunnel_deregister(&ip4ip6_handler
, AF_INET
);
1423 static void __exit
ip6_tnl_destroy_tunnels(void)
1428 for (h
= 0; h
< HASH_SIZE
; h
++) {
1429 while ((t
= tnls_r_l
[h
]) != NULL
)
1430 unregister_netdevice(t
->dev
);
1434 unregister_netdevice(t
->dev
);
1438 * ip6_tunnel_cleanup - free resources and unregister protocol
1441 static void __exit
ip6_tunnel_cleanup(void)
1443 if (xfrm6_tunnel_deregister(&ip4ip6_handler
, AF_INET
))
1444 printk(KERN_INFO
"ip6_tunnel close: can't deregister ip4ip6\n");
1446 if (xfrm6_tunnel_deregister(&ip6ip6_handler
, AF_INET6
))
1447 printk(KERN_INFO
"ip6_tunnel close: can't deregister ip6ip6\n");
1450 ip6_tnl_destroy_tunnels();
1454 module_init(ip6_tunnel_init
);
1455 module_exit(ip6_tunnel_cleanup
);