2 * IPv6 tunneling device
3 * Linux INET6 implementation
6 * Ville Nuorvala <vnuorval@tcs.hut.fi>
7 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
10 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
42 #include <linux/slab.h>
43 #include <linux/hash.h>
45 #include <asm/uaccess.h>
46 #include <linux/atomic.h>
50 #include <net/ip_tunnels.h>
52 #include <net/ip6_route.h>
53 #include <net/addrconf.h>
54 #include <net/ip6_tunnel.h>
56 #include <net/dsfield.h>
57 #include <net/inet_ecn.h>
58 #include <net/net_namespace.h>
59 #include <net/netns/generic.h>
61 MODULE_AUTHOR("Ville Nuorvala");
62 MODULE_DESCRIPTION("IPv6 tunneling device");
63 MODULE_LICENSE("GPL");
64 MODULE_ALIAS_RTNL_LINK("ip6tnl");
65 MODULE_ALIAS_NETDEV("ip6tnl0");
68 #define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__)
70 #define IP6_TNL_TRACE(x...) do {;} while(0)
73 #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
74 #define IPV6_TCLASS_SHIFT 20
76 #define HASH_SIZE_SHIFT 5
77 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
79 static bool log_ecn_error
= true;
80 module_param(log_ecn_error
, bool, 0644);
81 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
83 static u32
HASH(const struct in6_addr
*addr1
, const struct in6_addr
*addr2
)
85 u32 hash
= ipv6_addr_hash(addr1
) ^ ipv6_addr_hash(addr2
);
87 return hash_32(hash
, HASH_SIZE_SHIFT
);
90 static int ip6_tnl_dev_init(struct net_device
*dev
);
91 static void ip6_tnl_dev_setup(struct net_device
*dev
);
92 static struct rtnl_link_ops ip6_link_ops __read_mostly
;
94 static int ip6_tnl_net_id __read_mostly
;
96 /* the IPv6 tunnel fallback device */
97 struct net_device
*fb_tnl_dev
;
98 /* lists for storing tunnels in use */
99 struct ip6_tnl __rcu
*tnls_r_l
[HASH_SIZE
];
100 struct ip6_tnl __rcu
*tnls_wc
[1];
101 struct ip6_tnl __rcu
**tnls
[2];
104 static struct net_device_stats
*ip6_get_stats(struct net_device
*dev
)
106 struct pcpu_tstats tmp
, sum
= { 0 };
109 for_each_possible_cpu(i
) {
111 const struct pcpu_tstats
*tstats
= per_cpu_ptr(dev
->tstats
, i
);
114 start
= u64_stats_fetch_begin_bh(&tstats
->syncp
);
115 tmp
.rx_packets
= tstats
->rx_packets
;
116 tmp
.rx_bytes
= tstats
->rx_bytes
;
117 tmp
.tx_packets
= tstats
->tx_packets
;
118 tmp
.tx_bytes
= tstats
->tx_bytes
;
119 } while (u64_stats_fetch_retry_bh(&tstats
->syncp
, start
));
121 sum
.rx_packets
+= tmp
.rx_packets
;
122 sum
.rx_bytes
+= tmp
.rx_bytes
;
123 sum
.tx_packets
+= tmp
.tx_packets
;
124 sum
.tx_bytes
+= tmp
.tx_bytes
;
126 dev
->stats
.rx_packets
= sum
.rx_packets
;
127 dev
->stats
.rx_bytes
= sum
.rx_bytes
;
128 dev
->stats
.tx_packets
= sum
.tx_packets
;
129 dev
->stats
.tx_bytes
= sum
.tx_bytes
;
134 * Locking : hash tables are protected by RCU and RTNL
137 struct dst_entry
*ip6_tnl_dst_check(struct ip6_tnl
*t
)
139 struct dst_entry
*dst
= t
->dst_cache
;
141 if (dst
&& dst
->obsolete
&&
142 dst
->ops
->check(dst
, t
->dst_cookie
) == NULL
) {
150 EXPORT_SYMBOL_GPL(ip6_tnl_dst_check
);
152 void ip6_tnl_dst_reset(struct ip6_tnl
*t
)
154 dst_release(t
->dst_cache
);
157 EXPORT_SYMBOL_GPL(ip6_tnl_dst_reset
);
159 void ip6_tnl_dst_store(struct ip6_tnl
*t
, struct dst_entry
*dst
)
161 struct rt6_info
*rt
= (struct rt6_info
*) dst
;
162 t
->dst_cookie
= rt
->rt6i_node
? rt
->rt6i_node
->fn_sernum
: 0;
163 dst_release(t
->dst_cache
);
166 EXPORT_SYMBOL_GPL(ip6_tnl_dst_store
);
169 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
170 * @remote: the address of the tunnel exit-point
171 * @local: the address of the tunnel entry-point
174 * tunnel matching given end-points if found,
175 * else fallback tunnel if its device is up,
179 #define for_each_ip6_tunnel_rcu(start) \
180 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
182 static struct ip6_tnl
*
183 ip6_tnl_lookup(struct net
*net
, const struct in6_addr
*remote
, const struct in6_addr
*local
)
185 unsigned int hash
= HASH(remote
, local
);
187 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
189 for_each_ip6_tunnel_rcu(ip6n
->tnls_r_l
[hash
]) {
190 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
191 ipv6_addr_equal(remote
, &t
->parms
.raddr
) &&
192 (t
->dev
->flags
& IFF_UP
))
195 t
= rcu_dereference(ip6n
->tnls_wc
[0]);
196 if (t
&& (t
->dev
->flags
& IFF_UP
))
203 * ip6_tnl_bucket - get head of list matching given tunnel parameters
204 * @p: parameters containing tunnel end-points
207 * ip6_tnl_bucket() returns the head of the list matching the
208 * &struct in6_addr entries laddr and raddr in @p.
210 * Return: head of IPv6 tunnel list
213 static struct ip6_tnl __rcu
**
214 ip6_tnl_bucket(struct ip6_tnl_net
*ip6n
, const struct __ip6_tnl_parm
*p
)
216 const struct in6_addr
*remote
= &p
->raddr
;
217 const struct in6_addr
*local
= &p
->laddr
;
221 if (!ipv6_addr_any(remote
) || !ipv6_addr_any(local
)) {
223 h
= HASH(remote
, local
);
225 return &ip6n
->tnls
[prio
][h
];
229 * ip6_tnl_link - add tunnel to hash table
230 * @t: tunnel to be added
234 ip6_tnl_link(struct ip6_tnl_net
*ip6n
, struct ip6_tnl
*t
)
236 struct ip6_tnl __rcu
**tp
= ip6_tnl_bucket(ip6n
, &t
->parms
);
238 rcu_assign_pointer(t
->next
, rtnl_dereference(*tp
));
239 rcu_assign_pointer(*tp
, t
);
243 * ip6_tnl_unlink - remove tunnel from hash table
244 * @t: tunnel to be removed
248 ip6_tnl_unlink(struct ip6_tnl_net
*ip6n
, struct ip6_tnl
*t
)
250 struct ip6_tnl __rcu
**tp
;
251 struct ip6_tnl
*iter
;
253 for (tp
= ip6_tnl_bucket(ip6n
, &t
->parms
);
254 (iter
= rtnl_dereference(*tp
)) != NULL
;
257 rcu_assign_pointer(*tp
, t
->next
);
263 static void ip6_dev_free(struct net_device
*dev
)
265 free_percpu(dev
->tstats
);
269 static int ip6_tnl_create2(struct net_device
*dev
)
271 struct ip6_tnl
*t
= netdev_priv(dev
);
272 struct net
*net
= dev_net(dev
);
273 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
276 t
= netdev_priv(dev
);
278 dev
->rtnl_link_ops
= &ip6_link_ops
;
279 err
= register_netdevice(dev
);
283 strcpy(t
->parms
.name
, dev
->name
);
286 ip6_tnl_link(ip6n
, t
);
294 * ip6_tnl_create - create a new tunnel
295 * @p: tunnel parameters
296 * @pt: pointer to new tunnel
299 * Create tunnel matching given parameters.
302 * created tunnel or NULL
305 static struct ip6_tnl
*ip6_tnl_create(struct net
*net
, struct __ip6_tnl_parm
*p
)
307 struct net_device
*dev
;
313 strlcpy(name
, p
->name
, IFNAMSIZ
);
315 sprintf(name
, "ip6tnl%%d");
317 dev
= alloc_netdev(sizeof (*t
), name
, ip6_tnl_dev_setup
);
321 dev_net_set(dev
, net
);
323 t
= netdev_priv(dev
);
325 err
= ip6_tnl_create2(dev
);
338 * ip6_tnl_locate - find or create tunnel matching given parameters
339 * @p: tunnel parameters
340 * @create: != 0 if allowed to create new tunnel if no match found
343 * ip6_tnl_locate() first tries to locate an existing tunnel
344 * based on @parms. If this is unsuccessful, but @create is set a new
345 * tunnel device is created and registered for use.
348 * matching tunnel or NULL
351 static struct ip6_tnl
*ip6_tnl_locate(struct net
*net
,
352 struct __ip6_tnl_parm
*p
, int create
)
354 const struct in6_addr
*remote
= &p
->raddr
;
355 const struct in6_addr
*local
= &p
->laddr
;
356 struct ip6_tnl __rcu
**tp
;
358 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
360 for (tp
= ip6_tnl_bucket(ip6n
, p
);
361 (t
= rtnl_dereference(*tp
)) != NULL
;
363 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
364 ipv6_addr_equal(remote
, &t
->parms
.raddr
))
369 return ip6_tnl_create(net
, p
);
373 * ip6_tnl_dev_uninit - tunnel device uninitializer
374 * @dev: the device to be destroyed
377 * ip6_tnl_dev_uninit() removes tunnel from its list
381 ip6_tnl_dev_uninit(struct net_device
*dev
)
383 struct ip6_tnl
*t
= netdev_priv(dev
);
384 struct net
*net
= dev_net(dev
);
385 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
387 if (dev
== ip6n
->fb_tnl_dev
)
388 RCU_INIT_POINTER(ip6n
->tnls_wc
[0], NULL
);
390 ip6_tnl_unlink(ip6n
, t
);
391 ip6_tnl_dst_reset(t
);
396 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
397 * @skb: received socket buffer
400 * 0 if none was found,
401 * else index to encapsulation limit
404 __u16
ip6_tnl_parse_tlv_enc_lim(struct sk_buff
*skb
, __u8
*raw
)
406 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*)raw
;
407 unsigned int nhoff
= raw
- skb
->data
;
408 unsigned int off
= nhoff
+ sizeof(*ipv6h
);
409 u8 next
, nexthdr
= ipv6h
->nexthdr
;
411 while (ipv6_ext_hdr(nexthdr
) && nexthdr
!= NEXTHDR_NONE
) {
412 struct ipv6_opt_hdr
*hdr
;
415 if (!pskb_may_pull(skb
, off
+ sizeof(*hdr
)))
418 hdr
= (struct ipv6_opt_hdr
*)(skb
->data
+ off
);
419 if (nexthdr
== NEXTHDR_FRAGMENT
) {
420 struct frag_hdr
*frag_hdr
= (struct frag_hdr
*) hdr
;
421 if (frag_hdr
->frag_off
)
424 } else if (nexthdr
== NEXTHDR_AUTH
) {
425 optlen
= (hdr
->hdrlen
+ 2) << 2;
427 optlen
= ipv6_optlen(hdr
);
429 /* cache hdr->nexthdr, since pskb_may_pull() might
433 if (nexthdr
== NEXTHDR_DEST
) {
436 /* Remember : hdr is no longer valid at this point. */
437 if (!pskb_may_pull(skb
, off
+ optlen
))
441 struct ipv6_tlv_tnl_enc_lim
*tel
;
443 /* No more room for encapsulation limit */
444 if (i
+ sizeof(*tel
) > optlen
)
447 tel
= (struct ipv6_tlv_tnl_enc_lim
*)(skb
->data
+ off
+ i
);
448 /* return index of option if found and valid */
449 if (tel
->type
== IPV6_TLV_TNL_ENCAP_LIMIT
&&
451 return i
+ off
- nhoff
;
452 /* else jump to next option */
454 i
+= tel
->length
+ 2;
464 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim
);
467 * ip6_tnl_err - tunnel error handler
470 * ip6_tnl_err() should handle errors in the tunnel according
471 * to the specifications in RFC 2473.
475 ip6_tnl_err(struct sk_buff
*skb
, __u8 ipproto
, struct inet6_skb_parm
*opt
,
476 u8
*type
, u8
*code
, int *msg
, __u32
*info
, int offset
)
478 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*) skb
->data
;
481 u8 rel_type
= ICMPV6_DEST_UNREACH
;
482 u8 rel_code
= ICMPV6_ADDR_UNREACH
;
487 /* If the packet doesn't contain the original IPv6 header we are
488 in trouble since we might need the source address for further
489 processing of the error. */
492 if ((t
= ip6_tnl_lookup(dev_net(skb
->dev
), &ipv6h
->daddr
,
493 &ipv6h
->saddr
)) == NULL
)
496 if (t
->parms
.proto
!= ipproto
&& t
->parms
.proto
!= 0)
503 struct ipv6_tlv_tnl_enc_lim
*tel
;
505 case ICMPV6_DEST_UNREACH
:
506 net_warn_ratelimited("%s: Path to destination invalid or inactive!\n",
510 case ICMPV6_TIME_EXCEED
:
511 if ((*code
) == ICMPV6_EXC_HOPLIMIT
) {
512 net_warn_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
517 case ICMPV6_PARAMPROB
:
519 if ((*code
) == ICMPV6_HDR_FIELD
)
520 teli
= ip6_tnl_parse_tlv_enc_lim(skb
, skb
->data
);
522 if (teli
&& teli
== *info
- 2) {
523 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &skb
->data
[teli
];
524 if (tel
->encap_limit
== 0) {
525 net_warn_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
530 net_warn_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
534 case ICMPV6_PKT_TOOBIG
:
535 mtu
= *info
- offset
;
536 if (mtu
< IPV6_MIN_MTU
)
540 if ((len
= sizeof (*ipv6h
) + ntohs(ipv6h
->payload_len
)) > mtu
) {
541 rel_type
= ICMPV6_PKT_TOOBIG
;
560 ip4ip6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
561 u8 type
, u8 code
, int offset
, __be32 info
)
566 __u32 rel_info
= ntohl(info
);
568 struct sk_buff
*skb2
;
569 const struct iphdr
*eiph
;
573 err
= ip6_tnl_err(skb
, IPPROTO_IPIP
, opt
, &rel_type
, &rel_code
,
574 &rel_msg
, &rel_info
, offset
);
582 case ICMPV6_DEST_UNREACH
:
583 if (rel_code
!= ICMPV6_ADDR_UNREACH
)
585 rel_type
= ICMP_DEST_UNREACH
;
586 rel_code
= ICMP_HOST_UNREACH
;
588 case ICMPV6_PKT_TOOBIG
:
591 rel_type
= ICMP_DEST_UNREACH
;
592 rel_code
= ICMP_FRAG_NEEDED
;
595 rel_type
= ICMP_REDIRECT
;
596 rel_code
= ICMP_REDIR_HOST
;
601 if (!pskb_may_pull(skb
, offset
+ sizeof(struct iphdr
)))
604 skb2
= skb_clone(skb
, GFP_ATOMIC
);
610 skb_pull(skb2
, offset
);
611 skb_reset_network_header(skb2
);
614 /* Try to guess incoming interface */
615 rt
= ip_route_output_ports(dev_net(skb
->dev
), &fl4
, NULL
,
618 IPPROTO_IPIP
, RT_TOS(eiph
->tos
), 0);
622 skb2
->dev
= rt
->dst
.dev
;
624 /* route "incoming" packet */
625 if (rt
->rt_flags
& RTCF_LOCAL
) {
628 rt
= ip_route_output_ports(dev_net(skb
->dev
), &fl4
, NULL
,
629 eiph
->daddr
, eiph
->saddr
,
632 RT_TOS(eiph
->tos
), 0);
634 rt
->dst
.dev
->type
!= ARPHRD_TUNNEL
) {
639 skb_dst_set(skb2
, &rt
->dst
);
642 if (ip_route_input(skb2
, eiph
->daddr
, eiph
->saddr
, eiph
->tos
,
644 skb_dst(skb2
)->dev
->type
!= ARPHRD_TUNNEL
)
648 /* change mtu on this route */
649 if (rel_type
== ICMP_DEST_UNREACH
&& rel_code
== ICMP_FRAG_NEEDED
) {
650 if (rel_info
> dst_mtu(skb_dst(skb2
)))
653 skb_dst(skb2
)->ops
->update_pmtu(skb_dst(skb2
), NULL
, skb2
, rel_info
);
655 if (rel_type
== ICMP_REDIRECT
)
656 skb_dst(skb2
)->ops
->redirect(skb_dst(skb2
), NULL
, skb2
);
658 icmp_send(skb2
, rel_type
, rel_code
, htonl(rel_info
));
666 ip6ip6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
667 u8 type
, u8 code
, int offset
, __be32 info
)
672 __u32 rel_info
= ntohl(info
);
675 err
= ip6_tnl_err(skb
, IPPROTO_IPV6
, opt
, &rel_type
, &rel_code
,
676 &rel_msg
, &rel_info
, offset
);
680 if (rel_msg
&& pskb_may_pull(skb
, offset
+ sizeof(struct ipv6hdr
))) {
682 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
688 skb_pull(skb2
, offset
);
689 skb_reset_network_header(skb2
);
691 /* Try to guess incoming interface */
692 rt
= rt6_lookup(dev_net(skb
->dev
), &ipv6_hdr(skb2
)->saddr
,
695 if (rt
&& rt
->dst
.dev
)
696 skb2
->dev
= rt
->dst
.dev
;
698 icmpv6_send(skb2
, rel_type
, rel_code
, rel_info
);
708 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl
*t
,
709 const struct ipv6hdr
*ipv6h
,
712 __u8 dsfield
= ipv6_get_dsfield(ipv6h
) & ~INET_ECN_MASK
;
714 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
715 ipv4_change_dsfield(ip_hdr(skb
), INET_ECN_MASK
, dsfield
);
717 return IP6_ECN_decapsulate(ipv6h
, skb
);
720 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl
*t
,
721 const struct ipv6hdr
*ipv6h
,
724 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
725 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h
), ipv6_hdr(skb
));
727 return IP6_ECN_decapsulate(ipv6h
, skb
);
730 __u32
ip6_tnl_get_cap(struct ip6_tnl
*t
,
731 const struct in6_addr
*laddr
,
732 const struct in6_addr
*raddr
)
734 struct __ip6_tnl_parm
*p
= &t
->parms
;
735 int ltype
= ipv6_addr_type(laddr
);
736 int rtype
= ipv6_addr_type(raddr
);
739 if (ltype
== IPV6_ADDR_ANY
|| rtype
== IPV6_ADDR_ANY
) {
740 flags
= IP6_TNL_F_CAP_PER_PACKET
;
741 } else if (ltype
& (IPV6_ADDR_UNICAST
|IPV6_ADDR_MULTICAST
) &&
742 rtype
& (IPV6_ADDR_UNICAST
|IPV6_ADDR_MULTICAST
) &&
743 !((ltype
|rtype
) & IPV6_ADDR_LOOPBACK
) &&
744 (!((ltype
|rtype
) & IPV6_ADDR_LINKLOCAL
) || p
->link
)) {
745 if (ltype
&IPV6_ADDR_UNICAST
)
746 flags
|= IP6_TNL_F_CAP_XMIT
;
747 if (rtype
&IPV6_ADDR_UNICAST
)
748 flags
|= IP6_TNL_F_CAP_RCV
;
752 EXPORT_SYMBOL(ip6_tnl_get_cap
);
754 /* called with rcu_read_lock() */
755 int ip6_tnl_rcv_ctl(struct ip6_tnl
*t
,
756 const struct in6_addr
*laddr
,
757 const struct in6_addr
*raddr
)
759 struct __ip6_tnl_parm
*p
= &t
->parms
;
761 struct net
*net
= dev_net(t
->dev
);
763 if ((p
->flags
& IP6_TNL_F_CAP_RCV
) ||
764 ((p
->flags
& IP6_TNL_F_CAP_PER_PACKET
) &&
765 (ip6_tnl_get_cap(t
, laddr
, raddr
) & IP6_TNL_F_CAP_RCV
))) {
766 struct net_device
*ldev
= NULL
;
769 ldev
= dev_get_by_index_rcu(net
, p
->link
);
771 if ((ipv6_addr_is_multicast(laddr
) ||
772 likely(ipv6_chk_addr(net
, laddr
, ldev
, 0))) &&
773 likely(!ipv6_chk_addr(net
, raddr
, NULL
, 0)))
778 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl
);
781 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
782 * @skb: received socket buffer
783 * @protocol: ethernet protocol ID
784 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
789 static int ip6_tnl_rcv(struct sk_buff
*skb
, __u16 protocol
,
791 int (*dscp_ecn_decapsulate
)(const struct ip6_tnl
*t
,
792 const struct ipv6hdr
*ipv6h
,
793 struct sk_buff
*skb
))
796 const struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
801 if ((t
= ip6_tnl_lookup(dev_net(skb
->dev
), &ipv6h
->saddr
,
802 &ipv6h
->daddr
)) != NULL
) {
803 struct pcpu_tstats
*tstats
;
805 if (t
->parms
.proto
!= ipproto
&& t
->parms
.proto
!= 0) {
810 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
)) {
815 if (!ip6_tnl_rcv_ctl(t
, &ipv6h
->daddr
, &ipv6h
->saddr
)) {
816 t
->dev
->stats
.rx_dropped
++;
821 skb
->mac_header
= skb
->network_header
;
822 skb_reset_network_header(skb
);
823 skb
->protocol
= htons(protocol
);
824 skb
->pkt_type
= PACKET_HOST
;
825 memset(skb
->cb
, 0, sizeof(struct inet6_skb_parm
));
827 __skb_tunnel_rx(skb
, t
->dev
);
829 err
= dscp_ecn_decapsulate(t
, ipv6h
, skb
);
832 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
834 ipv6_get_dsfield(ipv6h
));
836 ++t
->dev
->stats
.rx_frame_errors
;
837 ++t
->dev
->stats
.rx_errors
;
843 tstats
= this_cpu_ptr(t
->dev
->tstats
);
844 u64_stats_update_begin(&tstats
->syncp
);
845 tstats
->rx_packets
++;
846 tstats
->rx_bytes
+= skb
->len
;
847 u64_stats_update_end(&tstats
->syncp
);
862 static int ip4ip6_rcv(struct sk_buff
*skb
)
864 return ip6_tnl_rcv(skb
, ETH_P_IP
, IPPROTO_IPIP
,
865 ip4ip6_dscp_ecn_decapsulate
);
868 static int ip6ip6_rcv(struct sk_buff
*skb
)
870 return ip6_tnl_rcv(skb
, ETH_P_IPV6
, IPPROTO_IPV6
,
871 ip6ip6_dscp_ecn_decapsulate
);
874 struct ipv6_tel_txoption
{
875 struct ipv6_txoptions ops
;
879 static void init_tel_txopt(struct ipv6_tel_txoption
*opt
, __u8 encap_limit
)
881 memset(opt
, 0, sizeof(struct ipv6_tel_txoption
));
883 opt
->dst_opt
[2] = IPV6_TLV_TNL_ENCAP_LIMIT
;
885 opt
->dst_opt
[4] = encap_limit
;
886 opt
->dst_opt
[5] = IPV6_TLV_PADN
;
889 opt
->ops
.dst0opt
= (struct ipv6_opt_hdr
*) opt
->dst_opt
;
890 opt
->ops
.opt_nflen
= 8;
894 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
895 * @t: the outgoing tunnel device
896 * @hdr: IPv6 header from the incoming packet
899 * Avoid trivial tunneling loop by checking that tunnel exit-point
900 * doesn't match source of incoming packet.
908 ip6_tnl_addr_conflict(const struct ip6_tnl
*t
, const struct ipv6hdr
*hdr
)
910 return ipv6_addr_equal(&t
->parms
.raddr
, &hdr
->saddr
);
913 int ip6_tnl_xmit_ctl(struct ip6_tnl
*t
)
915 struct __ip6_tnl_parm
*p
= &t
->parms
;
917 struct net
*net
= dev_net(t
->dev
);
919 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
920 struct net_device
*ldev
= NULL
;
924 ldev
= dev_get_by_index_rcu(net
, p
->link
);
926 if (unlikely(!ipv6_chk_addr(net
, &p
->laddr
, ldev
, 0)))
927 pr_warn("%s xmit: Local address not yet configured!\n",
929 else if (!ipv6_addr_is_multicast(&p
->raddr
) &&
930 unlikely(ipv6_chk_addr(net
, &p
->raddr
, NULL
, 0)))
931 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
939 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl
);
942 * ip6_tnl_xmit2 - encapsulate packet and send
943 * @skb: the outgoing socket buffer
944 * @dev: the outgoing tunnel device
945 * @dsfield: dscp code for outer header
946 * @fl: flow of tunneled packet
947 * @encap_limit: encapsulation limit
948 * @pmtu: Path MTU is stored if packet is too big
951 * Build new header and do some sanity checks on the packet before sending
957 * %-EMSGSIZE message too big. return mtu in this case.
960 static int ip6_tnl_xmit2(struct sk_buff
*skb
,
961 struct net_device
*dev
,
967 struct net
*net
= dev_net(dev
);
968 struct ip6_tnl
*t
= netdev_priv(dev
);
969 struct net_device_stats
*stats
= &t
->dev
->stats
;
970 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
971 struct ipv6_tel_txoption opt
;
972 struct dst_entry
*dst
= NULL
, *ndst
= NULL
;
973 struct net_device
*tdev
;
974 bool use_cache
= false;
976 unsigned int max_headroom
= sizeof(struct ipv6hdr
);
980 if (!(t
->parms
.flags
&
981 (IP6_TNL_F_USE_ORIG_TCLASS
| IP6_TNL_F_USE_ORIG_FWMARK
))) {
982 /* enable the cache only only if the routing decision does
983 * not depend on the current inner header value
989 dst
= ip6_tnl_dst_check(t
);
991 ndst
= ip6_route_output(net
, NULL
, fl6
);
994 goto tx_err_link_failure
;
995 ndst
= xfrm_lookup(net
, ndst
, flowi6_to_flowi(fl6
), NULL
, 0);
999 goto tx_err_link_failure
;
1007 stats
->collisions
++;
1008 net_warn_ratelimited("%s: Local routing loop detected!\n",
1010 goto tx_err_dst_release
;
1012 mtu
= dst_mtu(dst
) - sizeof (*ipv6h
);
1013 if (encap_limit
>= 0) {
1017 if (mtu
< IPV6_MIN_MTU
)
1020 skb_dst(skb
)->ops
->update_pmtu(skb_dst(skb
), NULL
, skb
, mtu
);
1021 if (skb
->len
> mtu
) {
1024 goto tx_err_dst_release
;
1028 * Okay, now see if we can stuff it in the buffer as-is.
1030 max_headroom
+= LL_RESERVED_SPACE(tdev
);
1032 if (skb_headroom(skb
) < max_headroom
|| skb_shared(skb
) ||
1033 (skb_cloned(skb
) && !skb_clone_writable(skb
, 0))) {
1034 struct sk_buff
*new_skb
;
1036 if (!(new_skb
= skb_realloc_headroom(skb
, max_headroom
)))
1037 goto tx_err_dst_release
;
1040 skb_set_owner_w(new_skb
, skb
->sk
);
1046 skb_dst_set(skb
, dst
);
1049 skb_dst_set_noref(skb
, dst
);
1051 skb
->transport_header
= skb
->network_header
;
1053 proto
= fl6
->flowi6_proto
;
1054 if (encap_limit
>= 0) {
1055 init_tel_txopt(&opt
, encap_limit
);
1056 ipv6_push_nfrag_opts(skb
, &opt
.ops
, &proto
, NULL
);
1058 skb_push(skb
, sizeof(struct ipv6hdr
));
1059 skb_reset_network_header(skb
);
1060 ipv6h
= ipv6_hdr(skb
);
1061 ip6_flow_hdr(ipv6h
, INET_ECN_encapsulate(0, dsfield
), fl6
->flowlabel
);
1062 ipv6h
->hop_limit
= t
->parms
.hop_limit
;
1063 ipv6h
->nexthdr
= proto
;
1064 ipv6h
->saddr
= fl6
->saddr
;
1065 ipv6h
->daddr
= fl6
->daddr
;
1066 ip6tunnel_xmit(skb
, dev
);
1068 ip6_tnl_dst_store(t
, ndst
);
1070 tx_err_link_failure
:
1071 stats
->tx_carrier_errors
++;
1072 dst_link_failure(skb
);
1079 ip4ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1081 struct ip6_tnl
*t
= netdev_priv(dev
);
1082 const struct iphdr
*iph
= ip_hdr(skb
);
1083 int encap_limit
= -1;
1089 if ((t
->parms
.proto
!= IPPROTO_IPIP
&& t
->parms
.proto
!= 0) ||
1090 !ip6_tnl_xmit_ctl(t
))
1093 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1094 encap_limit
= t
->parms
.encap_limit
;
1096 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof (fl6
));
1097 fl6
.flowi6_proto
= IPPROTO_IPIP
;
1099 dsfield
= ipv4_get_dsfield(iph
);
1101 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
1102 fl6
.flowlabel
|= htonl((__u32
)iph
->tos
<< IPV6_TCLASS_SHIFT
)
1104 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
1105 fl6
.flowi6_mark
= skb
->mark
;
1107 err
= ip6_tnl_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
1109 /* XXX: send ICMP error even if DF is not set. */
1110 if (err
== -EMSGSIZE
)
1111 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
1120 ip6ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1122 struct ip6_tnl
*t
= netdev_priv(dev
);
1123 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
1124 int encap_limit
= -1;
1131 if ((t
->parms
.proto
!= IPPROTO_IPV6
&& t
->parms
.proto
!= 0) ||
1132 !ip6_tnl_xmit_ctl(t
) || ip6_tnl_addr_conflict(t
, ipv6h
))
1135 offset
= ip6_tnl_parse_tlv_enc_lim(skb
, skb_network_header(skb
));
1137 struct ipv6_tlv_tnl_enc_lim
*tel
;
1138 tel
= (struct ipv6_tlv_tnl_enc_lim
*)&skb_network_header(skb
)[offset
];
1139 if (tel
->encap_limit
== 0) {
1140 icmpv6_send(skb
, ICMPV6_PARAMPROB
,
1141 ICMPV6_HDR_FIELD
, offset
+ 2);
1144 encap_limit
= tel
->encap_limit
- 1;
1145 } else if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1146 encap_limit
= t
->parms
.encap_limit
;
1148 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof (fl6
));
1149 fl6
.flowi6_proto
= IPPROTO_IPV6
;
1151 dsfield
= ipv6_get_dsfield(ipv6h
);
1152 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
1153 fl6
.flowlabel
|= (*(__be32
*) ipv6h
& IPV6_TCLASS_MASK
);
1154 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
1155 fl6
.flowlabel
|= (*(__be32
*) ipv6h
& IPV6_FLOWLABEL_MASK
);
1156 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
1157 fl6
.flowi6_mark
= skb
->mark
;
1159 err
= ip6_tnl_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
1161 if (err
== -EMSGSIZE
)
1162 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
1170 ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1172 struct ip6_tnl
*t
= netdev_priv(dev
);
1173 struct net_device_stats
*stats
= &t
->dev
->stats
;
1176 switch (skb
->protocol
) {
1177 case htons(ETH_P_IP
):
1178 ret
= ip4ip6_tnl_xmit(skb
, dev
);
1180 case htons(ETH_P_IPV6
):
1181 ret
= ip6ip6_tnl_xmit(skb
, dev
);
1190 return NETDEV_TX_OK
;
1194 stats
->tx_dropped
++;
1196 return NETDEV_TX_OK
;
1199 static void ip6_tnl_link_config(struct ip6_tnl
*t
)
1201 struct net_device
*dev
= t
->dev
;
1202 struct __ip6_tnl_parm
*p
= &t
->parms
;
1203 struct flowi6
*fl6
= &t
->fl
.u
.ip6
;
1205 memcpy(dev
->dev_addr
, &p
->laddr
, sizeof(struct in6_addr
));
1206 memcpy(dev
->broadcast
, &p
->raddr
, sizeof(struct in6_addr
));
1208 /* Set up flowi template */
1209 fl6
->saddr
= p
->laddr
;
1210 fl6
->daddr
= p
->raddr
;
1211 fl6
->flowi6_oif
= p
->link
;
1214 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_TCLASS
))
1215 fl6
->flowlabel
|= IPV6_TCLASS_MASK
& p
->flowinfo
;
1216 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_FLOWLABEL
))
1217 fl6
->flowlabel
|= IPV6_FLOWLABEL_MASK
& p
->flowinfo
;
1219 p
->flags
&= ~(IP6_TNL_F_CAP_XMIT
|IP6_TNL_F_CAP_RCV
|IP6_TNL_F_CAP_PER_PACKET
);
1220 p
->flags
|= ip6_tnl_get_cap(t
, &p
->laddr
, &p
->raddr
);
1222 if (p
->flags
&IP6_TNL_F_CAP_XMIT
&& p
->flags
&IP6_TNL_F_CAP_RCV
)
1223 dev
->flags
|= IFF_POINTOPOINT
;
1225 dev
->flags
&= ~IFF_POINTOPOINT
;
1227 dev
->iflink
= p
->link
;
1229 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
1230 int strict
= (ipv6_addr_type(&p
->raddr
) &
1231 (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
));
1233 struct rt6_info
*rt
= rt6_lookup(dev_net(dev
),
1234 &p
->raddr
, &p
->laddr
,
1241 dev
->hard_header_len
= rt
->dst
.dev
->hard_header_len
+
1242 sizeof (struct ipv6hdr
);
1244 dev
->mtu
= rt
->dst
.dev
->mtu
- sizeof (struct ipv6hdr
);
1245 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1248 if (dev
->mtu
< IPV6_MIN_MTU
)
1249 dev
->mtu
= IPV6_MIN_MTU
;
1256 * ip6_tnl_change - update the tunnel parameters
1257 * @t: tunnel to be changed
1258 * @p: tunnel configuration parameters
1261 * ip6_tnl_change() updates the tunnel parameters
1265 ip6_tnl_change(struct ip6_tnl
*t
, const struct __ip6_tnl_parm
*p
)
1267 t
->parms
.laddr
= p
->laddr
;
1268 t
->parms
.raddr
= p
->raddr
;
1269 t
->parms
.flags
= p
->flags
;
1270 t
->parms
.hop_limit
= p
->hop_limit
;
1271 t
->parms
.encap_limit
= p
->encap_limit
;
1272 t
->parms
.flowinfo
= p
->flowinfo
;
1273 t
->parms
.link
= p
->link
;
1274 t
->parms
.proto
= p
->proto
;
1275 ip6_tnl_dst_reset(t
);
1276 ip6_tnl_link_config(t
);
1280 static int ip6_tnl_update(struct ip6_tnl
*t
, struct __ip6_tnl_parm
*p
)
1282 struct net
*net
= dev_net(t
->dev
);
1283 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1286 ip6_tnl_unlink(ip6n
, t
);
1288 err
= ip6_tnl_change(t
, p
);
1289 ip6_tnl_link(ip6n
, t
);
1290 netdev_state_change(t
->dev
);
1295 ip6_tnl_parm_from_user(struct __ip6_tnl_parm
*p
, const struct ip6_tnl_parm
*u
)
1297 p
->laddr
= u
->laddr
;
1298 p
->raddr
= u
->raddr
;
1299 p
->flags
= u
->flags
;
1300 p
->hop_limit
= u
->hop_limit
;
1301 p
->encap_limit
= u
->encap_limit
;
1302 p
->flowinfo
= u
->flowinfo
;
1304 p
->proto
= u
->proto
;
1305 memcpy(p
->name
, u
->name
, sizeof(u
->name
));
1309 ip6_tnl_parm_to_user(struct ip6_tnl_parm
*u
, const struct __ip6_tnl_parm
*p
)
1311 u
->laddr
= p
->laddr
;
1312 u
->raddr
= p
->raddr
;
1313 u
->flags
= p
->flags
;
1314 u
->hop_limit
= p
->hop_limit
;
1315 u
->encap_limit
= p
->encap_limit
;
1316 u
->flowinfo
= p
->flowinfo
;
1318 u
->proto
= p
->proto
;
1319 memcpy(u
->name
, p
->name
, sizeof(u
->name
));
1323 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1324 * @dev: virtual device associated with tunnel
1325 * @ifr: parameters passed from userspace
1326 * @cmd: command to be performed
1329 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1332 * The possible commands are the following:
1333 * %SIOCGETTUNNEL: get tunnel parameters for device
1334 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1335 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1336 * %SIOCDELTUNNEL: delete tunnel
1338 * The fallback device "ip6tnl0", created during module
1339 * initialization, can be used for creating other tunnel devices.
1343 * %-EFAULT if unable to copy data to or from userspace,
1344 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1345 * %-EINVAL if passed tunnel parameters are invalid,
1346 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1347 * %-ENODEV if attempting to change or delete a nonexisting device
1351 ip6_tnl_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1354 struct ip6_tnl_parm p
;
1355 struct __ip6_tnl_parm p1
;
1356 struct ip6_tnl
*t
= NULL
;
1357 struct net
*net
= dev_net(dev
);
1358 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1362 if (dev
== ip6n
->fb_tnl_dev
) {
1363 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof (p
))) {
1367 ip6_tnl_parm_from_user(&p1
, &p
);
1368 t
= ip6_tnl_locate(net
, &p1
, 0);
1370 memset(&p
, 0, sizeof(p
));
1373 t
= netdev_priv(dev
);
1374 ip6_tnl_parm_to_user(&p
, &t
->parms
);
1375 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof (p
))) {
1382 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1385 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof (p
)))
1388 if (p
.proto
!= IPPROTO_IPV6
&& p
.proto
!= IPPROTO_IPIP
&&
1391 ip6_tnl_parm_from_user(&p1
, &p
);
1392 t
= ip6_tnl_locate(net
, &p1
, cmd
== SIOCADDTUNNEL
);
1393 if (dev
!= ip6n
->fb_tnl_dev
&& cmd
== SIOCCHGTUNNEL
) {
1395 if (t
->dev
!= dev
) {
1400 t
= netdev_priv(dev
);
1402 err
= ip6_tnl_update(t
, &p1
);
1406 ip6_tnl_parm_to_user(&p
, &t
->parms
);
1407 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1411 err
= (cmd
== SIOCADDTUNNEL
? -ENOBUFS
: -ENOENT
);
1415 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1418 if (dev
== ip6n
->fb_tnl_dev
) {
1420 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof (p
)))
1423 ip6_tnl_parm_from_user(&p1
, &p
);
1424 t
= ip6_tnl_locate(net
, &p1
, 0);
1428 if (t
->dev
== ip6n
->fb_tnl_dev
)
1433 unregister_netdevice(dev
);
1442 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1443 * @dev: virtual device associated with tunnel
1444 * @new_mtu: the new mtu
1448 * %-EINVAL if mtu too small
1452 ip6_tnl_change_mtu(struct net_device
*dev
, int new_mtu
)
1454 if (new_mtu
< IPV6_MIN_MTU
) {
1462 static const struct net_device_ops ip6_tnl_netdev_ops
= {
1463 .ndo_init
= ip6_tnl_dev_init
,
1464 .ndo_uninit
= ip6_tnl_dev_uninit
,
1465 .ndo_start_xmit
= ip6_tnl_xmit
,
1466 .ndo_do_ioctl
= ip6_tnl_ioctl
,
1467 .ndo_change_mtu
= ip6_tnl_change_mtu
,
1468 .ndo_get_stats
= ip6_get_stats
,
1473 * ip6_tnl_dev_setup - setup virtual tunnel device
1474 * @dev: virtual device associated with tunnel
1477 * Initialize function pointers and device parameters
1480 static void ip6_tnl_dev_setup(struct net_device
*dev
)
1484 dev
->netdev_ops
= &ip6_tnl_netdev_ops
;
1485 dev
->destructor
= ip6_dev_free
;
1487 dev
->type
= ARPHRD_TUNNEL6
;
1488 dev
->hard_header_len
= LL_MAX_HEADER
+ sizeof (struct ipv6hdr
);
1489 dev
->mtu
= ETH_DATA_LEN
- sizeof (struct ipv6hdr
);
1490 t
= netdev_priv(dev
);
1491 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1493 dev
->flags
|= IFF_NOARP
;
1494 dev
->addr_len
= sizeof(struct in6_addr
);
1495 dev
->features
|= NETIF_F_NETNS_LOCAL
;
1496 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1501 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1502 * @dev: virtual device associated with tunnel
1506 ip6_tnl_dev_init_gen(struct net_device
*dev
)
1508 struct ip6_tnl
*t
= netdev_priv(dev
);
1511 dev
->tstats
= alloc_percpu(struct pcpu_tstats
);
1518 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1519 * @dev: virtual device associated with tunnel
1522 static int ip6_tnl_dev_init(struct net_device
*dev
)
1524 struct ip6_tnl
*t
= netdev_priv(dev
);
1525 int err
= ip6_tnl_dev_init_gen(dev
);
1529 ip6_tnl_link_config(t
);
1534 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1535 * @dev: fallback device
1540 static int __net_init
ip6_fb_tnl_dev_init(struct net_device
*dev
)
1542 struct ip6_tnl
*t
= netdev_priv(dev
);
1543 struct net
*net
= dev_net(dev
);
1544 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1546 t
->parms
.proto
= IPPROTO_IPV6
;
1549 rcu_assign_pointer(ip6n
->tnls_wc
[0], t
);
1553 static int ip6_tnl_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1557 if (!data
|| !data
[IFLA_IPTUN_PROTO
])
1560 proto
= nla_get_u8(data
[IFLA_IPTUN_PROTO
]);
1561 if (proto
!= IPPROTO_IPV6
&&
1562 proto
!= IPPROTO_IPIP
&&
1569 static void ip6_tnl_netlink_parms(struct nlattr
*data
[],
1570 struct __ip6_tnl_parm
*parms
)
1572 memset(parms
, 0, sizeof(*parms
));
1577 if (data
[IFLA_IPTUN_LINK
])
1578 parms
->link
= nla_get_u32(data
[IFLA_IPTUN_LINK
]);
1580 if (data
[IFLA_IPTUN_LOCAL
])
1581 nla_memcpy(&parms
->laddr
, data
[IFLA_IPTUN_LOCAL
],
1582 sizeof(struct in6_addr
));
1584 if (data
[IFLA_IPTUN_REMOTE
])
1585 nla_memcpy(&parms
->raddr
, data
[IFLA_IPTUN_REMOTE
],
1586 sizeof(struct in6_addr
));
1588 if (data
[IFLA_IPTUN_TTL
])
1589 parms
->hop_limit
= nla_get_u8(data
[IFLA_IPTUN_TTL
]);
1591 if (data
[IFLA_IPTUN_ENCAP_LIMIT
])
1592 parms
->encap_limit
= nla_get_u8(data
[IFLA_IPTUN_ENCAP_LIMIT
]);
1594 if (data
[IFLA_IPTUN_FLOWINFO
])
1595 parms
->flowinfo
= nla_get_be32(data
[IFLA_IPTUN_FLOWINFO
]);
1597 if (data
[IFLA_IPTUN_FLAGS
])
1598 parms
->flags
= nla_get_u32(data
[IFLA_IPTUN_FLAGS
]);
1600 if (data
[IFLA_IPTUN_PROTO
])
1601 parms
->proto
= nla_get_u8(data
[IFLA_IPTUN_PROTO
]);
1604 static int ip6_tnl_newlink(struct net
*src_net
, struct net_device
*dev
,
1605 struct nlattr
*tb
[], struct nlattr
*data
[])
1607 struct net
*net
= dev_net(dev
);
1610 nt
= netdev_priv(dev
);
1611 ip6_tnl_netlink_parms(data
, &nt
->parms
);
1613 if (ip6_tnl_locate(net
, &nt
->parms
, 0))
1616 return ip6_tnl_create2(dev
);
1619 static int ip6_tnl_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
1620 struct nlattr
*data
[])
1623 struct __ip6_tnl_parm p
;
1624 struct net
*net
= dev_net(dev
);
1625 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1627 if (dev
== ip6n
->fb_tnl_dev
)
1630 ip6_tnl_netlink_parms(data
, &p
);
1632 t
= ip6_tnl_locate(net
, &p
, 0);
1638 t
= netdev_priv(dev
);
1640 return ip6_tnl_update(t
, &p
);
1643 static void ip6_tnl_dellink(struct net_device
*dev
, struct list_head
*head
)
1645 struct net
*net
= dev_net(dev
);
1646 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1648 if (dev
!= ip6n
->fb_tnl_dev
)
1649 unregister_netdevice_queue(dev
, head
);
1652 static size_t ip6_tnl_get_size(const struct net_device
*dev
)
1655 /* IFLA_IPTUN_LINK */
1657 /* IFLA_IPTUN_LOCAL */
1658 nla_total_size(sizeof(struct in6_addr
)) +
1659 /* IFLA_IPTUN_REMOTE */
1660 nla_total_size(sizeof(struct in6_addr
)) +
1661 /* IFLA_IPTUN_TTL */
1663 /* IFLA_IPTUN_ENCAP_LIMIT */
1665 /* IFLA_IPTUN_FLOWINFO */
1667 /* IFLA_IPTUN_FLAGS */
1669 /* IFLA_IPTUN_PROTO */
1674 static int ip6_tnl_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1676 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1677 struct __ip6_tnl_parm
*parm
= &tunnel
->parms
;
1679 if (nla_put_u32(skb
, IFLA_IPTUN_LINK
, parm
->link
) ||
1680 nla_put(skb
, IFLA_IPTUN_LOCAL
, sizeof(struct in6_addr
),
1682 nla_put(skb
, IFLA_IPTUN_REMOTE
, sizeof(struct in6_addr
),
1684 nla_put_u8(skb
, IFLA_IPTUN_TTL
, parm
->hop_limit
) ||
1685 nla_put_u8(skb
, IFLA_IPTUN_ENCAP_LIMIT
, parm
->encap_limit
) ||
1686 nla_put_be32(skb
, IFLA_IPTUN_FLOWINFO
, parm
->flowinfo
) ||
1687 nla_put_u32(skb
, IFLA_IPTUN_FLAGS
, parm
->flags
) ||
1688 nla_put_u8(skb
, IFLA_IPTUN_PROTO
, parm
->proto
))
1689 goto nla_put_failure
;
1696 static const struct nla_policy ip6_tnl_policy
[IFLA_IPTUN_MAX
+ 1] = {
1697 [IFLA_IPTUN_LINK
] = { .type
= NLA_U32
},
1698 [IFLA_IPTUN_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
1699 [IFLA_IPTUN_REMOTE
] = { .len
= sizeof(struct in6_addr
) },
1700 [IFLA_IPTUN_TTL
] = { .type
= NLA_U8
},
1701 [IFLA_IPTUN_ENCAP_LIMIT
] = { .type
= NLA_U8
},
1702 [IFLA_IPTUN_FLOWINFO
] = { .type
= NLA_U32
},
1703 [IFLA_IPTUN_FLAGS
] = { .type
= NLA_U32
},
1704 [IFLA_IPTUN_PROTO
] = { .type
= NLA_U8
},
1707 static struct rtnl_link_ops ip6_link_ops __read_mostly
= {
1709 .maxtype
= IFLA_IPTUN_MAX
,
1710 .policy
= ip6_tnl_policy
,
1711 .priv_size
= sizeof(struct ip6_tnl
),
1712 .setup
= ip6_tnl_dev_setup
,
1713 .validate
= ip6_tnl_validate
,
1714 .newlink
= ip6_tnl_newlink
,
1715 .changelink
= ip6_tnl_changelink
,
1716 .dellink
= ip6_tnl_dellink
,
1717 .get_size
= ip6_tnl_get_size
,
1718 .fill_info
= ip6_tnl_fill_info
,
1721 static struct xfrm6_tunnel ip4ip6_handler __read_mostly
= {
1722 .handler
= ip4ip6_rcv
,
1723 .err_handler
= ip4ip6_err
,
1727 static struct xfrm6_tunnel ip6ip6_handler __read_mostly
= {
1728 .handler
= ip6ip6_rcv
,
1729 .err_handler
= ip6ip6_err
,
1733 static void __net_exit
ip6_tnl_destroy_tunnels(struct ip6_tnl_net
*ip6n
)
1739 for (h
= 0; h
< HASH_SIZE
; h
++) {
1740 t
= rtnl_dereference(ip6n
->tnls_r_l
[h
]);
1742 unregister_netdevice_queue(t
->dev
, &list
);
1743 t
= rtnl_dereference(t
->next
);
1747 t
= rtnl_dereference(ip6n
->tnls_wc
[0]);
1748 unregister_netdevice_queue(t
->dev
, &list
);
1749 unregister_netdevice_many(&list
);
1752 static int __net_init
ip6_tnl_init_net(struct net
*net
)
1754 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1755 struct ip6_tnl
*t
= NULL
;
1758 ip6n
->tnls
[0] = ip6n
->tnls_wc
;
1759 ip6n
->tnls
[1] = ip6n
->tnls_r_l
;
1762 ip6n
->fb_tnl_dev
= alloc_netdev(sizeof(struct ip6_tnl
), "ip6tnl0",
1765 if (!ip6n
->fb_tnl_dev
)
1767 dev_net_set(ip6n
->fb_tnl_dev
, net
);
1768 ip6n
->fb_tnl_dev
->rtnl_link_ops
= &ip6_link_ops
;
1770 err
= ip6_fb_tnl_dev_init(ip6n
->fb_tnl_dev
);
1774 err
= register_netdev(ip6n
->fb_tnl_dev
);
1778 t
= netdev_priv(ip6n
->fb_tnl_dev
);
1780 strcpy(t
->parms
.name
, ip6n
->fb_tnl_dev
->name
);
1784 ip6_dev_free(ip6n
->fb_tnl_dev
);
1789 static void __net_exit
ip6_tnl_exit_net(struct net
*net
)
1791 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1794 ip6_tnl_destroy_tunnels(ip6n
);
1798 static struct pernet_operations ip6_tnl_net_ops
= {
1799 .init
= ip6_tnl_init_net
,
1800 .exit
= ip6_tnl_exit_net
,
1801 .id
= &ip6_tnl_net_id
,
1802 .size
= sizeof(struct ip6_tnl_net
),
1806 * ip6_tunnel_init - register protocol and reserve needed resources
1808 * Return: 0 on success
1811 static int __init
ip6_tunnel_init(void)
1815 err
= register_pernet_device(&ip6_tnl_net_ops
);
1819 err
= xfrm6_tunnel_register(&ip4ip6_handler
, AF_INET
);
1821 pr_err("%s: can't register ip4ip6\n", __func__
);
1825 err
= xfrm6_tunnel_register(&ip6ip6_handler
, AF_INET6
);
1827 pr_err("%s: can't register ip6ip6\n", __func__
);
1830 err
= rtnl_link_register(&ip6_link_ops
);
1832 goto rtnl_link_failed
;
1837 xfrm6_tunnel_deregister(&ip6ip6_handler
, AF_INET6
);
1839 xfrm6_tunnel_deregister(&ip4ip6_handler
, AF_INET
);
1841 unregister_pernet_device(&ip6_tnl_net_ops
);
1847 * ip6_tunnel_cleanup - free resources and unregister protocol
1850 static void __exit
ip6_tunnel_cleanup(void)
1852 rtnl_link_unregister(&ip6_link_ops
);
1853 if (xfrm6_tunnel_deregister(&ip4ip6_handler
, AF_INET
))
1854 pr_info("%s: can't deregister ip4ip6\n", __func__
);
1856 if (xfrm6_tunnel_deregister(&ip6ip6_handler
, AF_INET6
))
1857 pr_info("%s: can't deregister ip6ip6\n", __func__
);
1859 unregister_pernet_device(&ip6_tnl_net_ops
);
1862 module_init(ip6_tunnel_init
);
1863 module_exit(ip6_tunnel_cleanup
);