2 * IPv6 over IPv6 tunnel device
3 * Linux INET6 implementation
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
11 * linux/net/ipv6/sit.c
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
22 #include <linux/config.h>
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>
31 #include <linux/if_tunnel.h>
32 #include <linux/net.h>
33 #include <linux/in6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/icmpv6.h>
37 #include <linux/init.h>
38 #include <linux/route.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/netfilter_ipv6.h>
42 #include <asm/uaccess.h>
43 #include <asm/atomic.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 #include <net/ip6_tunnel.h>
51 #include <net/dsfield.h>
52 #include <net/inet_ecn.h>
54 MODULE_AUTHOR("Ville Nuorvala");
55 MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");
56 MODULE_LICENSE("GPL");
58 #define IPV6_TLV_TEL_DST_SIZE 8
61 #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)
63 #define IP6_TNL_TRACE(x...) do {;} while(0)
66 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
70 #define HASH(addr) (((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \
71 (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \
74 static int ip6ip6_fb_tnl_dev_init(struct net_device
*dev
);
75 static int ip6ip6_tnl_dev_init(struct net_device
*dev
);
76 static void ip6ip6_tnl_dev_setup(struct net_device
*dev
);
78 /* the IPv6 tunnel fallback device */
79 static struct net_device
*ip6ip6_fb_tnl_dev
;
82 /* lists for storing tunnels in use */
83 static struct ip6_tnl
*tnls_r_l
[HASH_SIZE
];
84 static struct ip6_tnl
*tnls_wc
[1];
85 static struct ip6_tnl
**tnls
[2] = { tnls_wc
, tnls_r_l
};
87 /* lock for the tunnel lists */
88 static DEFINE_RWLOCK(ip6ip6_lock
);
90 static inline struct dst_entry
*ip6_tnl_dst_check(struct ip6_tnl
*t
)
92 struct dst_entry
*dst
= t
->dst_cache
;
94 if (dst
&& dst
->obsolete
&&
95 dst
->ops
->check(dst
, t
->dst_cookie
) == NULL
) {
104 static inline void ip6_tnl_dst_reset(struct ip6_tnl
*t
)
106 dst_release(t
->dst_cache
);
110 static inline void ip6_tnl_dst_store(struct ip6_tnl
*t
, struct dst_entry
*dst
)
112 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
113 t
->dst_cookie
= rt
->rt6i_node
? rt
->rt6i_node
->fn_sernum
: 0;
114 dst_release(t
->dst_cache
);
119 * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses
120 * @remote: the address of the tunnel exit-point
121 * @local: the address of the tunnel entry-point
124 * tunnel matching given end-points if found,
125 * else fallback tunnel if its device is up,
129 static struct ip6_tnl
*
130 ip6ip6_tnl_lookup(struct in6_addr
*remote
, struct in6_addr
*local
)
132 unsigned h0
= HASH(remote
);
133 unsigned h1
= HASH(local
);
136 for (t
= tnls_r_l
[h0
^ h1
]; t
; t
= t
->next
) {
137 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
138 ipv6_addr_equal(remote
, &t
->parms
.raddr
) &&
139 (t
->dev
->flags
& IFF_UP
))
142 if ((t
= tnls_wc
[0]) != NULL
&& (t
->dev
->flags
& IFF_UP
))
149 * ip6ip6_bucket - get head of list matching given tunnel parameters
150 * @p: parameters containing tunnel end-points
153 * ip6ip6_bucket() returns the head of the list matching the
154 * &struct in6_addr entries laddr and raddr in @p.
156 * Return: head of IPv6 tunnel list
159 static struct ip6_tnl
**
160 ip6ip6_bucket(struct ip6_tnl_parm
*p
)
162 struct in6_addr
*remote
= &p
->raddr
;
163 struct in6_addr
*local
= &p
->laddr
;
167 if (!ipv6_addr_any(remote
) || !ipv6_addr_any(local
)) {
169 h
= HASH(remote
) ^ HASH(local
);
171 return &tnls
[prio
][h
];
175 * ip6ip6_tnl_link - add tunnel to hash table
176 * @t: tunnel to be added
180 ip6ip6_tnl_link(struct ip6_tnl
*t
)
182 struct ip6_tnl
**tp
= ip6ip6_bucket(&t
->parms
);
185 write_lock_bh(&ip6ip6_lock
);
187 write_unlock_bh(&ip6ip6_lock
);
191 * ip6ip6_tnl_unlink - remove tunnel from hash table
192 * @t: tunnel to be removed
196 ip6ip6_tnl_unlink(struct ip6_tnl
*t
)
200 for (tp
= ip6ip6_bucket(&t
->parms
); *tp
; tp
= &(*tp
)->next
) {
202 write_lock_bh(&ip6ip6_lock
);
204 write_unlock_bh(&ip6ip6_lock
);
211 * ip6_tnl_create() - create a new tunnel
212 * @p: tunnel parameters
213 * @pt: pointer to new tunnel
216 * Create tunnel matching given parameters.
223 ip6_tnl_create(struct ip6_tnl_parm
*p
, struct ip6_tnl
**pt
)
225 struct net_device
*dev
;
231 strlcpy(name
, p
->name
, IFNAMSIZ
);
234 for (i
= 1; i
< IP6_TNL_MAX
; i
++) {
235 sprintf(name
, "ip6tnl%d", i
);
236 if (__dev_get_by_name(name
) == NULL
)
239 if (i
== IP6_TNL_MAX
)
242 dev
= alloc_netdev(sizeof (*t
), name
, ip6ip6_tnl_dev_setup
);
246 t
= netdev_priv(dev
);
247 dev
->init
= ip6ip6_tnl_dev_init
;
250 if ((err
= register_netdevice(dev
)) < 0) {
262 * ip6ip6_tnl_locate - find or create tunnel matching given parameters
263 * @p: tunnel parameters
264 * @create: != 0 if allowed to create new tunnel if no match found
267 * ip6ip6_tnl_locate() first tries to locate an existing tunnel
268 * based on @parms. If this is unsuccessful, but @create is set a new
269 * tunnel device is created and registered for use.
272 * 0 if tunnel located or created,
273 * -EINVAL if parameters incorrect,
274 * -ENODEV if no matching tunnel available
278 ip6ip6_tnl_locate(struct ip6_tnl_parm
*p
, struct ip6_tnl
**pt
, int create
)
280 struct in6_addr
*remote
= &p
->raddr
;
281 struct in6_addr
*local
= &p
->laddr
;
284 if (p
->proto
!= IPPROTO_IPV6
)
287 for (t
= *ip6ip6_bucket(p
); t
; t
= t
->next
) {
288 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
289 ipv6_addr_equal(remote
, &t
->parms
.raddr
)) {
291 return (create
? -EEXIST
: 0);
297 return ip6_tnl_create(p
, pt
);
301 * ip6ip6_tnl_dev_uninit - tunnel device uninitializer
302 * @dev: the device to be destroyed
305 * ip6ip6_tnl_dev_uninit() removes tunnel from its list
309 ip6ip6_tnl_dev_uninit(struct net_device
*dev
)
311 struct ip6_tnl
*t
= netdev_priv(dev
);
313 if (dev
== ip6ip6_fb_tnl_dev
) {
314 write_lock_bh(&ip6ip6_lock
);
316 write_unlock_bh(&ip6ip6_lock
);
318 ip6ip6_tnl_unlink(t
);
320 ip6_tnl_dst_reset(t
);
325 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
326 * @skb: received socket buffer
329 * 0 if none was found,
330 * else index to encapsulation limit
334 parse_tlv_tnl_enc_lim(struct sk_buff
*skb
, __u8
* raw
)
336 struct ipv6hdr
*ipv6h
= (struct ipv6hdr
*) raw
;
337 __u8 nexthdr
= ipv6h
->nexthdr
;
338 __u16 off
= sizeof (*ipv6h
);
340 while (ipv6_ext_hdr(nexthdr
) && nexthdr
!= NEXTHDR_NONE
) {
342 struct ipv6_opt_hdr
*hdr
;
343 if (raw
+ off
+ sizeof (*hdr
) > skb
->data
&&
344 !pskb_may_pull(skb
, raw
- skb
->data
+ off
+ sizeof (*hdr
)))
347 hdr
= (struct ipv6_opt_hdr
*) (raw
+ off
);
348 if (nexthdr
== NEXTHDR_FRAGMENT
) {
349 struct frag_hdr
*frag_hdr
= (struct frag_hdr
*) hdr
;
350 if (frag_hdr
->frag_off
)
353 } else if (nexthdr
== NEXTHDR_AUTH
) {
354 optlen
= (hdr
->hdrlen
+ 2) << 2;
356 optlen
= ipv6_optlen(hdr
);
358 if (nexthdr
== NEXTHDR_DEST
) {
361 struct ipv6_tlv_tnl_enc_lim
*tel
;
363 /* No more room for encapsulation limit */
364 if (i
+ sizeof (*tel
) > off
+ optlen
)
367 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &raw
[i
];
368 /* return index of option if found and valid */
369 if (tel
->type
== IPV6_TLV_TNL_ENCAP_LIMIT
&&
372 /* else jump to next option */
374 i
+= tel
->length
+ 2;
379 nexthdr
= hdr
->nexthdr
;
386 * ip6ip6_err - tunnel error handler
389 * ip6ip6_err() should handle errors in the tunnel according
390 * to the specifications in RFC 2473.
394 ip6ip6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
395 int type
, int code
, int offset
, __u32 info
)
397 struct ipv6hdr
*ipv6h
= (struct ipv6hdr
*) skb
->data
;
400 int rel_type
= ICMPV6_DEST_UNREACH
;
401 int rel_code
= ICMPV6_ADDR_UNREACH
;
406 /* If the packet doesn't contain the original IPv6 header we are
407 in trouble since we might need the source address for further
408 processing of the error. */
410 read_lock(&ip6ip6_lock
);
411 if ((t
= ip6ip6_tnl_lookup(&ipv6h
->daddr
, &ipv6h
->saddr
)) == NULL
)
418 struct ipv6_tlv_tnl_enc_lim
*tel
;
420 case ICMPV6_DEST_UNREACH
:
423 "%s: Path to destination invalid "
424 "or inactive!\n", t
->parms
.name
);
427 case ICMPV6_TIME_EXCEED
:
428 if (code
== ICMPV6_EXC_HOPLIMIT
) {
431 "%s: Too small hop limit or "
432 "routing loop in tunnel!\n",
437 case ICMPV6_PARAMPROB
:
438 /* ignore if parameter problem not caused by a tunnel
439 encapsulation limit sub-option */
440 if (code
!= ICMPV6_HDR_FIELD
) {
443 teli
= parse_tlv_tnl_enc_lim(skb
, skb
->data
);
445 if (teli
&& teli
== ntohl(info
) - 2) {
446 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &skb
->data
[teli
];
447 if (tel
->encap_limit
== 0) {
450 "%s: Too small encapsulation "
451 "limit or routing loop in "
452 "tunnel!\n", t
->parms
.name
);
457 case ICMPV6_PKT_TOOBIG
:
458 mtu
= ntohl(info
) - offset
;
459 if (mtu
< IPV6_MIN_MTU
)
463 if ((len
= sizeof (*ipv6h
) + ntohs(ipv6h
->payload_len
)) > mtu
) {
464 rel_type
= ICMPV6_PKT_TOOBIG
;
471 if (rel_msg
&& pskb_may_pull(skb
, offset
+ sizeof (*ipv6h
))) {
473 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
477 dst_release(skb2
->dst
);
479 skb_pull(skb2
, offset
);
480 skb2
->nh
.raw
= skb2
->data
;
482 /* Try to guess incoming interface */
483 rt
= rt6_lookup(&skb2
->nh
.ipv6h
->saddr
, NULL
, 0, 0);
485 if (rt
&& rt
->rt6i_dev
)
486 skb2
->dev
= rt
->rt6i_dev
;
488 icmpv6_send(skb2
, rel_type
, rel_code
, rel_info
, skb2
->dev
);
491 dst_release(&rt
->u
.dst
);
496 read_unlock(&ip6ip6_lock
);
500 static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr
*outer_iph
,
503 struct ipv6hdr
*inner_iph
= skb
->nh
.ipv6h
;
505 if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph
)))
506 IP6_ECN_set_ce(inner_iph
);
510 * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally
511 * @skb: received socket buffer
517 ip6ip6_rcv(struct sk_buff
*skb
)
519 struct ipv6hdr
*ipv6h
;
522 if (!pskb_may_pull(skb
, sizeof (*ipv6h
)))
525 ipv6h
= skb
->nh
.ipv6h
;
527 read_lock(&ip6ip6_lock
);
529 if ((t
= ip6ip6_tnl_lookup(&ipv6h
->saddr
, &ipv6h
->daddr
)) != NULL
) {
530 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
)) {
531 read_unlock(&ip6ip6_lock
);
536 if (!(t
->parms
.flags
& IP6_TNL_F_CAP_RCV
)) {
537 t
->stat
.rx_dropped
++;
538 read_unlock(&ip6ip6_lock
);
542 skb
->mac
.raw
= skb
->nh
.raw
;
543 skb
->nh
.raw
= skb
->data
;
544 skb
->protocol
= htons(ETH_P_IPV6
);
545 skb
->pkt_type
= PACKET_HOST
;
546 memset(skb
->cb
, 0, sizeof(struct inet6_skb_parm
));
548 dst_release(skb
->dst
);
550 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
551 ipv6_copy_dscp(ipv6h
, skb
->nh
.ipv6h
);
552 ip6ip6_ecn_decapsulate(ipv6h
, skb
);
553 t
->stat
.rx_packets
++;
554 t
->stat
.rx_bytes
+= skb
->len
;
556 read_unlock(&ip6ip6_lock
);
559 read_unlock(&ip6ip6_lock
);
560 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_ADDR_UNREACH
, 0, skb
->dev
);
565 static inline struct ipv6_txoptions
*create_tel(__u8 encap_limit
)
567 struct ipv6_tlv_tnl_enc_lim
*tel
;
568 struct ipv6_txoptions
*opt
;
571 int opt_len
= sizeof(*opt
) + 8;
573 if (!(opt
= kmalloc(opt_len
, GFP_ATOMIC
))) {
576 memset(opt
, 0, opt_len
);
577 opt
->tot_len
= opt_len
;
578 opt
->dst0opt
= (struct ipv6_opt_hdr
*) (opt
+ 1);
581 tel
= (struct ipv6_tlv_tnl_enc_lim
*) (opt
->dst0opt
+ 1);
582 tel
->type
= IPV6_TLV_TNL_ENCAP_LIMIT
;
584 tel
->encap_limit
= encap_limit
;
586 raw
= (__u8
*) opt
->dst0opt
;
587 raw
[5] = IPV6_TLV_PADN
;
594 * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
595 * @t: the outgoing tunnel device
596 * @hdr: IPv6 header from the incoming packet
599 * Avoid trivial tunneling loop by checking that tunnel exit-point
600 * doesn't match source of incoming packet.
608 ip6ip6_tnl_addr_conflict(struct ip6_tnl
*t
, struct ipv6hdr
*hdr
)
610 return ipv6_addr_equal(&t
->parms
.raddr
, &hdr
->saddr
);
614 * ip6ip6_tnl_xmit - encapsulate packet and send
615 * @skb: the outgoing socket buffer
616 * @dev: the outgoing tunnel device
619 * Build new header and do some sanity checks on the packet before sending
627 ip6ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
629 struct ip6_tnl
*t
= netdev_priv(dev
);
630 struct net_device_stats
*stats
= &t
->stat
;
631 struct ipv6hdr
*ipv6h
= skb
->nh
.ipv6h
;
632 struct ipv6_txoptions
*opt
= NULL
;
633 int encap_limit
= -1;
636 struct dst_entry
*dst
;
637 struct net_device
*tdev
;
639 int max_headroom
= sizeof(struct ipv6hdr
);
645 if (t
->recursion
++) {
649 if (skb
->protocol
!= htons(ETH_P_IPV6
) ||
650 !(t
->parms
.flags
& IP6_TNL_F_CAP_XMIT
) ||
651 ip6ip6_tnl_addr_conflict(t
, ipv6h
)) {
654 if ((offset
= parse_tlv_tnl_enc_lim(skb
, skb
->nh
.raw
)) > 0) {
655 struct ipv6_tlv_tnl_enc_lim
*tel
;
656 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &skb
->nh
.raw
[offset
];
657 if (tel
->encap_limit
== 0) {
658 icmpv6_send(skb
, ICMPV6_PARAMPROB
,
659 ICMPV6_HDR_FIELD
, offset
+ 2, skb
->dev
);
662 encap_limit
= tel
->encap_limit
- 1;
663 } else if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)) {
664 encap_limit
= t
->parms
.encap_limit
;
666 memcpy(&fl
, &t
->fl
, sizeof (fl
));
669 dsfield
= ipv6_get_dsfield(ipv6h
);
670 if ((t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
))
671 fl
.fl6_flowlabel
|= (*(__u32
*) ipv6h
& IPV6_TCLASS_MASK
);
672 if ((t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
))
673 fl
.fl6_flowlabel
|= (*(__u32
*) ipv6h
& IPV6_FLOWLABEL_MASK
);
675 if (encap_limit
>= 0 && (opt
= create_tel(encap_limit
)) == NULL
)
678 if ((dst
= ip6_tnl_dst_check(t
)) != NULL
)
681 dst
= ip6_route_output(NULL
, &fl
);
683 if (dst
->error
|| xfrm_lookup(&dst
, &fl
, NULL
, 0) < 0)
684 goto tx_err_link_failure
;
693 "%s: Local routing loop detected!\n",
695 goto tx_err_dst_release
;
697 mtu
= dst_mtu(dst
) - sizeof (*ipv6h
);
702 if (mtu
< IPV6_MIN_MTU
)
704 if (skb
->dst
&& mtu
< dst_mtu(skb
->dst
)) {
705 struct rt6_info
*rt
= (struct rt6_info
*) skb
->dst
;
706 rt
->rt6i_flags
|= RTF_MODIFIED
;
707 rt
->u
.dst
.metrics
[RTAX_MTU
-1] = mtu
;
709 if (skb
->len
> mtu
) {
710 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
, dev
);
711 goto tx_err_dst_release
;
715 * Okay, now see if we can stuff it in the buffer as-is.
717 max_headroom
+= LL_RESERVED_SPACE(tdev
);
719 if (skb_headroom(skb
) < max_headroom
||
720 skb_cloned(skb
) || skb_shared(skb
)) {
721 struct sk_buff
*new_skb
;
723 if (!(new_skb
= skb_realloc_headroom(skb
, max_headroom
)))
724 goto tx_err_dst_release
;
727 skb_set_owner_w(new_skb
, skb
->sk
);
731 dst_release(skb
->dst
);
732 skb
->dst
= dst_clone(dst
);
734 skb
->h
.raw
= skb
->nh
.raw
;
737 ipv6_push_nfrag_opts(skb
, opt
, &proto
, NULL
);
739 skb
->nh
.raw
= skb_push(skb
, sizeof(struct ipv6hdr
));
740 ipv6h
= skb
->nh
.ipv6h
;
741 *(u32
*)ipv6h
= fl
.fl6_flowlabel
| htonl(0x60000000);
742 dsfield
= INET_ECN_encapsulate(0, dsfield
);
743 ipv6_change_dsfield(ipv6h
, ~INET_ECN_MASK
, dsfield
);
744 ipv6h
->payload_len
= htons(skb
->len
- sizeof(struct ipv6hdr
));
745 ipv6h
->hop_limit
= t
->parms
.hop_limit
;
746 ipv6h
->nexthdr
= proto
;
747 ipv6_addr_copy(&ipv6h
->saddr
, &fl
.fl6_src
);
748 ipv6_addr_copy(&ipv6h
->daddr
, &fl
.fl6_dst
);
751 err
= NF_HOOK(PF_INET6
, NF_IP6_LOCAL_OUT
, skb
, NULL
,
752 skb
->dst
->dev
, dst_output
);
754 if (err
== NET_XMIT_SUCCESS
|| err
== NET_XMIT_CN
) {
755 stats
->tx_bytes
+= pkt_len
;
759 stats
->tx_aborted_errors
++;
761 ip6_tnl_dst_store(t
, dst
);
768 stats
->tx_carrier_errors
++;
769 dst_link_failure(skb
);
781 static void ip6_tnl_set_cap(struct ip6_tnl
*t
)
783 struct ip6_tnl_parm
*p
= &t
->parms
;
784 struct in6_addr
*laddr
= &p
->laddr
;
785 struct in6_addr
*raddr
= &p
->raddr
;
786 int ltype
= ipv6_addr_type(laddr
);
787 int rtype
= ipv6_addr_type(raddr
);
789 p
->flags
&= ~(IP6_TNL_F_CAP_XMIT
|IP6_TNL_F_CAP_RCV
);
791 if (ltype
!= IPV6_ADDR_ANY
&& rtype
!= IPV6_ADDR_ANY
&&
794 IPV6_ADDR_LOOPBACK
|IPV6_ADDR_LINKLOCAL
|
795 IPV6_ADDR_MAPPED
|IPV6_ADDR_RESERVED
)) == IPV6_ADDR_UNICAST
) {
796 struct net_device
*ldev
= NULL
;
801 ldev
= dev_get_by_index(p
->link
);
803 if (ltype
&IPV6_ADDR_UNICAST
&& !ipv6_chk_addr(laddr
, ldev
, 0))
806 if (rtype
&IPV6_ADDR_UNICAST
&& ipv6_chk_addr(raddr
, NULL
, 0))
810 if (ltype
&IPV6_ADDR_UNICAST
)
811 p
->flags
|= IP6_TNL_F_CAP_XMIT
;
812 if (rtype
&IPV6_ADDR_UNICAST
)
813 p
->flags
|= IP6_TNL_F_CAP_RCV
;
820 static void ip6ip6_tnl_link_config(struct ip6_tnl
*t
)
822 struct net_device
*dev
= t
->dev
;
823 struct ip6_tnl_parm
*p
= &t
->parms
;
824 struct flowi
*fl
= &t
->fl
;
826 memcpy(&dev
->dev_addr
, &p
->laddr
, sizeof(struct in6_addr
));
827 memcpy(&dev
->broadcast
, &p
->raddr
, sizeof(struct in6_addr
));
829 /* Set up flowi template */
830 ipv6_addr_copy(&fl
->fl6_src
, &p
->laddr
);
831 ipv6_addr_copy(&fl
->fl6_dst
, &p
->raddr
);
833 fl
->fl6_flowlabel
= 0;
835 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_TCLASS
))
836 fl
->fl6_flowlabel
|= IPV6_TCLASS_MASK
& p
->flowinfo
;
837 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_FLOWLABEL
))
838 fl
->fl6_flowlabel
|= IPV6_FLOWLABEL_MASK
& p
->flowinfo
;
842 if (p
->flags
&IP6_TNL_F_CAP_XMIT
&& p
->flags
&IP6_TNL_F_CAP_RCV
)
843 dev
->flags
|= IFF_POINTOPOINT
;
845 dev
->flags
&= ~IFF_POINTOPOINT
;
847 dev
->iflink
= p
->link
;
849 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
850 struct rt6_info
*rt
= rt6_lookup(&p
->raddr
, &p
->laddr
,
857 dev
->hard_header_len
= rt
->rt6i_dev
->hard_header_len
+
858 sizeof (struct ipv6hdr
);
860 dev
->mtu
= rt
->rt6i_dev
->mtu
- sizeof (struct ipv6hdr
);
862 if (dev
->mtu
< IPV6_MIN_MTU
)
863 dev
->mtu
= IPV6_MIN_MTU
;
865 dst_release(&rt
->u
.dst
);
870 * ip6ip6_tnl_change - update the tunnel parameters
871 * @t: tunnel to be changed
872 * @p: tunnel configuration parameters
873 * @active: != 0 if tunnel is ready for use
876 * ip6ip6_tnl_change() updates the tunnel parameters
880 ip6ip6_tnl_change(struct ip6_tnl
*t
, struct ip6_tnl_parm
*p
)
882 ipv6_addr_copy(&t
->parms
.laddr
, &p
->laddr
);
883 ipv6_addr_copy(&t
->parms
.raddr
, &p
->raddr
);
884 t
->parms
.flags
= p
->flags
;
885 t
->parms
.hop_limit
= p
->hop_limit
;
886 t
->parms
.encap_limit
= p
->encap_limit
;
887 t
->parms
.flowinfo
= p
->flowinfo
;
888 t
->parms
.link
= p
->link
;
889 ip6_tnl_dst_reset(t
);
890 ip6ip6_tnl_link_config(t
);
895 * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace
896 * @dev: virtual device associated with tunnel
897 * @ifr: parameters passed from userspace
898 * @cmd: command to be performed
901 * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels
904 * The possible commands are the following:
905 * %SIOCGETTUNNEL: get tunnel parameters for device
906 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
907 * %SIOCCHGTUNNEL: change tunnel parameters to those given
908 * %SIOCDELTUNNEL: delete tunnel
910 * The fallback device "ip6tnl0", created during module
911 * initialization, can be used for creating other tunnel devices.
915 * %-EFAULT if unable to copy data to or from userspace,
916 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
917 * %-EINVAL if passed tunnel parameters are invalid,
918 * %-EEXIST if changing a tunnel's parameters would cause a conflict
919 * %-ENODEV if attempting to change or delete a nonexisting device
923 ip6ip6_tnl_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
927 struct ip6_tnl_parm p
;
928 struct ip6_tnl
*t
= NULL
;
932 if (dev
== ip6ip6_fb_tnl_dev
) {
933 if (copy_from_user(&p
,
934 ifr
->ifr_ifru
.ifru_data
,
939 if ((err
= ip6ip6_tnl_locate(&p
, &t
, 0)) == -ENODEV
)
940 t
= netdev_priv(dev
);
944 t
= netdev_priv(dev
);
946 memcpy(&p
, &t
->parms
, sizeof (p
));
947 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof (p
))) {
954 create
= (cmd
== SIOCADDTUNNEL
);
955 if (!capable(CAP_NET_ADMIN
))
957 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof (p
))) {
961 if (!create
&& dev
!= ip6ip6_fb_tnl_dev
) {
962 t
= netdev_priv(dev
);
964 if (!t
&& (err
= ip6ip6_tnl_locate(&p
, &t
, create
))) {
967 if (cmd
== SIOCCHGTUNNEL
) {
972 ip6ip6_tnl_unlink(t
);
973 err
= ip6ip6_tnl_change(t
, &p
);
975 netdev_state_change(dev
);
977 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
,
978 &t
->parms
, sizeof (p
))) {
986 if (!capable(CAP_NET_ADMIN
))
989 if (dev
== ip6ip6_fb_tnl_dev
) {
990 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
,
995 err
= ip6ip6_tnl_locate(&p
, &t
, 0);
998 if (t
== netdev_priv(ip6ip6_fb_tnl_dev
)) {
1003 t
= netdev_priv(dev
);
1005 err
= unregister_netdevice(t
->dev
);
1014 * ip6ip6_tnl_get_stats - return the stats for tunnel device
1015 * @dev: virtual device associated with tunnel
1017 * Return: stats for device
1020 static struct net_device_stats
*
1021 ip6ip6_tnl_get_stats(struct net_device
*dev
)
1023 return &(((struct ip6_tnl
*)netdev_priv(dev
))->stat
);
1027 * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device
1028 * @dev: virtual device associated with tunnel
1029 * @new_mtu: the new mtu
1033 * %-EINVAL if mtu too small
1037 ip6ip6_tnl_change_mtu(struct net_device
*dev
, int new_mtu
)
1039 if (new_mtu
< IPV6_MIN_MTU
) {
1047 * ip6ip6_tnl_dev_setup - setup virtual tunnel device
1048 * @dev: virtual device associated with tunnel
1051 * Initialize function pointers and device parameters
1054 static void ip6ip6_tnl_dev_setup(struct net_device
*dev
)
1056 SET_MODULE_OWNER(dev
);
1057 dev
->uninit
= ip6ip6_tnl_dev_uninit
;
1058 dev
->destructor
= free_netdev
;
1059 dev
->hard_start_xmit
= ip6ip6_tnl_xmit
;
1060 dev
->get_stats
= ip6ip6_tnl_get_stats
;
1061 dev
->do_ioctl
= ip6ip6_tnl_ioctl
;
1062 dev
->change_mtu
= ip6ip6_tnl_change_mtu
;
1064 dev
->type
= ARPHRD_TUNNEL6
;
1065 dev
->hard_header_len
= LL_MAX_HEADER
+ sizeof (struct ipv6hdr
);
1066 dev
->mtu
= ETH_DATA_LEN
- sizeof (struct ipv6hdr
);
1067 dev
->flags
|= IFF_NOARP
;
1068 dev
->addr_len
= sizeof(struct in6_addr
);
1073 * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1074 * @dev: virtual device associated with tunnel
1078 ip6ip6_tnl_dev_init_gen(struct net_device
*dev
)
1080 struct ip6_tnl
*t
= netdev_priv(dev
);
1081 t
->fl
.proto
= IPPROTO_IPV6
;
1083 strcpy(t
->parms
.name
, dev
->name
);
1087 * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1088 * @dev: virtual device associated with tunnel
1092 ip6ip6_tnl_dev_init(struct net_device
*dev
)
1094 struct ip6_tnl
*t
= netdev_priv(dev
);
1095 ip6ip6_tnl_dev_init_gen(dev
);
1096 ip6ip6_tnl_link_config(t
);
1101 * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1102 * @dev: fallback device
1108 ip6ip6_fb_tnl_dev_init(struct net_device
*dev
)
1110 struct ip6_tnl
*t
= netdev_priv(dev
);
1111 ip6ip6_tnl_dev_init_gen(dev
);
1117 static struct xfrm6_tunnel ip6ip6_handler
= {
1118 .handler
= ip6ip6_rcv
,
1119 .err_handler
= ip6ip6_err
,
1124 * ip6_tunnel_init - register protocol and reserve needed resources
1126 * Return: 0 on success
1129 static int __init
ip6_tunnel_init(void)
1133 if (xfrm6_tunnel_register(&ip6ip6_handler
)) {
1134 printk(KERN_ERR
"ip6ip6 init: can't register tunnel\n");
1137 ip6ip6_fb_tnl_dev
= alloc_netdev(sizeof(struct ip6_tnl
), "ip6tnl0",
1138 ip6ip6_tnl_dev_setup
);
1140 if (!ip6ip6_fb_tnl_dev
) {
1144 ip6ip6_fb_tnl_dev
->init
= ip6ip6_fb_tnl_dev_init
;
1146 if ((err
= register_netdev(ip6ip6_fb_tnl_dev
))) {
1147 free_netdev(ip6ip6_fb_tnl_dev
);
1152 xfrm6_tunnel_deregister(&ip6ip6_handler
);
1157 * ip6_tunnel_cleanup - free resources and unregister protocol
1160 static void __exit
ip6_tunnel_cleanup(void)
1162 if (xfrm6_tunnel_deregister(&ip6ip6_handler
))
1163 printk(KERN_INFO
"ip6ip6 close: can't deregister tunnel\n");
1165 unregister_netdev(ip6ip6_fb_tnl_dev
);
1168 module_init(ip6_tunnel_init
);
1169 module_exit(ip6_tunnel_cleanup
);