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/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>
41 #include <linux/slab.h>
42 #include <linux/hash.h>
43 #include <linux/etherdevice.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");
67 #define HASH_SIZE_SHIFT 5
68 #define HASH_SIZE (1 << HASH_SIZE_SHIFT)
70 static bool log_ecn_error
= true;
71 module_param(log_ecn_error
, bool, 0644);
72 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
74 static u32
HASH(const struct in6_addr
*addr1
, const struct in6_addr
*addr2
)
76 u32 hash
= ipv6_addr_hash(addr1
) ^ ipv6_addr_hash(addr2
);
78 return hash_32(hash
, HASH_SIZE_SHIFT
);
81 static int ip6_tnl_dev_init(struct net_device
*dev
);
82 static void ip6_tnl_dev_setup(struct net_device
*dev
);
83 static struct rtnl_link_ops ip6_link_ops __read_mostly
;
85 static int ip6_tnl_net_id __read_mostly
;
87 /* the IPv6 tunnel fallback device */
88 struct net_device
*fb_tnl_dev
;
89 /* lists for storing tunnels in use */
90 struct ip6_tnl __rcu
*tnls_r_l
[HASH_SIZE
];
91 struct ip6_tnl __rcu
*tnls_wc
[1];
92 struct ip6_tnl __rcu
**tnls
[2];
95 static struct net_device_stats
*ip6_get_stats(struct net_device
*dev
)
97 struct pcpu_sw_netstats tmp
, sum
= { 0 };
100 for_each_possible_cpu(i
) {
102 const struct pcpu_sw_netstats
*tstats
=
103 per_cpu_ptr(dev
->tstats
, i
);
106 start
= u64_stats_fetch_begin_irq(&tstats
->syncp
);
107 tmp
.rx_packets
= tstats
->rx_packets
;
108 tmp
.rx_bytes
= tstats
->rx_bytes
;
109 tmp
.tx_packets
= tstats
->tx_packets
;
110 tmp
.tx_bytes
= tstats
->tx_bytes
;
111 } while (u64_stats_fetch_retry_irq(&tstats
->syncp
, start
));
113 sum
.rx_packets
+= tmp
.rx_packets
;
114 sum
.rx_bytes
+= tmp
.rx_bytes
;
115 sum
.tx_packets
+= tmp
.tx_packets
;
116 sum
.tx_bytes
+= tmp
.tx_bytes
;
118 dev
->stats
.rx_packets
= sum
.rx_packets
;
119 dev
->stats
.rx_bytes
= sum
.rx_bytes
;
120 dev
->stats
.tx_packets
= sum
.tx_packets
;
121 dev
->stats
.tx_bytes
= sum
.tx_bytes
;
126 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
127 * @remote: the address of the tunnel exit-point
128 * @local: the address of the tunnel entry-point
131 * tunnel matching given end-points if found,
132 * else fallback tunnel if its device is up,
136 #define for_each_ip6_tunnel_rcu(start) \
137 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
139 static struct ip6_tnl
*
140 ip6_tnl_lookup(struct net
*net
, const struct in6_addr
*remote
, const struct in6_addr
*local
)
142 unsigned int hash
= HASH(remote
, local
);
144 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
147 for_each_ip6_tunnel_rcu(ip6n
->tnls_r_l
[hash
]) {
148 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
149 ipv6_addr_equal(remote
, &t
->parms
.raddr
) &&
150 (t
->dev
->flags
& IFF_UP
))
154 memset(&any
, 0, sizeof(any
));
155 hash
= HASH(&any
, local
);
156 for_each_ip6_tunnel_rcu(ip6n
->tnls_r_l
[hash
]) {
157 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
158 ipv6_addr_any(&t
->parms
.raddr
) &&
159 (t
->dev
->flags
& IFF_UP
))
163 hash
= HASH(remote
, &any
);
164 for_each_ip6_tunnel_rcu(ip6n
->tnls_r_l
[hash
]) {
165 if (ipv6_addr_equal(remote
, &t
->parms
.raddr
) &&
166 ipv6_addr_any(&t
->parms
.laddr
) &&
167 (t
->dev
->flags
& IFF_UP
))
171 t
= rcu_dereference(ip6n
->tnls_wc
[0]);
172 if (t
&& (t
->dev
->flags
& IFF_UP
))
179 * ip6_tnl_bucket - get head of list matching given tunnel parameters
180 * @p: parameters containing tunnel end-points
183 * ip6_tnl_bucket() returns the head of the list matching the
184 * &struct in6_addr entries laddr and raddr in @p.
186 * Return: head of IPv6 tunnel list
189 static struct ip6_tnl __rcu
**
190 ip6_tnl_bucket(struct ip6_tnl_net
*ip6n
, const struct __ip6_tnl_parm
*p
)
192 const struct in6_addr
*remote
= &p
->raddr
;
193 const struct in6_addr
*local
= &p
->laddr
;
197 if (!ipv6_addr_any(remote
) || !ipv6_addr_any(local
)) {
199 h
= HASH(remote
, local
);
201 return &ip6n
->tnls
[prio
][h
];
205 * ip6_tnl_link - add tunnel to hash table
206 * @t: tunnel to be added
210 ip6_tnl_link(struct ip6_tnl_net
*ip6n
, struct ip6_tnl
*t
)
212 struct ip6_tnl __rcu
**tp
= ip6_tnl_bucket(ip6n
, &t
->parms
);
214 rcu_assign_pointer(t
->next
, rtnl_dereference(*tp
));
215 rcu_assign_pointer(*tp
, t
);
219 * ip6_tnl_unlink - remove tunnel from hash table
220 * @t: tunnel to be removed
224 ip6_tnl_unlink(struct ip6_tnl_net
*ip6n
, struct ip6_tnl
*t
)
226 struct ip6_tnl __rcu
**tp
;
227 struct ip6_tnl
*iter
;
229 for (tp
= ip6_tnl_bucket(ip6n
, &t
->parms
);
230 (iter
= rtnl_dereference(*tp
)) != NULL
;
233 rcu_assign_pointer(*tp
, t
->next
);
239 static void ip6_dev_free(struct net_device
*dev
)
241 struct ip6_tnl
*t
= netdev_priv(dev
);
243 dst_cache_destroy(&t
->dst_cache
);
244 free_percpu(dev
->tstats
);
248 static int ip6_tnl_create2(struct net_device
*dev
)
250 struct ip6_tnl
*t
= netdev_priv(dev
);
251 struct net
*net
= dev_net(dev
);
252 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
255 t
= netdev_priv(dev
);
257 dev
->rtnl_link_ops
= &ip6_link_ops
;
258 err
= register_netdevice(dev
);
262 strcpy(t
->parms
.name
, dev
->name
);
265 ip6_tnl_link(ip6n
, t
);
273 * ip6_tnl_create - create a new tunnel
274 * @p: tunnel parameters
275 * @pt: pointer to new tunnel
278 * Create tunnel matching given parameters.
281 * created tunnel or error pointer
284 static struct ip6_tnl
*ip6_tnl_create(struct net
*net
, struct __ip6_tnl_parm
*p
)
286 struct net_device
*dev
;
292 if (!dev_valid_name(p
->name
))
294 strlcpy(name
, p
->name
, IFNAMSIZ
);
296 sprintf(name
, "ip6tnl%%d");
299 dev
= alloc_netdev(sizeof(*t
), name
, NET_NAME_UNKNOWN
,
304 dev_net_set(dev
, net
);
306 t
= netdev_priv(dev
);
308 t
->net
= dev_net(dev
);
309 err
= ip6_tnl_create2(dev
);
322 * ip6_tnl_locate - find or create tunnel matching given parameters
323 * @p: tunnel parameters
324 * @create: != 0 if allowed to create new tunnel if no match found
327 * ip6_tnl_locate() first tries to locate an existing tunnel
328 * based on @parms. If this is unsuccessful, but @create is set a new
329 * tunnel device is created and registered for use.
332 * matching tunnel or error pointer
335 static struct ip6_tnl
*ip6_tnl_locate(struct net
*net
,
336 struct __ip6_tnl_parm
*p
, int create
)
338 const struct in6_addr
*remote
= &p
->raddr
;
339 const struct in6_addr
*local
= &p
->laddr
;
340 struct ip6_tnl __rcu
**tp
;
342 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
344 for (tp
= ip6_tnl_bucket(ip6n
, p
);
345 (t
= rtnl_dereference(*tp
)) != NULL
;
347 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
348 ipv6_addr_equal(remote
, &t
->parms
.raddr
)) {
350 return ERR_PTR(-EEXIST
);
356 return ERR_PTR(-ENODEV
);
357 return ip6_tnl_create(net
, p
);
361 * ip6_tnl_dev_uninit - tunnel device uninitializer
362 * @dev: the device to be destroyed
365 * ip6_tnl_dev_uninit() removes tunnel from its list
369 ip6_tnl_dev_uninit(struct net_device
*dev
)
371 struct ip6_tnl
*t
= netdev_priv(dev
);
372 struct net
*net
= t
->net
;
373 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
375 if (dev
== ip6n
->fb_tnl_dev
)
376 RCU_INIT_POINTER(ip6n
->tnls_wc
[0], NULL
);
378 ip6_tnl_unlink(ip6n
, t
);
379 dst_cache_reset(&t
->dst_cache
);
384 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
385 * @skb: received socket buffer
388 * 0 if none was found,
389 * else index to encapsulation limit
392 __u16
ip6_tnl_parse_tlv_enc_lim(struct sk_buff
*skb
, __u8
*raw
)
394 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*)raw
;
395 unsigned int nhoff
= raw
- skb
->data
;
396 unsigned int off
= nhoff
+ sizeof(*ipv6h
);
397 u8 next
, nexthdr
= ipv6h
->nexthdr
;
399 while (ipv6_ext_hdr(nexthdr
) && nexthdr
!= NEXTHDR_NONE
) {
400 struct ipv6_opt_hdr
*hdr
;
403 if (!pskb_may_pull(skb
, off
+ sizeof(*hdr
)))
406 hdr
= (struct ipv6_opt_hdr
*)(skb
->data
+ off
);
407 if (nexthdr
== NEXTHDR_FRAGMENT
) {
408 struct frag_hdr
*frag_hdr
= (struct frag_hdr
*) hdr
;
409 if (frag_hdr
->frag_off
)
412 } else if (nexthdr
== NEXTHDR_AUTH
) {
413 optlen
= (hdr
->hdrlen
+ 2) << 2;
415 optlen
= ipv6_optlen(hdr
);
417 /* cache hdr->nexthdr, since pskb_may_pull() might
421 if (nexthdr
== NEXTHDR_DEST
) {
424 /* Remember : hdr is no longer valid at this point. */
425 if (!pskb_may_pull(skb
, off
+ optlen
))
429 struct ipv6_tlv_tnl_enc_lim
*tel
;
431 /* No more room for encapsulation limit */
432 if (i
+ sizeof(*tel
) > optlen
)
435 tel
= (struct ipv6_tlv_tnl_enc_lim
*)(skb
->data
+ off
+ i
);
436 /* return index of option if found and valid */
437 if (tel
->type
== IPV6_TLV_TNL_ENCAP_LIMIT
&&
439 return i
+ off
- nhoff
;
440 /* else jump to next option */
442 i
+= tel
->length
+ 2;
452 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim
);
455 * ip6_tnl_err - tunnel error handler
458 * ip6_tnl_err() should handle errors in the tunnel according
459 * to the specifications in RFC 2473.
463 ip6_tnl_err(struct sk_buff
*skb
, __u8 ipproto
, struct inet6_skb_parm
*opt
,
464 u8
*type
, u8
*code
, int *msg
, __u32
*info
, int offset
)
466 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*) skb
->data
;
469 u8 rel_type
= ICMPV6_DEST_UNREACH
;
470 u8 rel_code
= ICMPV6_ADDR_UNREACH
;
476 /* If the packet doesn't contain the original IPv6 header we are
477 in trouble since we might need the source address for further
478 processing of the error. */
481 t
= ip6_tnl_lookup(dev_net(skb
->dev
), &ipv6h
->daddr
, &ipv6h
->saddr
);
485 tproto
= ACCESS_ONCE(t
->parms
.proto
);
486 if (tproto
!= ipproto
&& tproto
!= 0)
493 struct ipv6_tlv_tnl_enc_lim
*tel
;
495 case ICMPV6_DEST_UNREACH
:
496 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
500 case ICMPV6_TIME_EXCEED
:
501 if ((*code
) == ICMPV6_EXC_HOPLIMIT
) {
502 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
507 case ICMPV6_PARAMPROB
:
509 if ((*code
) == ICMPV6_HDR_FIELD
)
510 teli
= ip6_tnl_parse_tlv_enc_lim(skb
, skb
->data
);
512 if (teli
&& teli
== *info
- 2) {
513 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &skb
->data
[teli
];
514 if (tel
->encap_limit
== 0) {
515 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
520 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
524 case ICMPV6_PKT_TOOBIG
:
525 mtu
= *info
- offset
;
526 if (mtu
< IPV6_MIN_MTU
)
530 len
= sizeof(*ipv6h
) + ntohs(ipv6h
->payload_len
);
532 rel_type
= ICMPV6_PKT_TOOBIG
;
551 ip4ip6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
552 u8 type
, u8 code
, int offset
, __be32 info
)
557 __u32 rel_info
= ntohl(info
);
559 struct sk_buff
*skb2
;
560 const struct iphdr
*eiph
;
564 err
= ip6_tnl_err(skb
, IPPROTO_IPIP
, opt
, &rel_type
, &rel_code
,
565 &rel_msg
, &rel_info
, offset
);
573 case ICMPV6_DEST_UNREACH
:
574 if (rel_code
!= ICMPV6_ADDR_UNREACH
)
576 rel_type
= ICMP_DEST_UNREACH
;
577 rel_code
= ICMP_HOST_UNREACH
;
579 case ICMPV6_PKT_TOOBIG
:
582 rel_type
= ICMP_DEST_UNREACH
;
583 rel_code
= ICMP_FRAG_NEEDED
;
586 rel_type
= ICMP_REDIRECT
;
587 rel_code
= ICMP_REDIR_HOST
;
592 if (!pskb_may_pull(skb
, offset
+ sizeof(struct iphdr
)))
595 skb2
= skb_clone(skb
, GFP_ATOMIC
);
601 skb_pull(skb2
, offset
);
602 skb_reset_network_header(skb2
);
605 /* Try to guess incoming interface */
606 rt
= ip_route_output_ports(dev_net(skb
->dev
), &fl4
, NULL
,
609 IPPROTO_IPIP
, RT_TOS(eiph
->tos
), 0);
613 skb2
->dev
= rt
->dst
.dev
;
615 /* route "incoming" packet */
616 if (rt
->rt_flags
& RTCF_LOCAL
) {
619 rt
= ip_route_output_ports(dev_net(skb
->dev
), &fl4
, NULL
,
620 eiph
->daddr
, eiph
->saddr
,
623 RT_TOS(eiph
->tos
), 0);
625 rt
->dst
.dev
->type
!= ARPHRD_TUNNEL
) {
630 skb_dst_set(skb2
, &rt
->dst
);
633 if (ip_route_input(skb2
, eiph
->daddr
, eiph
->saddr
, eiph
->tos
,
635 skb_dst(skb2
)->dev
->type
!= ARPHRD_TUNNEL
)
639 /* change mtu on this route */
640 if (rel_type
== ICMP_DEST_UNREACH
&& rel_code
== ICMP_FRAG_NEEDED
) {
641 if (rel_info
> dst_mtu(skb_dst(skb2
)))
644 skb_dst(skb2
)->ops
->update_pmtu(skb_dst(skb2
), NULL
, skb2
, rel_info
);
646 if (rel_type
== ICMP_REDIRECT
)
647 skb_dst(skb2
)->ops
->redirect(skb_dst(skb2
), NULL
, skb2
);
649 icmp_send(skb2
, rel_type
, rel_code
, htonl(rel_info
));
657 ip6ip6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
658 u8 type
, u8 code
, int offset
, __be32 info
)
663 __u32 rel_info
= ntohl(info
);
666 err
= ip6_tnl_err(skb
, IPPROTO_IPV6
, opt
, &rel_type
, &rel_code
,
667 &rel_msg
, &rel_info
, offset
);
671 if (rel_msg
&& pskb_may_pull(skb
, offset
+ sizeof(struct ipv6hdr
))) {
673 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
679 skb_pull(skb2
, offset
);
680 skb_reset_network_header(skb2
);
682 /* Try to guess incoming interface */
683 rt
= rt6_lookup(dev_net(skb
->dev
), &ipv6_hdr(skb2
)->saddr
,
686 if (rt
&& rt
->dst
.dev
)
687 skb2
->dev
= rt
->dst
.dev
;
689 icmpv6_send(skb2
, rel_type
, rel_code
, rel_info
);
699 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl
*t
,
700 const struct ipv6hdr
*ipv6h
,
703 __u8 dsfield
= ipv6_get_dsfield(ipv6h
) & ~INET_ECN_MASK
;
705 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
706 ipv4_change_dsfield(ip_hdr(skb
), INET_ECN_MASK
, dsfield
);
708 return IP6_ECN_decapsulate(ipv6h
, skb
);
711 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl
*t
,
712 const struct ipv6hdr
*ipv6h
,
715 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
716 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h
), ipv6_hdr(skb
));
718 return IP6_ECN_decapsulate(ipv6h
, skb
);
721 __u32
ip6_tnl_get_cap(struct ip6_tnl
*t
,
722 const struct in6_addr
*laddr
,
723 const struct in6_addr
*raddr
)
725 struct __ip6_tnl_parm
*p
= &t
->parms
;
726 int ltype
= ipv6_addr_type(laddr
);
727 int rtype
= ipv6_addr_type(raddr
);
730 if (ltype
== IPV6_ADDR_ANY
|| rtype
== IPV6_ADDR_ANY
) {
731 flags
= IP6_TNL_F_CAP_PER_PACKET
;
732 } else if (ltype
& (IPV6_ADDR_UNICAST
|IPV6_ADDR_MULTICAST
) &&
733 rtype
& (IPV6_ADDR_UNICAST
|IPV6_ADDR_MULTICAST
) &&
734 !((ltype
|rtype
) & IPV6_ADDR_LOOPBACK
) &&
735 (!((ltype
|rtype
) & IPV6_ADDR_LINKLOCAL
) || p
->link
)) {
736 if (ltype
&IPV6_ADDR_UNICAST
)
737 flags
|= IP6_TNL_F_CAP_XMIT
;
738 if (rtype
&IPV6_ADDR_UNICAST
)
739 flags
|= IP6_TNL_F_CAP_RCV
;
743 EXPORT_SYMBOL(ip6_tnl_get_cap
);
745 /* called with rcu_read_lock() */
746 int ip6_tnl_rcv_ctl(struct ip6_tnl
*t
,
747 const struct in6_addr
*laddr
,
748 const struct in6_addr
*raddr
)
750 struct __ip6_tnl_parm
*p
= &t
->parms
;
752 struct net
*net
= t
->net
;
754 if ((p
->flags
& IP6_TNL_F_CAP_RCV
) ||
755 ((p
->flags
& IP6_TNL_F_CAP_PER_PACKET
) &&
756 (ip6_tnl_get_cap(t
, laddr
, raddr
) & IP6_TNL_F_CAP_RCV
))) {
757 struct net_device
*ldev
= NULL
;
760 ldev
= dev_get_by_index_rcu(net
, p
->link
);
762 if ((ipv6_addr_is_multicast(laddr
) ||
763 likely(ipv6_chk_addr(net
, laddr
, ldev
, 0))) &&
764 likely(!ipv6_chk_addr(net
, raddr
, NULL
, 0)))
769 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl
);
772 * ip6_tnl_rcv - decapsulate IPv6 packet and retransmit it locally
773 * @skb: received socket buffer
774 * @protocol: ethernet protocol ID
775 * @dscp_ecn_decapsulate: the function to decapsulate DSCP code and ECN
780 static int ip6_tnl_rcv(struct sk_buff
*skb
, __u16 protocol
,
782 int (*dscp_ecn_decapsulate
)(const struct ip6_tnl
*t
,
783 const struct ipv6hdr
*ipv6h
,
784 struct sk_buff
*skb
))
787 const struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
792 t
= ip6_tnl_lookup(dev_net(skb
->dev
), &ipv6h
->saddr
, &ipv6h
->daddr
);
794 struct pcpu_sw_netstats
*tstats
;
796 tproto
= ACCESS_ONCE(t
->parms
.proto
);
797 if (tproto
!= ipproto
&& tproto
!= 0) {
802 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
)) {
807 if (!ip6_tnl_rcv_ctl(t
, &ipv6h
->daddr
, &ipv6h
->saddr
)) {
808 t
->dev
->stats
.rx_dropped
++;
812 skb
->mac_header
= skb
->network_header
;
813 skb_reset_network_header(skb
);
814 skb
->protocol
= htons(protocol
);
815 memset(skb
->cb
, 0, sizeof(struct inet6_skb_parm
));
817 __skb_tunnel_rx(skb
, t
->dev
, t
->net
);
819 err
= dscp_ecn_decapsulate(t
, ipv6h
, skb
);
822 net_info_ratelimited("non-ECT from %pI6 with dsfield=%#x\n",
824 ipv6_get_dsfield(ipv6h
));
826 ++t
->dev
->stats
.rx_frame_errors
;
827 ++t
->dev
->stats
.rx_errors
;
833 tstats
= this_cpu_ptr(t
->dev
->tstats
);
834 u64_stats_update_begin(&tstats
->syncp
);
835 tstats
->rx_packets
++;
836 tstats
->rx_bytes
+= skb
->len
;
837 u64_stats_update_end(&tstats
->syncp
);
852 static int ip4ip6_rcv(struct sk_buff
*skb
)
854 return ip6_tnl_rcv(skb
, ETH_P_IP
, IPPROTO_IPIP
,
855 ip4ip6_dscp_ecn_decapsulate
);
858 static int ip6ip6_rcv(struct sk_buff
*skb
)
860 return ip6_tnl_rcv(skb
, ETH_P_IPV6
, IPPROTO_IPV6
,
861 ip6ip6_dscp_ecn_decapsulate
);
864 struct ipv6_tel_txoption
{
865 struct ipv6_txoptions ops
;
869 static void init_tel_txopt(struct ipv6_tel_txoption
*opt
, __u8 encap_limit
)
871 memset(opt
, 0, sizeof(struct ipv6_tel_txoption
));
873 opt
->dst_opt
[2] = IPV6_TLV_TNL_ENCAP_LIMIT
;
875 opt
->dst_opt
[4] = encap_limit
;
876 opt
->dst_opt
[5] = IPV6_TLV_PADN
;
879 opt
->ops
.dst0opt
= (struct ipv6_opt_hdr
*) opt
->dst_opt
;
880 opt
->ops
.opt_nflen
= 8;
884 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
885 * @t: the outgoing tunnel device
886 * @hdr: IPv6 header from the incoming packet
889 * Avoid trivial tunneling loop by checking that tunnel exit-point
890 * doesn't match source of incoming packet.
898 ip6_tnl_addr_conflict(const struct ip6_tnl
*t
, const struct ipv6hdr
*hdr
)
900 return ipv6_addr_equal(&t
->parms
.raddr
, &hdr
->saddr
);
903 int ip6_tnl_xmit_ctl(struct ip6_tnl
*t
,
904 const struct in6_addr
*laddr
,
905 const struct in6_addr
*raddr
)
907 struct __ip6_tnl_parm
*p
= &t
->parms
;
909 struct net
*net
= t
->net
;
911 if ((p
->flags
& IP6_TNL_F_CAP_XMIT
) ||
912 ((p
->flags
& IP6_TNL_F_CAP_PER_PACKET
) &&
913 (ip6_tnl_get_cap(t
, laddr
, raddr
) & IP6_TNL_F_CAP_XMIT
))) {
914 struct net_device
*ldev
= NULL
;
918 ldev
= dev_get_by_index_rcu(net
, p
->link
);
920 if (unlikely(!ipv6_chk_addr(net
, laddr
, ldev
, 0)))
921 pr_warn("%s xmit: Local address not yet configured!\n",
923 else if (!ipv6_addr_is_multicast(raddr
) &&
924 unlikely(ipv6_chk_addr(net
, raddr
, NULL
, 0)))
925 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
933 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl
);
936 * ip6_tnl_xmit2 - encapsulate packet and send
937 * @skb: the outgoing socket buffer
938 * @dev: the outgoing tunnel device
939 * @dsfield: dscp code for outer header
940 * @fl: flow of tunneled packet
941 * @encap_limit: encapsulation limit
942 * @pmtu: Path MTU is stored if packet is too big
945 * Build new header and do some sanity checks on the packet before sending
951 * %-EMSGSIZE message too big. return mtu in this case.
954 static int ip6_tnl_xmit2(struct sk_buff
*skb
,
955 struct net_device
*dev
,
961 struct ip6_tnl
*t
= netdev_priv(dev
);
962 struct net
*net
= t
->net
;
963 struct net_device_stats
*stats
= &t
->dev
->stats
;
964 struct ipv6hdr
*ipv6h
;
965 struct ipv6_tel_txoption opt
;
966 struct dst_entry
*dst
= NULL
, *ndst
= NULL
;
967 struct net_device
*tdev
;
969 unsigned int max_headroom
= sizeof(struct ipv6hdr
);
974 if (ipv6_addr_any(&t
->parms
.raddr
)) {
975 struct in6_addr
*addr6
;
976 struct neighbour
*neigh
;
980 goto tx_err_link_failure
;
982 neigh
= dst_neigh_lookup(skb_dst(skb
),
983 &ipv6_hdr(skb
)->daddr
);
985 goto tx_err_link_failure
;
987 addr6
= (struct in6_addr
*)&neigh
->primary_key
;
988 addr_type
= ipv6_addr_type(addr6
);
990 if (addr_type
== IPV6_ADDR_ANY
)
991 addr6
= &ipv6_hdr(skb
)->daddr
;
993 memcpy(&fl6
->daddr
, addr6
, sizeof(fl6
->daddr
));
994 neigh_release(neigh
);
995 } else if (!fl6
->flowi6_mark
)
996 dst
= dst_cache_get(&t
->dst_cache
);
998 if (!ip6_tnl_xmit_ctl(t
, &fl6
->saddr
, &fl6
->daddr
))
999 goto tx_err_link_failure
;
1002 dst
= ip6_route_output(net
, NULL
, fl6
);
1005 goto tx_err_link_failure
;
1006 dst
= xfrm_lookup(net
, dst
, flowi6_to_flowi(fl6
), NULL
, 0);
1010 goto tx_err_link_failure
;
1018 stats
->collisions
++;
1019 net_warn_ratelimited("%s: Local routing loop detected!\n",
1021 goto tx_err_dst_release
;
1023 mtu
= dst_mtu(dst
) - sizeof(*ipv6h
);
1024 if (encap_limit
>= 0) {
1028 if (mtu
< IPV6_MIN_MTU
)
1031 skb_dst(skb
)->ops
->update_pmtu(skb_dst(skb
), NULL
, skb
, mtu
);
1032 if (skb
->len
> mtu
) {
1035 goto tx_err_dst_release
;
1038 skb_scrub_packet(skb
, !net_eq(t
->net
, dev_net(dev
)));
1041 * Okay, now see if we can stuff it in the buffer as-is.
1043 max_headroom
+= LL_RESERVED_SPACE(tdev
);
1045 if (skb_headroom(skb
) < max_headroom
|| skb_shared(skb
) ||
1046 (skb_cloned(skb
) && !skb_clone_writable(skb
, 0))) {
1047 struct sk_buff
*new_skb
;
1049 new_skb
= skb_realloc_headroom(skb
, max_headroom
);
1051 goto tx_err_dst_release
;
1054 skb_set_owner_w(new_skb
, skb
->sk
);
1059 if (!fl6
->flowi6_mark
&& ndst
)
1060 dst_cache_set_ip6(&t
->dst_cache
, ndst
, &fl6
->saddr
);
1061 skb_dst_set(skb
, dst
);
1063 skb
->transport_header
= skb
->network_header
;
1065 proto
= fl6
->flowi6_proto
;
1066 if (encap_limit
>= 0) {
1067 init_tel_txopt(&opt
, encap_limit
);
1068 ipv6_push_nfrag_opts(skb
, &opt
.ops
, &proto
, NULL
);
1071 if (likely(!skb
->encapsulation
)) {
1072 skb_reset_inner_headers(skb
);
1073 skb
->encapsulation
= 1;
1076 skb_push(skb
, sizeof(struct ipv6hdr
));
1077 skb_reset_network_header(skb
);
1078 ipv6h
= ipv6_hdr(skb
);
1079 ip6_flow_hdr(ipv6h
, INET_ECN_encapsulate(0, dsfield
),
1080 ip6_make_flowlabel(net
, skb
, fl6
->flowlabel
, true, fl6
));
1081 ipv6h
->hop_limit
= t
->parms
.hop_limit
;
1082 ipv6h
->nexthdr
= proto
;
1083 ipv6h
->saddr
= fl6
->saddr
;
1084 ipv6h
->daddr
= fl6
->daddr
;
1085 ip6tunnel_xmit(NULL
, skb
, dev
);
1087 tx_err_link_failure
:
1088 stats
->tx_carrier_errors
++;
1089 dst_link_failure(skb
);
1096 ip4ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1098 struct ip6_tnl
*t
= netdev_priv(dev
);
1099 const struct iphdr
*iph
= ip_hdr(skb
);
1100 int encap_limit
= -1;
1107 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1109 tproto
= ACCESS_ONCE(t
->parms
.proto
);
1110 if (tproto
!= IPPROTO_IPIP
&& tproto
!= 0)
1113 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1114 encap_limit
= t
->parms
.encap_limit
;
1116 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
1117 fl6
.flowi6_proto
= IPPROTO_IPIP
;
1119 dsfield
= ipv4_get_dsfield(iph
);
1121 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
1122 fl6
.flowlabel
|= htonl((__u32
)iph
->tos
<< IPV6_TCLASS_SHIFT
)
1124 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
1125 fl6
.flowi6_mark
= skb
->mark
;
1127 err
= ip6_tnl_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
1129 /* XXX: send ICMP error even if DF is not set. */
1130 if (err
== -EMSGSIZE
)
1131 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
1140 ip6ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1142 struct ip6_tnl
*t
= netdev_priv(dev
);
1143 struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
1144 int encap_limit
= -1;
1152 tproto
= ACCESS_ONCE(t
->parms
.proto
);
1153 if ((tproto
!= IPPROTO_IPV6
&& tproto
!= 0) ||
1154 ip6_tnl_addr_conflict(t
, ipv6h
))
1157 offset
= ip6_tnl_parse_tlv_enc_lim(skb
, skb_network_header(skb
));
1159 struct ipv6_tlv_tnl_enc_lim
*tel
;
1160 tel
= (struct ipv6_tlv_tnl_enc_lim
*)&skb_network_header(skb
)[offset
];
1161 if (tel
->encap_limit
== 0) {
1162 icmpv6_send(skb
, ICMPV6_PARAMPROB
,
1163 ICMPV6_HDR_FIELD
, offset
+ 2);
1166 encap_limit
= tel
->encap_limit
- 1;
1167 } else if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1168 encap_limit
= t
->parms
.encap_limit
;
1170 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
1171 fl6
.flowi6_proto
= IPPROTO_IPV6
;
1173 dsfield
= ipv6_get_dsfield(ipv6h
);
1174 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
1175 fl6
.flowlabel
|= (*(__be32
*) ipv6h
& IPV6_TCLASS_MASK
);
1176 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
1177 fl6
.flowlabel
|= ip6_flowlabel(ipv6h
);
1178 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
1179 fl6
.flowi6_mark
= skb
->mark
;
1181 err
= ip6_tnl_xmit2(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
);
1183 if (err
== -EMSGSIZE
)
1184 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
1192 ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1194 struct ip6_tnl
*t
= netdev_priv(dev
);
1195 struct net_device_stats
*stats
= &t
->dev
->stats
;
1198 switch (skb
->protocol
) {
1199 case htons(ETH_P_IP
):
1200 ret
= ip4ip6_tnl_xmit(skb
, dev
);
1202 case htons(ETH_P_IPV6
):
1203 ret
= ip6ip6_tnl_xmit(skb
, dev
);
1212 return NETDEV_TX_OK
;
1216 stats
->tx_dropped
++;
1218 return NETDEV_TX_OK
;
1221 static void ip6_tnl_link_config(struct ip6_tnl
*t
)
1223 struct net_device
*dev
= t
->dev
;
1224 struct __ip6_tnl_parm
*p
= &t
->parms
;
1225 struct flowi6
*fl6
= &t
->fl
.u
.ip6
;
1227 memcpy(dev
->dev_addr
, &p
->laddr
, sizeof(struct in6_addr
));
1228 memcpy(dev
->broadcast
, &p
->raddr
, sizeof(struct in6_addr
));
1230 /* Set up flowi template */
1231 fl6
->saddr
= p
->laddr
;
1232 fl6
->daddr
= p
->raddr
;
1233 fl6
->flowi6_oif
= p
->link
;
1236 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_TCLASS
))
1237 fl6
->flowlabel
|= IPV6_TCLASS_MASK
& p
->flowinfo
;
1238 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_FLOWLABEL
))
1239 fl6
->flowlabel
|= IPV6_FLOWLABEL_MASK
& p
->flowinfo
;
1241 p
->flags
&= ~(IP6_TNL_F_CAP_XMIT
|IP6_TNL_F_CAP_RCV
|IP6_TNL_F_CAP_PER_PACKET
);
1242 p
->flags
|= ip6_tnl_get_cap(t
, &p
->laddr
, &p
->raddr
);
1244 if (p
->flags
&IP6_TNL_F_CAP_XMIT
&& p
->flags
&IP6_TNL_F_CAP_RCV
)
1245 dev
->flags
|= IFF_POINTOPOINT
;
1247 dev
->flags
&= ~IFF_POINTOPOINT
;
1249 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
1250 int strict
= (ipv6_addr_type(&p
->raddr
) &
1251 (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
));
1253 struct rt6_info
*rt
= rt6_lookup(t
->net
,
1254 &p
->raddr
, &p
->laddr
,
1261 dev
->hard_header_len
= rt
->dst
.dev
->hard_header_len
+
1262 sizeof(struct ipv6hdr
);
1264 dev
->mtu
= rt
->dst
.dev
->mtu
- sizeof(struct ipv6hdr
);
1265 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1268 if (dev
->mtu
< IPV6_MIN_MTU
)
1269 dev
->mtu
= IPV6_MIN_MTU
;
1276 * ip6_tnl_change - update the tunnel parameters
1277 * @t: tunnel to be changed
1278 * @p: tunnel configuration parameters
1281 * ip6_tnl_change() updates the tunnel parameters
1285 ip6_tnl_change(struct ip6_tnl
*t
, const struct __ip6_tnl_parm
*p
)
1287 t
->parms
.laddr
= p
->laddr
;
1288 t
->parms
.raddr
= p
->raddr
;
1289 t
->parms
.flags
= p
->flags
;
1290 t
->parms
.hop_limit
= p
->hop_limit
;
1291 t
->parms
.encap_limit
= p
->encap_limit
;
1292 t
->parms
.flowinfo
= p
->flowinfo
;
1293 t
->parms
.link
= p
->link
;
1294 t
->parms
.proto
= p
->proto
;
1295 dst_cache_reset(&t
->dst_cache
);
1296 ip6_tnl_link_config(t
);
1300 static int ip6_tnl_update(struct ip6_tnl
*t
, struct __ip6_tnl_parm
*p
)
1302 struct net
*net
= t
->net
;
1303 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1306 ip6_tnl_unlink(ip6n
, t
);
1308 err
= ip6_tnl_change(t
, p
);
1309 ip6_tnl_link(ip6n
, t
);
1310 netdev_state_change(t
->dev
);
1314 static int ip6_tnl0_update(struct ip6_tnl
*t
, struct __ip6_tnl_parm
*p
)
1316 /* for default tnl0 device allow to change only the proto */
1317 t
->parms
.proto
= p
->proto
;
1318 netdev_state_change(t
->dev
);
1323 ip6_tnl_parm_from_user(struct __ip6_tnl_parm
*p
, const struct ip6_tnl_parm
*u
)
1325 p
->laddr
= u
->laddr
;
1326 p
->raddr
= u
->raddr
;
1327 p
->flags
= u
->flags
;
1328 p
->hop_limit
= u
->hop_limit
;
1329 p
->encap_limit
= u
->encap_limit
;
1330 p
->flowinfo
= u
->flowinfo
;
1332 p
->proto
= u
->proto
;
1333 memcpy(p
->name
, u
->name
, sizeof(u
->name
));
1337 ip6_tnl_parm_to_user(struct ip6_tnl_parm
*u
, const struct __ip6_tnl_parm
*p
)
1339 u
->laddr
= p
->laddr
;
1340 u
->raddr
= p
->raddr
;
1341 u
->flags
= p
->flags
;
1342 u
->hop_limit
= p
->hop_limit
;
1343 u
->encap_limit
= p
->encap_limit
;
1344 u
->flowinfo
= p
->flowinfo
;
1346 u
->proto
= p
->proto
;
1347 memcpy(u
->name
, p
->name
, sizeof(u
->name
));
1351 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1352 * @dev: virtual device associated with tunnel
1353 * @ifr: parameters passed from userspace
1354 * @cmd: command to be performed
1357 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1360 * The possible commands are the following:
1361 * %SIOCGETTUNNEL: get tunnel parameters for device
1362 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1363 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1364 * %SIOCDELTUNNEL: delete tunnel
1366 * The fallback device "ip6tnl0", created during module
1367 * initialization, can be used for creating other tunnel devices.
1371 * %-EFAULT if unable to copy data to or from userspace,
1372 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1373 * %-EINVAL if passed tunnel parameters are invalid,
1374 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1375 * %-ENODEV if attempting to change or delete a nonexisting device
1379 ip6_tnl_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1382 struct ip6_tnl_parm p
;
1383 struct __ip6_tnl_parm p1
;
1384 struct ip6_tnl
*t
= netdev_priv(dev
);
1385 struct net
*net
= t
->net
;
1386 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1390 if (dev
== ip6n
->fb_tnl_dev
) {
1391 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
))) {
1395 ip6_tnl_parm_from_user(&p1
, &p
);
1396 t
= ip6_tnl_locate(net
, &p1
, 0);
1398 t
= netdev_priv(dev
);
1400 memset(&p
, 0, sizeof(p
));
1402 ip6_tnl_parm_to_user(&p
, &t
->parms
);
1403 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
))) {
1410 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1413 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1416 if (p
.proto
!= IPPROTO_IPV6
&& p
.proto
!= IPPROTO_IPIP
&&
1419 ip6_tnl_parm_from_user(&p1
, &p
);
1420 t
= ip6_tnl_locate(net
, &p1
, cmd
== SIOCADDTUNNEL
);
1421 if (cmd
== SIOCCHGTUNNEL
) {
1423 if (t
->dev
!= dev
) {
1428 t
= netdev_priv(dev
);
1429 if (dev
== ip6n
->fb_tnl_dev
)
1430 err
= ip6_tnl0_update(t
, &p1
);
1432 err
= ip6_tnl_update(t
, &p1
);
1436 ip6_tnl_parm_to_user(&p
, &t
->parms
);
1437 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1446 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1449 if (dev
== ip6n
->fb_tnl_dev
) {
1451 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1454 ip6_tnl_parm_from_user(&p1
, &p
);
1455 t
= ip6_tnl_locate(net
, &p1
, 0);
1459 if (t
->dev
== ip6n
->fb_tnl_dev
)
1464 unregister_netdevice(dev
);
1473 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1474 * @dev: virtual device associated with tunnel
1475 * @new_mtu: the new mtu
1479 * %-EINVAL if mtu too small
1483 ip6_tnl_change_mtu(struct net_device
*dev
, int new_mtu
)
1485 struct ip6_tnl
*tnl
= netdev_priv(dev
);
1487 if (tnl
->parms
.proto
== IPPROTO_IPIP
) {
1491 if (new_mtu
< IPV6_MIN_MTU
)
1494 if (new_mtu
> 0xFFF8 - dev
->hard_header_len
)
1500 int ip6_tnl_get_iflink(const struct net_device
*dev
)
1502 struct ip6_tnl
*t
= netdev_priv(dev
);
1504 return t
->parms
.link
;
1506 EXPORT_SYMBOL(ip6_tnl_get_iflink
);
1508 static const struct net_device_ops ip6_tnl_netdev_ops
= {
1509 .ndo_init
= ip6_tnl_dev_init
,
1510 .ndo_uninit
= ip6_tnl_dev_uninit
,
1511 .ndo_start_xmit
= ip6_tnl_xmit
,
1512 .ndo_do_ioctl
= ip6_tnl_ioctl
,
1513 .ndo_change_mtu
= ip6_tnl_change_mtu
,
1514 .ndo_get_stats
= ip6_get_stats
,
1515 .ndo_get_iflink
= ip6_tnl_get_iflink
,
1520 * ip6_tnl_dev_setup - setup virtual tunnel device
1521 * @dev: virtual device associated with tunnel
1524 * Initialize function pointers and device parameters
1527 static void ip6_tnl_dev_setup(struct net_device
*dev
)
1531 dev
->netdev_ops
= &ip6_tnl_netdev_ops
;
1532 dev
->destructor
= ip6_dev_free
;
1534 dev
->type
= ARPHRD_TUNNEL6
;
1535 dev
->hard_header_len
= LL_MAX_HEADER
+ sizeof(struct ipv6hdr
);
1536 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct ipv6hdr
);
1537 t
= netdev_priv(dev
);
1538 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1540 dev
->flags
|= IFF_NOARP
;
1541 dev
->addr_len
= sizeof(struct in6_addr
);
1542 netif_keep_dst(dev
);
1543 /* This perm addr will be used as interface identifier by IPv6 */
1544 dev
->addr_assign_type
= NET_ADDR_RANDOM
;
1545 eth_random_addr(dev
->perm_addr
);
1550 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1551 * @dev: virtual device associated with tunnel
1555 ip6_tnl_dev_init_gen(struct net_device
*dev
)
1557 struct ip6_tnl
*t
= netdev_priv(dev
);
1561 t
->net
= dev_net(dev
);
1562 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
1566 ret
= dst_cache_init(&t
->dst_cache
, GFP_KERNEL
);
1568 free_percpu(dev
->tstats
);
1577 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1578 * @dev: virtual device associated with tunnel
1581 static int ip6_tnl_dev_init(struct net_device
*dev
)
1583 struct ip6_tnl
*t
= netdev_priv(dev
);
1584 int err
= ip6_tnl_dev_init_gen(dev
);
1588 ip6_tnl_link_config(t
);
1593 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1594 * @dev: fallback device
1599 static int __net_init
ip6_fb_tnl_dev_init(struct net_device
*dev
)
1601 struct ip6_tnl
*t
= netdev_priv(dev
);
1602 struct net
*net
= dev_net(dev
);
1603 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1605 t
->parms
.proto
= IPPROTO_IPV6
;
1608 rcu_assign_pointer(ip6n
->tnls_wc
[0], t
);
1612 static int ip6_tnl_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
1616 if (!data
|| !data
[IFLA_IPTUN_PROTO
])
1619 proto
= nla_get_u8(data
[IFLA_IPTUN_PROTO
]);
1620 if (proto
!= IPPROTO_IPV6
&&
1621 proto
!= IPPROTO_IPIP
&&
1628 static void ip6_tnl_netlink_parms(struct nlattr
*data
[],
1629 struct __ip6_tnl_parm
*parms
)
1631 memset(parms
, 0, sizeof(*parms
));
1636 if (data
[IFLA_IPTUN_LINK
])
1637 parms
->link
= nla_get_u32(data
[IFLA_IPTUN_LINK
]);
1639 if (data
[IFLA_IPTUN_LOCAL
])
1640 parms
->laddr
= nla_get_in6_addr(data
[IFLA_IPTUN_LOCAL
]);
1642 if (data
[IFLA_IPTUN_REMOTE
])
1643 parms
->raddr
= nla_get_in6_addr(data
[IFLA_IPTUN_REMOTE
]);
1645 if (data
[IFLA_IPTUN_TTL
])
1646 parms
->hop_limit
= nla_get_u8(data
[IFLA_IPTUN_TTL
]);
1648 if (data
[IFLA_IPTUN_ENCAP_LIMIT
])
1649 parms
->encap_limit
= nla_get_u8(data
[IFLA_IPTUN_ENCAP_LIMIT
]);
1651 if (data
[IFLA_IPTUN_FLOWINFO
])
1652 parms
->flowinfo
= nla_get_be32(data
[IFLA_IPTUN_FLOWINFO
]);
1654 if (data
[IFLA_IPTUN_FLAGS
])
1655 parms
->flags
= nla_get_u32(data
[IFLA_IPTUN_FLAGS
]);
1657 if (data
[IFLA_IPTUN_PROTO
])
1658 parms
->proto
= nla_get_u8(data
[IFLA_IPTUN_PROTO
]);
1661 static int ip6_tnl_newlink(struct net
*src_net
, struct net_device
*dev
,
1662 struct nlattr
*tb
[], struct nlattr
*data
[])
1664 struct net
*net
= dev_net(dev
);
1665 struct ip6_tnl
*nt
, *t
;
1667 nt
= netdev_priv(dev
);
1668 ip6_tnl_netlink_parms(data
, &nt
->parms
);
1670 t
= ip6_tnl_locate(net
, &nt
->parms
, 0);
1674 return ip6_tnl_create2(dev
);
1677 static int ip6_tnl_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
1678 struct nlattr
*data
[])
1680 struct ip6_tnl
*t
= netdev_priv(dev
);
1681 struct __ip6_tnl_parm p
;
1682 struct net
*net
= t
->net
;
1683 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1685 if (dev
== ip6n
->fb_tnl_dev
)
1688 ip6_tnl_netlink_parms(data
, &p
);
1690 t
= ip6_tnl_locate(net
, &p
, 0);
1695 t
= netdev_priv(dev
);
1697 return ip6_tnl_update(t
, &p
);
1700 static void ip6_tnl_dellink(struct net_device
*dev
, struct list_head
*head
)
1702 struct net
*net
= dev_net(dev
);
1703 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1705 if (dev
!= ip6n
->fb_tnl_dev
)
1706 unregister_netdevice_queue(dev
, head
);
1709 static size_t ip6_tnl_get_size(const struct net_device
*dev
)
1712 /* IFLA_IPTUN_LINK */
1714 /* IFLA_IPTUN_LOCAL */
1715 nla_total_size(sizeof(struct in6_addr
)) +
1716 /* IFLA_IPTUN_REMOTE */
1717 nla_total_size(sizeof(struct in6_addr
)) +
1718 /* IFLA_IPTUN_TTL */
1720 /* IFLA_IPTUN_ENCAP_LIMIT */
1722 /* IFLA_IPTUN_FLOWINFO */
1724 /* IFLA_IPTUN_FLAGS */
1726 /* IFLA_IPTUN_PROTO */
1731 static int ip6_tnl_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
1733 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1734 struct __ip6_tnl_parm
*parm
= &tunnel
->parms
;
1736 if (nla_put_u32(skb
, IFLA_IPTUN_LINK
, parm
->link
) ||
1737 nla_put_in6_addr(skb
, IFLA_IPTUN_LOCAL
, &parm
->laddr
) ||
1738 nla_put_in6_addr(skb
, IFLA_IPTUN_REMOTE
, &parm
->raddr
) ||
1739 nla_put_u8(skb
, IFLA_IPTUN_TTL
, parm
->hop_limit
) ||
1740 nla_put_u8(skb
, IFLA_IPTUN_ENCAP_LIMIT
, parm
->encap_limit
) ||
1741 nla_put_be32(skb
, IFLA_IPTUN_FLOWINFO
, parm
->flowinfo
) ||
1742 nla_put_u32(skb
, IFLA_IPTUN_FLAGS
, parm
->flags
) ||
1743 nla_put_u8(skb
, IFLA_IPTUN_PROTO
, parm
->proto
))
1744 goto nla_put_failure
;
1751 struct net
*ip6_tnl_get_link_net(const struct net_device
*dev
)
1753 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
1757 EXPORT_SYMBOL(ip6_tnl_get_link_net
);
1759 static const struct nla_policy ip6_tnl_policy
[IFLA_IPTUN_MAX
+ 1] = {
1760 [IFLA_IPTUN_LINK
] = { .type
= NLA_U32
},
1761 [IFLA_IPTUN_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
1762 [IFLA_IPTUN_REMOTE
] = { .len
= sizeof(struct in6_addr
) },
1763 [IFLA_IPTUN_TTL
] = { .type
= NLA_U8
},
1764 [IFLA_IPTUN_ENCAP_LIMIT
] = { .type
= NLA_U8
},
1765 [IFLA_IPTUN_FLOWINFO
] = { .type
= NLA_U32
},
1766 [IFLA_IPTUN_FLAGS
] = { .type
= NLA_U32
},
1767 [IFLA_IPTUN_PROTO
] = { .type
= NLA_U8
},
1770 static struct rtnl_link_ops ip6_link_ops __read_mostly
= {
1772 .maxtype
= IFLA_IPTUN_MAX
,
1773 .policy
= ip6_tnl_policy
,
1774 .priv_size
= sizeof(struct ip6_tnl
),
1775 .setup
= ip6_tnl_dev_setup
,
1776 .validate
= ip6_tnl_validate
,
1777 .newlink
= ip6_tnl_newlink
,
1778 .changelink
= ip6_tnl_changelink
,
1779 .dellink
= ip6_tnl_dellink
,
1780 .get_size
= ip6_tnl_get_size
,
1781 .fill_info
= ip6_tnl_fill_info
,
1782 .get_link_net
= ip6_tnl_get_link_net
,
1785 static struct xfrm6_tunnel ip4ip6_handler __read_mostly
= {
1786 .handler
= ip4ip6_rcv
,
1787 .err_handler
= ip4ip6_err
,
1791 static struct xfrm6_tunnel ip6ip6_handler __read_mostly
= {
1792 .handler
= ip6ip6_rcv
,
1793 .err_handler
= ip6ip6_err
,
1797 static void __net_exit
ip6_tnl_destroy_tunnels(struct net
*net
)
1799 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1800 struct net_device
*dev
, *aux
;
1805 for_each_netdev_safe(net
, dev
, aux
)
1806 if (dev
->rtnl_link_ops
== &ip6_link_ops
)
1807 unregister_netdevice_queue(dev
, &list
);
1809 for (h
= 0; h
< HASH_SIZE
; h
++) {
1810 t
= rtnl_dereference(ip6n
->tnls_r_l
[h
]);
1812 /* If dev is in the same netns, it has already
1813 * been added to the list by the previous loop.
1815 if (!net_eq(dev_net(t
->dev
), net
))
1816 unregister_netdevice_queue(t
->dev
, &list
);
1817 t
= rtnl_dereference(t
->next
);
1821 unregister_netdevice_many(&list
);
1824 static int __net_init
ip6_tnl_init_net(struct net
*net
)
1826 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1827 struct ip6_tnl
*t
= NULL
;
1830 ip6n
->tnls
[0] = ip6n
->tnls_wc
;
1831 ip6n
->tnls
[1] = ip6n
->tnls_r_l
;
1834 ip6n
->fb_tnl_dev
= alloc_netdev(sizeof(struct ip6_tnl
), "ip6tnl0",
1835 NET_NAME_UNKNOWN
, ip6_tnl_dev_setup
);
1837 if (!ip6n
->fb_tnl_dev
)
1839 dev_net_set(ip6n
->fb_tnl_dev
, net
);
1840 ip6n
->fb_tnl_dev
->rtnl_link_ops
= &ip6_link_ops
;
1841 /* FB netdevice is special: we have one, and only one per netns.
1842 * Allowing to move it to another netns is clearly unsafe.
1844 ip6n
->fb_tnl_dev
->features
|= NETIF_F_NETNS_LOCAL
;
1846 err
= ip6_fb_tnl_dev_init(ip6n
->fb_tnl_dev
);
1850 err
= register_netdev(ip6n
->fb_tnl_dev
);
1854 t
= netdev_priv(ip6n
->fb_tnl_dev
);
1856 strcpy(t
->parms
.name
, ip6n
->fb_tnl_dev
->name
);
1860 ip6_dev_free(ip6n
->fb_tnl_dev
);
1865 static void __net_exit
ip6_tnl_exit_net(struct net
*net
)
1868 ip6_tnl_destroy_tunnels(net
);
1872 static struct pernet_operations ip6_tnl_net_ops
= {
1873 .init
= ip6_tnl_init_net
,
1874 .exit
= ip6_tnl_exit_net
,
1875 .id
= &ip6_tnl_net_id
,
1876 .size
= sizeof(struct ip6_tnl_net
),
1880 * ip6_tunnel_init - register protocol and reserve needed resources
1882 * Return: 0 on success
1885 static int __init
ip6_tunnel_init(void)
1889 err
= register_pernet_device(&ip6_tnl_net_ops
);
1893 err
= xfrm6_tunnel_register(&ip4ip6_handler
, AF_INET
);
1895 pr_err("%s: can't register ip4ip6\n", __func__
);
1899 err
= xfrm6_tunnel_register(&ip6ip6_handler
, AF_INET6
);
1901 pr_err("%s: can't register ip6ip6\n", __func__
);
1904 err
= rtnl_link_register(&ip6_link_ops
);
1906 goto rtnl_link_failed
;
1911 xfrm6_tunnel_deregister(&ip6ip6_handler
, AF_INET6
);
1913 xfrm6_tunnel_deregister(&ip4ip6_handler
, AF_INET
);
1915 unregister_pernet_device(&ip6_tnl_net_ops
);
1921 * ip6_tunnel_cleanup - free resources and unregister protocol
1924 static void __exit
ip6_tunnel_cleanup(void)
1926 rtnl_link_unregister(&ip6_link_ops
);
1927 if (xfrm6_tunnel_deregister(&ip4ip6_handler
, AF_INET
))
1928 pr_info("%s: can't deregister ip4ip6\n", __func__
);
1930 if (xfrm6_tunnel_deregister(&ip6ip6_handler
, AF_INET6
))
1931 pr_info("%s: can't deregister ip6ip6\n", __func__
);
1933 unregister_pernet_device(&ip6_tnl_net_ops
);
1936 module_init(ip6_tunnel_init
);
1937 module_exit(ip6_tunnel_cleanup
);