1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPv6 tunneling device
4 * Linux INET6 implementation
7 * Ville Nuorvala <vnuorval@tcs.hut.fi>
8 * Yasuyuki Kozakai <kozakai@linux-ipv6.org>
11 * linux/net/ipv6/sit.c and linux/net/ipv4/ipip.c
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/capability.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/sockios.h>
23 #include <linux/icmp.h>
27 #include <linux/net.h>
28 #include <linux/in6.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/icmpv6.h>
32 #include <linux/init.h>
33 #include <linux/route.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/netfilter_ipv6.h>
36 #include <linux/slab.h>
37 #include <linux/hash.h>
38 #include <linux/etherdevice.h>
40 #include <linux/uaccess.h>
41 #include <linux/atomic.h>
45 #include <net/ip_tunnels.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>
53 #include <net/net_namespace.h>
54 #include <net/netns/generic.h>
55 #include <net/dst_metadata.h>
57 MODULE_AUTHOR("Ville Nuorvala");
58 MODULE_DESCRIPTION("IPv6 tunneling device");
59 MODULE_LICENSE("GPL");
60 MODULE_ALIAS_RTNL_LINK("ip6tnl");
61 MODULE_ALIAS_NETDEV("ip6tnl0");
63 #define IP6_TUNNEL_HASH_SIZE_SHIFT 5
64 #define IP6_TUNNEL_HASH_SIZE (1 << IP6_TUNNEL_HASH_SIZE_SHIFT)
66 static bool log_ecn_error
= true;
67 module_param(log_ecn_error
, bool, 0644);
68 MODULE_PARM_DESC(log_ecn_error
, "Log packets received with corrupted ECN");
70 static u32
HASH(const struct in6_addr
*addr1
, const struct in6_addr
*addr2
)
72 u32 hash
= ipv6_addr_hash(addr1
) ^ ipv6_addr_hash(addr2
);
74 return hash_32(hash
, IP6_TUNNEL_HASH_SIZE_SHIFT
);
77 static int ip6_tnl_dev_init(struct net_device
*dev
);
78 static void ip6_tnl_dev_setup(struct net_device
*dev
);
79 static struct rtnl_link_ops ip6_link_ops __read_mostly
;
81 static unsigned int ip6_tnl_net_id __read_mostly
;
83 /* the IPv6 tunnel fallback device */
84 struct net_device
*fb_tnl_dev
;
85 /* lists for storing tunnels in use */
86 struct ip6_tnl __rcu
*tnls_r_l
[IP6_TUNNEL_HASH_SIZE
];
87 struct ip6_tnl __rcu
*tnls_wc
[1];
88 struct ip6_tnl __rcu
**tnls
[2];
89 struct ip6_tnl __rcu
*collect_md_tun
;
92 static struct net_device_stats
*ip6_get_stats(struct net_device
*dev
)
94 struct pcpu_sw_netstats tmp
, sum
= { 0 };
97 for_each_possible_cpu(i
) {
99 const struct pcpu_sw_netstats
*tstats
=
100 per_cpu_ptr(dev
->tstats
, i
);
103 start
= u64_stats_fetch_begin_irq(&tstats
->syncp
);
104 tmp
.rx_packets
= tstats
->rx_packets
;
105 tmp
.rx_bytes
= tstats
->rx_bytes
;
106 tmp
.tx_packets
= tstats
->tx_packets
;
107 tmp
.tx_bytes
= tstats
->tx_bytes
;
108 } while (u64_stats_fetch_retry_irq(&tstats
->syncp
, start
));
110 sum
.rx_packets
+= tmp
.rx_packets
;
111 sum
.rx_bytes
+= tmp
.rx_bytes
;
112 sum
.tx_packets
+= tmp
.tx_packets
;
113 sum
.tx_bytes
+= tmp
.tx_bytes
;
115 dev
->stats
.rx_packets
= sum
.rx_packets
;
116 dev
->stats
.rx_bytes
= sum
.rx_bytes
;
117 dev
->stats
.tx_packets
= sum
.tx_packets
;
118 dev
->stats
.tx_bytes
= sum
.tx_bytes
;
123 * ip6_tnl_lookup - fetch tunnel matching the end-point addresses
124 * @link: ifindex of underlying interface
125 * @remote: the address of the tunnel exit-point
126 * @local: the address of the tunnel entry-point
129 * tunnel matching given end-points if found,
130 * else fallback tunnel if its device is up,
134 #define for_each_ip6_tunnel_rcu(start) \
135 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
137 static struct ip6_tnl
*
138 ip6_tnl_lookup(struct net
*net
, int link
,
139 const struct in6_addr
*remote
, const struct in6_addr
*local
)
141 unsigned int hash
= HASH(remote
, local
);
142 struct ip6_tnl
*t
, *cand
= NULL
;
143 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
146 for_each_ip6_tunnel_rcu(ip6n
->tnls_r_l
[hash
]) {
147 if (!ipv6_addr_equal(local
, &t
->parms
.laddr
) ||
148 !ipv6_addr_equal(remote
, &t
->parms
.raddr
) ||
149 !(t
->dev
->flags
& IFF_UP
))
152 if (link
== t
->parms
.link
)
158 memset(&any
, 0, sizeof(any
));
159 hash
= HASH(&any
, local
);
160 for_each_ip6_tunnel_rcu(ip6n
->tnls_r_l
[hash
]) {
161 if (!ipv6_addr_equal(local
, &t
->parms
.laddr
) ||
162 !ipv6_addr_any(&t
->parms
.raddr
) ||
163 !(t
->dev
->flags
& IFF_UP
))
166 if (link
== t
->parms
.link
)
172 hash
= HASH(remote
, &any
);
173 for_each_ip6_tunnel_rcu(ip6n
->tnls_r_l
[hash
]) {
174 if (!ipv6_addr_equal(remote
, &t
->parms
.raddr
) ||
175 !ipv6_addr_any(&t
->parms
.laddr
) ||
176 !(t
->dev
->flags
& IFF_UP
))
179 if (link
== t
->parms
.link
)
188 t
= rcu_dereference(ip6n
->collect_md_tun
);
189 if (t
&& t
->dev
->flags
& IFF_UP
)
192 t
= rcu_dereference(ip6n
->tnls_wc
[0]);
193 if (t
&& (t
->dev
->flags
& IFF_UP
))
200 * ip6_tnl_bucket - get head of list matching given tunnel parameters
201 * @p: parameters containing tunnel end-points
204 * ip6_tnl_bucket() returns the head of the list matching the
205 * &struct in6_addr entries laddr and raddr in @p.
207 * Return: head of IPv6 tunnel list
210 static struct ip6_tnl __rcu
**
211 ip6_tnl_bucket(struct ip6_tnl_net
*ip6n
, const struct __ip6_tnl_parm
*p
)
213 const struct in6_addr
*remote
= &p
->raddr
;
214 const struct in6_addr
*local
= &p
->laddr
;
218 if (!ipv6_addr_any(remote
) || !ipv6_addr_any(local
)) {
220 h
= HASH(remote
, local
);
222 return &ip6n
->tnls
[prio
][h
];
226 * ip6_tnl_link - add tunnel to hash table
227 * @t: tunnel to be added
231 ip6_tnl_link(struct ip6_tnl_net
*ip6n
, struct ip6_tnl
*t
)
233 struct ip6_tnl __rcu
**tp
= ip6_tnl_bucket(ip6n
, &t
->parms
);
235 if (t
->parms
.collect_md
)
236 rcu_assign_pointer(ip6n
->collect_md_tun
, t
);
237 rcu_assign_pointer(t
->next
, rtnl_dereference(*tp
));
238 rcu_assign_pointer(*tp
, t
);
242 * ip6_tnl_unlink - remove tunnel from hash table
243 * @t: tunnel to be removed
247 ip6_tnl_unlink(struct ip6_tnl_net
*ip6n
, struct ip6_tnl
*t
)
249 struct ip6_tnl __rcu
**tp
;
250 struct ip6_tnl
*iter
;
252 if (t
->parms
.collect_md
)
253 rcu_assign_pointer(ip6n
->collect_md_tun
, NULL
);
255 for (tp
= ip6_tnl_bucket(ip6n
, &t
->parms
);
256 (iter
= rtnl_dereference(*tp
)) != NULL
;
259 rcu_assign_pointer(*tp
, t
->next
);
265 static void ip6_dev_free(struct net_device
*dev
)
267 struct ip6_tnl
*t
= netdev_priv(dev
);
269 gro_cells_destroy(&t
->gro_cells
);
270 dst_cache_destroy(&t
->dst_cache
);
271 free_percpu(dev
->tstats
);
274 static int ip6_tnl_create2(struct net_device
*dev
)
276 struct ip6_tnl
*t
= netdev_priv(dev
);
277 struct net
*net
= dev_net(dev
);
278 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
281 t
= netdev_priv(dev
);
283 dev
->rtnl_link_ops
= &ip6_link_ops
;
284 err
= register_netdevice(dev
);
288 strcpy(t
->parms
.name
, dev
->name
);
291 ip6_tnl_link(ip6n
, t
);
299 * ip6_tnl_create - create a new tunnel
300 * @p: tunnel parameters
301 * @pt: pointer to new tunnel
304 * Create tunnel matching given parameters.
307 * created tunnel or error pointer
310 static struct ip6_tnl
*ip6_tnl_create(struct net
*net
, struct __ip6_tnl_parm
*p
)
312 struct net_device
*dev
;
318 if (!dev_valid_name(p
->name
))
320 strlcpy(name
, p
->name
, IFNAMSIZ
);
322 sprintf(name
, "ip6tnl%%d");
325 dev
= alloc_netdev(sizeof(*t
), name
, NET_NAME_UNKNOWN
,
330 dev_net_set(dev
, net
);
332 t
= netdev_priv(dev
);
334 t
->net
= dev_net(dev
);
335 err
= ip6_tnl_create2(dev
);
348 * ip6_tnl_locate - find or create tunnel matching given parameters
349 * @p: tunnel parameters
350 * @create: != 0 if allowed to create new tunnel if no match found
353 * ip6_tnl_locate() first tries to locate an existing tunnel
354 * based on @parms. If this is unsuccessful, but @create is set a new
355 * tunnel device is created and registered for use.
358 * matching tunnel or error pointer
361 static struct ip6_tnl
*ip6_tnl_locate(struct net
*net
,
362 struct __ip6_tnl_parm
*p
, int create
)
364 const struct in6_addr
*remote
= &p
->raddr
;
365 const struct in6_addr
*local
= &p
->laddr
;
366 struct ip6_tnl __rcu
**tp
;
368 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
370 for (tp
= ip6_tnl_bucket(ip6n
, p
);
371 (t
= rtnl_dereference(*tp
)) != NULL
;
373 if (ipv6_addr_equal(local
, &t
->parms
.laddr
) &&
374 ipv6_addr_equal(remote
, &t
->parms
.raddr
) &&
375 p
->link
== t
->parms
.link
) {
377 return ERR_PTR(-EEXIST
);
383 return ERR_PTR(-ENODEV
);
384 return ip6_tnl_create(net
, p
);
388 * ip6_tnl_dev_uninit - tunnel device uninitializer
389 * @dev: the device to be destroyed
392 * ip6_tnl_dev_uninit() removes tunnel from its list
396 ip6_tnl_dev_uninit(struct net_device
*dev
)
398 struct ip6_tnl
*t
= netdev_priv(dev
);
399 struct net
*net
= t
->net
;
400 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
402 if (dev
== ip6n
->fb_tnl_dev
)
403 RCU_INIT_POINTER(ip6n
->tnls_wc
[0], NULL
);
405 ip6_tnl_unlink(ip6n
, t
);
406 dst_cache_reset(&t
->dst_cache
);
411 * parse_tvl_tnl_enc_lim - handle encapsulation limit option
412 * @skb: received socket buffer
415 * 0 if none was found,
416 * else index to encapsulation limit
419 __u16
ip6_tnl_parse_tlv_enc_lim(struct sk_buff
*skb
, __u8
*raw
)
421 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*)raw
;
422 unsigned int nhoff
= raw
- skb
->data
;
423 unsigned int off
= nhoff
+ sizeof(*ipv6h
);
424 u8 next
, nexthdr
= ipv6h
->nexthdr
;
426 while (ipv6_ext_hdr(nexthdr
) && nexthdr
!= NEXTHDR_NONE
) {
427 struct ipv6_opt_hdr
*hdr
;
430 if (!pskb_may_pull(skb
, off
+ sizeof(*hdr
)))
433 hdr
= (struct ipv6_opt_hdr
*)(skb
->data
+ off
);
434 if (nexthdr
== NEXTHDR_FRAGMENT
) {
435 struct frag_hdr
*frag_hdr
= (struct frag_hdr
*) hdr
;
436 if (frag_hdr
->frag_off
)
439 } else if (nexthdr
== NEXTHDR_AUTH
) {
440 optlen
= ipv6_authlen(hdr
);
442 optlen
= ipv6_optlen(hdr
);
444 /* cache hdr->nexthdr, since pskb_may_pull() might
448 if (nexthdr
== NEXTHDR_DEST
) {
451 /* Remember : hdr is no longer valid at this point. */
452 if (!pskb_may_pull(skb
, off
+ optlen
))
456 struct ipv6_tlv_tnl_enc_lim
*tel
;
458 /* No more room for encapsulation limit */
459 if (i
+ sizeof(*tel
) > optlen
)
462 tel
= (struct ipv6_tlv_tnl_enc_lim
*)(skb
->data
+ off
+ i
);
463 /* return index of option if found and valid */
464 if (tel
->type
== IPV6_TLV_TNL_ENCAP_LIMIT
&&
466 return i
+ off
- nhoff
;
467 /* else jump to next option */
469 i
+= tel
->length
+ 2;
479 EXPORT_SYMBOL(ip6_tnl_parse_tlv_enc_lim
);
482 * ip6_tnl_err - tunnel error handler
485 * ip6_tnl_err() should handle errors in the tunnel according
486 * to the specifications in RFC 2473.
490 ip6_tnl_err(struct sk_buff
*skb
, __u8 ipproto
, struct inet6_skb_parm
*opt
,
491 u8
*type
, u8
*code
, int *msg
, __u32
*info
, int offset
)
493 const struct ipv6hdr
*ipv6h
= (const struct ipv6hdr
*)skb
->data
;
494 struct net
*net
= dev_net(skb
->dev
);
495 u8 rel_type
= ICMPV6_DEST_UNREACH
;
496 u8 rel_code
= ICMPV6_ADDR_UNREACH
;
504 /* If the packet doesn't contain the original IPv6 header we are
505 in trouble since we might need the source address for further
506 processing of the error. */
509 t
= ip6_tnl_lookup(dev_net(skb
->dev
), skb
->dev
->ifindex
, &ipv6h
->daddr
, &ipv6h
->saddr
);
513 tproto
= READ_ONCE(t
->parms
.proto
);
514 if (tproto
!= ipproto
&& tproto
!= 0)
520 case ICMPV6_DEST_UNREACH
:
521 net_dbg_ratelimited("%s: Path to destination invalid or inactive!\n",
525 case ICMPV6_TIME_EXCEED
:
526 if ((*code
) == ICMPV6_EXC_HOPLIMIT
) {
527 net_dbg_ratelimited("%s: Too small hop limit or routing loop in tunnel!\n",
532 case ICMPV6_PARAMPROB
: {
533 struct ipv6_tlv_tnl_enc_lim
*tel
;
537 if ((*code
) == ICMPV6_HDR_FIELD
)
538 teli
= ip6_tnl_parse_tlv_enc_lim(skb
, skb
->data
);
540 if (teli
&& teli
== *info
- 2) {
541 tel
= (struct ipv6_tlv_tnl_enc_lim
*) &skb
->data
[teli
];
542 if (tel
->encap_limit
== 0) {
543 net_dbg_ratelimited("%s: Too small encapsulation limit or routing loop in tunnel!\n",
548 net_dbg_ratelimited("%s: Recipient unable to parse tunneled packet!\n",
553 case ICMPV6_PKT_TOOBIG
: {
556 ip6_update_pmtu(skb
, net
, htonl(*info
), 0, 0,
557 sock_net_uid(net
, NULL
));
558 mtu
= *info
- offset
;
559 if (mtu
< IPV6_MIN_MTU
)
561 len
= sizeof(*ipv6h
) + ntohs(ipv6h
->payload_len
);
563 rel_type
= ICMPV6_PKT_TOOBIG
;
571 ip6_redirect(skb
, net
, skb
->dev
->ifindex
, 0,
572 sock_net_uid(net
, NULL
));
587 ip4ip6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
588 u8 type
, u8 code
, int offset
, __be32 info
)
590 __u32 rel_info
= ntohl(info
);
591 const struct iphdr
*eiph
;
592 struct sk_buff
*skb2
;
593 int err
, rel_msg
= 0;
599 err
= ip6_tnl_err(skb
, IPPROTO_IPIP
, opt
, &rel_type
, &rel_code
,
600 &rel_msg
, &rel_info
, offset
);
608 case ICMPV6_DEST_UNREACH
:
609 if (rel_code
!= ICMPV6_ADDR_UNREACH
)
611 rel_type
= ICMP_DEST_UNREACH
;
612 rel_code
= ICMP_HOST_UNREACH
;
614 case ICMPV6_PKT_TOOBIG
:
617 rel_type
= ICMP_DEST_UNREACH
;
618 rel_code
= ICMP_FRAG_NEEDED
;
624 if (!pskb_may_pull(skb
, offset
+ sizeof(struct iphdr
)))
627 skb2
= skb_clone(skb
, GFP_ATOMIC
);
633 skb_pull(skb2
, offset
);
634 skb_reset_network_header(skb2
);
637 /* Try to guess incoming interface */
638 rt
= ip_route_output_ports(dev_net(skb
->dev
), &fl4
, NULL
, eiph
->saddr
,
639 0, 0, 0, IPPROTO_IPIP
, RT_TOS(eiph
->tos
), 0);
643 skb2
->dev
= rt
->dst
.dev
;
646 /* route "incoming" packet */
647 if (rt
->rt_flags
& RTCF_LOCAL
) {
648 rt
= ip_route_output_ports(dev_net(skb
->dev
), &fl4
, NULL
,
649 eiph
->daddr
, eiph
->saddr
, 0, 0,
650 IPPROTO_IPIP
, RT_TOS(eiph
->tos
), 0);
651 if (IS_ERR(rt
) || rt
->dst
.dev
->type
!= ARPHRD_TUNNEL6
) {
656 skb_dst_set(skb2
, &rt
->dst
);
658 if (ip_route_input(skb2
, eiph
->daddr
, eiph
->saddr
, eiph
->tos
,
660 skb_dst(skb2
)->dev
->type
!= ARPHRD_TUNNEL6
)
664 /* change mtu on this route */
665 if (rel_type
== ICMP_DEST_UNREACH
&& rel_code
== ICMP_FRAG_NEEDED
) {
666 if (rel_info
> dst_mtu(skb_dst(skb2
)))
669 skb_dst_update_pmtu_no_confirm(skb2
, rel_info
);
672 icmp_send(skb2
, rel_type
, rel_code
, htonl(rel_info
));
680 ip6ip6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
681 u8 type
, u8 code
, int offset
, __be32 info
)
683 __u32 rel_info
= ntohl(info
);
684 int err
, rel_msg
= 0;
688 err
= ip6_tnl_err(skb
, IPPROTO_IPV6
, opt
, &rel_type
, &rel_code
,
689 &rel_msg
, &rel_info
, offset
);
693 if (rel_msg
&& pskb_may_pull(skb
, offset
+ sizeof(struct ipv6hdr
))) {
695 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
701 skb_pull(skb2
, offset
);
702 skb_reset_network_header(skb2
);
704 /* Try to guess incoming interface */
705 rt
= rt6_lookup(dev_net(skb
->dev
), &ipv6_hdr(skb2
)->saddr
,
708 if (rt
&& rt
->dst
.dev
)
709 skb2
->dev
= rt
->dst
.dev
;
711 icmpv6_send(skb2
, rel_type
, rel_code
, rel_info
);
721 static int ip4ip6_dscp_ecn_decapsulate(const struct ip6_tnl
*t
,
722 const struct ipv6hdr
*ipv6h
,
725 __u8 dsfield
= ipv6_get_dsfield(ipv6h
) & ~INET_ECN_MASK
;
727 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
728 ipv4_change_dsfield(ip_hdr(skb
), INET_ECN_MASK
, dsfield
);
730 return IP6_ECN_decapsulate(ipv6h
, skb
);
733 static int ip6ip6_dscp_ecn_decapsulate(const struct ip6_tnl
*t
,
734 const struct ipv6hdr
*ipv6h
,
737 if (t
->parms
.flags
& IP6_TNL_F_RCV_DSCP_COPY
)
738 ipv6_copy_dscp(ipv6_get_dsfield(ipv6h
), ipv6_hdr(skb
));
740 return IP6_ECN_decapsulate(ipv6h
, skb
);
743 __u32
ip6_tnl_get_cap(struct ip6_tnl
*t
,
744 const struct in6_addr
*laddr
,
745 const struct in6_addr
*raddr
)
747 struct __ip6_tnl_parm
*p
= &t
->parms
;
748 int ltype
= ipv6_addr_type(laddr
);
749 int rtype
= ipv6_addr_type(raddr
);
752 if (ltype
== IPV6_ADDR_ANY
|| rtype
== IPV6_ADDR_ANY
) {
753 flags
= IP6_TNL_F_CAP_PER_PACKET
;
754 } else if (ltype
& (IPV6_ADDR_UNICAST
|IPV6_ADDR_MULTICAST
) &&
755 rtype
& (IPV6_ADDR_UNICAST
|IPV6_ADDR_MULTICAST
) &&
756 !((ltype
|rtype
) & IPV6_ADDR_LOOPBACK
) &&
757 (!((ltype
|rtype
) & IPV6_ADDR_LINKLOCAL
) || p
->link
)) {
758 if (ltype
&IPV6_ADDR_UNICAST
)
759 flags
|= IP6_TNL_F_CAP_XMIT
;
760 if (rtype
&IPV6_ADDR_UNICAST
)
761 flags
|= IP6_TNL_F_CAP_RCV
;
765 EXPORT_SYMBOL(ip6_tnl_get_cap
);
767 /* called with rcu_read_lock() */
768 int ip6_tnl_rcv_ctl(struct ip6_tnl
*t
,
769 const struct in6_addr
*laddr
,
770 const struct in6_addr
*raddr
)
772 struct __ip6_tnl_parm
*p
= &t
->parms
;
774 struct net
*net
= t
->net
;
776 if ((p
->flags
& IP6_TNL_F_CAP_RCV
) ||
777 ((p
->flags
& IP6_TNL_F_CAP_PER_PACKET
) &&
778 (ip6_tnl_get_cap(t
, laddr
, raddr
) & IP6_TNL_F_CAP_RCV
))) {
779 struct net_device
*ldev
= NULL
;
782 ldev
= dev_get_by_index_rcu(net
, p
->link
);
784 if ((ipv6_addr_is_multicast(laddr
) ||
785 likely(ipv6_chk_addr_and_flags(net
, laddr
, ldev
, false,
786 0, IFA_F_TENTATIVE
))) &&
787 ((p
->flags
& IP6_TNL_F_ALLOW_LOCAL_REMOTE
) ||
788 likely(!ipv6_chk_addr_and_flags(net
, raddr
, ldev
, true,
789 0, IFA_F_TENTATIVE
))))
794 EXPORT_SYMBOL_GPL(ip6_tnl_rcv_ctl
);
796 static int __ip6_tnl_rcv(struct ip6_tnl
*tunnel
, struct sk_buff
*skb
,
797 const struct tnl_ptk_info
*tpi
,
798 struct metadata_dst
*tun_dst
,
799 int (*dscp_ecn_decapsulate
)(const struct ip6_tnl
*t
,
800 const struct ipv6hdr
*ipv6h
,
801 struct sk_buff
*skb
),
804 struct pcpu_sw_netstats
*tstats
;
805 const struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
808 if ((!(tpi
->flags
& TUNNEL_CSUM
) &&
809 (tunnel
->parms
.i_flags
& TUNNEL_CSUM
)) ||
810 ((tpi
->flags
& TUNNEL_CSUM
) &&
811 !(tunnel
->parms
.i_flags
& TUNNEL_CSUM
))) {
812 tunnel
->dev
->stats
.rx_crc_errors
++;
813 tunnel
->dev
->stats
.rx_errors
++;
817 if (tunnel
->parms
.i_flags
& TUNNEL_SEQ
) {
818 if (!(tpi
->flags
& TUNNEL_SEQ
) ||
820 (s32
)(ntohl(tpi
->seq
) - tunnel
->i_seqno
) < 0)) {
821 tunnel
->dev
->stats
.rx_fifo_errors
++;
822 tunnel
->dev
->stats
.rx_errors
++;
825 tunnel
->i_seqno
= ntohl(tpi
->seq
) + 1;
828 skb
->protocol
= tpi
->proto
;
830 /* Warning: All skb pointers will be invalidated! */
831 if (tunnel
->dev
->type
== ARPHRD_ETHER
) {
832 if (!pskb_may_pull(skb
, ETH_HLEN
)) {
833 tunnel
->dev
->stats
.rx_length_errors
++;
834 tunnel
->dev
->stats
.rx_errors
++;
838 ipv6h
= ipv6_hdr(skb
);
839 skb
->protocol
= eth_type_trans(skb
, tunnel
->dev
);
840 skb_postpull_rcsum(skb
, eth_hdr(skb
), ETH_HLEN
);
842 skb
->dev
= tunnel
->dev
;
845 skb_reset_network_header(skb
);
846 memset(skb
->cb
, 0, sizeof(struct inet6_skb_parm
));
848 __skb_tunnel_rx(skb
, tunnel
->dev
, tunnel
->net
);
850 err
= dscp_ecn_decapsulate(tunnel
, ipv6h
, skb
);
853 net_info_ratelimited("non-ECT from %pI6 with DS=%#x\n",
855 ipv6_get_dsfield(ipv6h
));
857 ++tunnel
->dev
->stats
.rx_frame_errors
;
858 ++tunnel
->dev
->stats
.rx_errors
;
863 tstats
= this_cpu_ptr(tunnel
->dev
->tstats
);
864 u64_stats_update_begin(&tstats
->syncp
);
865 tstats
->rx_packets
++;
866 tstats
->rx_bytes
+= skb
->len
;
867 u64_stats_update_end(&tstats
->syncp
);
869 skb_scrub_packet(skb
, !net_eq(tunnel
->net
, dev_net(tunnel
->dev
)));
872 skb_dst_set(skb
, (struct dst_entry
*)tun_dst
);
874 gro_cells_receive(&tunnel
->gro_cells
, skb
);
879 dst_release((struct dst_entry
*)tun_dst
);
884 int ip6_tnl_rcv(struct ip6_tnl
*t
, struct sk_buff
*skb
,
885 const struct tnl_ptk_info
*tpi
,
886 struct metadata_dst
*tun_dst
,
889 return __ip6_tnl_rcv(t
, skb
, tpi
, tun_dst
, ip6ip6_dscp_ecn_decapsulate
,
892 EXPORT_SYMBOL(ip6_tnl_rcv
);
894 static const struct tnl_ptk_info tpi_v6
= {
895 /* no tunnel info required for ipxip6. */
896 .proto
= htons(ETH_P_IPV6
),
899 static const struct tnl_ptk_info tpi_v4
= {
900 /* no tunnel info required for ipxip6. */
901 .proto
= htons(ETH_P_IP
),
904 static int ipxip6_rcv(struct sk_buff
*skb
, u8 ipproto
,
905 const struct tnl_ptk_info
*tpi
,
906 int (*dscp_ecn_decapsulate
)(const struct ip6_tnl
*t
,
907 const struct ipv6hdr
*ipv6h
,
908 struct sk_buff
*skb
))
911 const struct ipv6hdr
*ipv6h
= ipv6_hdr(skb
);
912 struct metadata_dst
*tun_dst
= NULL
;
916 t
= ip6_tnl_lookup(dev_net(skb
->dev
), skb
->dev
->ifindex
, &ipv6h
->saddr
, &ipv6h
->daddr
);
919 u8 tproto
= READ_ONCE(t
->parms
.proto
);
921 if (tproto
!= ipproto
&& tproto
!= 0)
923 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
925 ipv6h
= ipv6_hdr(skb
);
926 if (!ip6_tnl_rcv_ctl(t
, &ipv6h
->daddr
, &ipv6h
->saddr
))
928 if (iptunnel_pull_header(skb
, 0, tpi
->proto
, false))
930 if (t
->parms
.collect_md
) {
931 tun_dst
= ipv6_tun_rx_dst(skb
, 0, 0, 0);
935 ret
= __ip6_tnl_rcv(t
, skb
, tpi
, tun_dst
, dscp_ecn_decapsulate
,
949 static int ip4ip6_rcv(struct sk_buff
*skb
)
951 return ipxip6_rcv(skb
, IPPROTO_IPIP
, &tpi_v4
,
952 ip4ip6_dscp_ecn_decapsulate
);
955 static int ip6ip6_rcv(struct sk_buff
*skb
)
957 return ipxip6_rcv(skb
, IPPROTO_IPV6
, &tpi_v6
,
958 ip6ip6_dscp_ecn_decapsulate
);
961 struct ipv6_tel_txoption
{
962 struct ipv6_txoptions ops
;
966 static void init_tel_txopt(struct ipv6_tel_txoption
*opt
, __u8 encap_limit
)
968 memset(opt
, 0, sizeof(struct ipv6_tel_txoption
));
970 opt
->dst_opt
[2] = IPV6_TLV_TNL_ENCAP_LIMIT
;
972 opt
->dst_opt
[4] = encap_limit
;
973 opt
->dst_opt
[5] = IPV6_TLV_PADN
;
976 opt
->ops
.dst1opt
= (struct ipv6_opt_hdr
*) opt
->dst_opt
;
977 opt
->ops
.opt_nflen
= 8;
981 * ip6_tnl_addr_conflict - compare packet addresses to tunnel's own
982 * @t: the outgoing tunnel device
983 * @hdr: IPv6 header from the incoming packet
986 * Avoid trivial tunneling loop by checking that tunnel exit-point
987 * doesn't match source of incoming packet.
995 ip6_tnl_addr_conflict(const struct ip6_tnl
*t
, const struct ipv6hdr
*hdr
)
997 return ipv6_addr_equal(&t
->parms
.raddr
, &hdr
->saddr
);
1000 int ip6_tnl_xmit_ctl(struct ip6_tnl
*t
,
1001 const struct in6_addr
*laddr
,
1002 const struct in6_addr
*raddr
)
1004 struct __ip6_tnl_parm
*p
= &t
->parms
;
1006 struct net
*net
= t
->net
;
1008 if (t
->parms
.collect_md
)
1011 if ((p
->flags
& IP6_TNL_F_CAP_XMIT
) ||
1012 ((p
->flags
& IP6_TNL_F_CAP_PER_PACKET
) &&
1013 (ip6_tnl_get_cap(t
, laddr
, raddr
) & IP6_TNL_F_CAP_XMIT
))) {
1014 struct net_device
*ldev
= NULL
;
1018 ldev
= dev_get_by_index_rcu(net
, p
->link
);
1020 if (unlikely(!ipv6_chk_addr_and_flags(net
, laddr
, ldev
, false,
1021 0, IFA_F_TENTATIVE
)))
1022 pr_warn("%s xmit: Local address not yet configured!\n",
1024 else if (!(p
->flags
& IP6_TNL_F_ALLOW_LOCAL_REMOTE
) &&
1025 !ipv6_addr_is_multicast(raddr
) &&
1026 unlikely(ipv6_chk_addr_and_flags(net
, raddr
, ldev
,
1027 true, 0, IFA_F_TENTATIVE
)))
1028 pr_warn("%s xmit: Routing loop! Remote address found on this node!\n",
1036 EXPORT_SYMBOL_GPL(ip6_tnl_xmit_ctl
);
1039 * ip6_tnl_xmit - encapsulate packet and send
1040 * @skb: the outgoing socket buffer
1041 * @dev: the outgoing tunnel device
1042 * @dsfield: dscp code for outer header
1043 * @fl6: flow of tunneled packet
1044 * @encap_limit: encapsulation limit
1045 * @pmtu: Path MTU is stored if packet is too big
1046 * @proto: next header value
1049 * Build new header and do some sanity checks on the packet before sending
1055 * %-EMSGSIZE message too big. return mtu in this case.
1058 int ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
, __u8 dsfield
,
1059 struct flowi6
*fl6
, int encap_limit
, __u32
*pmtu
,
1062 struct ip6_tnl
*t
= netdev_priv(dev
);
1063 struct net
*net
= t
->net
;
1064 struct net_device_stats
*stats
= &t
->dev
->stats
;
1065 struct ipv6hdr
*ipv6h
;
1066 struct ipv6_tel_txoption opt
;
1067 struct dst_entry
*dst
= NULL
, *ndst
= NULL
;
1068 struct net_device
*tdev
;
1070 unsigned int eth_hlen
= t
->dev
->type
== ARPHRD_ETHER
? ETH_HLEN
: 0;
1071 unsigned int psh_hlen
= sizeof(struct ipv6hdr
) + t
->encap_hlen
;
1072 unsigned int max_headroom
= psh_hlen
;
1073 bool use_cache
= false;
1077 if (t
->parms
.collect_md
) {
1078 hop_limit
= skb_tunnel_info(skb
)->key
.ttl
;
1081 hop_limit
= t
->parms
.hop_limit
;
1085 if (ipv6_addr_any(&t
->parms
.raddr
)) {
1086 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
1087 struct in6_addr
*addr6
;
1088 struct neighbour
*neigh
;
1092 goto tx_err_link_failure
;
1094 neigh
= dst_neigh_lookup(skb_dst(skb
),
1095 &ipv6_hdr(skb
)->daddr
);
1097 goto tx_err_link_failure
;
1099 addr6
= (struct in6_addr
*)&neigh
->primary_key
;
1100 addr_type
= ipv6_addr_type(addr6
);
1102 if (addr_type
== IPV6_ADDR_ANY
)
1103 addr6
= &ipv6_hdr(skb
)->daddr
;
1105 memcpy(&fl6
->daddr
, addr6
, sizeof(fl6
->daddr
));
1106 neigh_release(neigh
);
1108 } else if (t
->parms
.proto
!= 0 && !(t
->parms
.flags
&
1109 (IP6_TNL_F_USE_ORIG_TCLASS
|
1110 IP6_TNL_F_USE_ORIG_FWMARK
))) {
1111 /* enable the cache only if neither the outer protocol nor the
1112 * routing decision depends on the current inner header value
1118 dst
= dst_cache_get(&t
->dst_cache
);
1120 if (!ip6_tnl_xmit_ctl(t
, &fl6
->saddr
, &fl6
->daddr
))
1121 goto tx_err_link_failure
;
1125 /* add dsfield to flowlabel for route lookup */
1126 fl6
->flowlabel
= ip6_make_flowinfo(dsfield
, fl6
->flowlabel
);
1128 dst
= ip6_route_output(net
, NULL
, fl6
);
1131 goto tx_err_link_failure
;
1132 dst
= xfrm_lookup(net
, dst
, flowi6_to_flowi(fl6
), NULL
, 0);
1136 goto tx_err_link_failure
;
1138 if (t
->parms
.collect_md
&& ipv6_addr_any(&fl6
->saddr
) &&
1139 ipv6_dev_get_saddr(net
, ip6_dst_idev(dst
)->dev
,
1140 &fl6
->daddr
, 0, &fl6
->saddr
))
1141 goto tx_err_link_failure
;
1148 stats
->collisions
++;
1149 net_warn_ratelimited("%s: Local routing loop detected!\n",
1151 goto tx_err_dst_release
;
1153 mtu
= dst_mtu(dst
) - eth_hlen
- psh_hlen
- t
->tun_hlen
;
1154 if (encap_limit
>= 0) {
1158 mtu
= max(mtu
, skb
->protocol
== htons(ETH_P_IPV6
) ?
1159 IPV6_MIN_MTU
: IPV4_MIN_MTU
);
1161 skb_dst_update_pmtu_no_confirm(skb
, mtu
);
1162 if (skb
->len
- t
->tun_hlen
- eth_hlen
> mtu
&& !skb_is_gso(skb
)) {
1165 goto tx_err_dst_release
;
1168 if (t
->err_count
> 0) {
1169 if (time_before(jiffies
,
1170 t
->err_time
+ IP6TUNNEL_ERR_TIMEO
)) {
1173 dst_link_failure(skb
);
1179 skb_scrub_packet(skb
, !net_eq(t
->net
, dev_net(dev
)));
1182 * Okay, now see if we can stuff it in the buffer as-is.
1184 max_headroom
+= LL_RESERVED_SPACE(tdev
);
1186 if (skb_headroom(skb
) < max_headroom
|| skb_shared(skb
) ||
1187 (skb_cloned(skb
) && !skb_clone_writable(skb
, 0))) {
1188 struct sk_buff
*new_skb
;
1190 new_skb
= skb_realloc_headroom(skb
, max_headroom
);
1192 goto tx_err_dst_release
;
1195 skb_set_owner_w(new_skb
, skb
->sk
);
1200 if (t
->parms
.collect_md
) {
1201 if (t
->encap
.type
!= TUNNEL_ENCAP_NONE
)
1202 goto tx_err_dst_release
;
1204 if (use_cache
&& ndst
)
1205 dst_cache_set_ip6(&t
->dst_cache
, ndst
, &fl6
->saddr
);
1207 skb_dst_set(skb
, dst
);
1209 if (hop_limit
== 0) {
1210 if (skb
->protocol
== htons(ETH_P_IP
))
1211 hop_limit
= ip_hdr(skb
)->ttl
;
1212 else if (skb
->protocol
== htons(ETH_P_IPV6
))
1213 hop_limit
= ipv6_hdr(skb
)->hop_limit
;
1215 hop_limit
= ip6_dst_hoplimit(dst
);
1218 /* Calculate max headroom for all the headers and adjust
1219 * needed_headroom if necessary.
1221 max_headroom
= LL_RESERVED_SPACE(dst
->dev
) + sizeof(struct ipv6hdr
)
1222 + dst
->header_len
+ t
->hlen
;
1223 if (max_headroom
> dev
->needed_headroom
)
1224 dev
->needed_headroom
= max_headroom
;
1226 err
= ip6_tnl_encap(skb
, t
, &proto
, fl6
);
1230 if (encap_limit
>= 0) {
1231 init_tel_txopt(&opt
, encap_limit
);
1232 ipv6_push_frag_opts(skb
, &opt
.ops
, &proto
);
1235 skb_push(skb
, sizeof(struct ipv6hdr
));
1236 skb_reset_network_header(skb
);
1237 ipv6h
= ipv6_hdr(skb
);
1238 ip6_flow_hdr(ipv6h
, dsfield
,
1239 ip6_make_flowlabel(net
, skb
, fl6
->flowlabel
, true, fl6
));
1240 ipv6h
->hop_limit
= hop_limit
;
1241 ipv6h
->nexthdr
= proto
;
1242 ipv6h
->saddr
= fl6
->saddr
;
1243 ipv6h
->daddr
= fl6
->daddr
;
1244 ip6tunnel_xmit(NULL
, skb
, dev
);
1246 tx_err_link_failure
:
1247 stats
->tx_carrier_errors
++;
1248 dst_link_failure(skb
);
1253 EXPORT_SYMBOL(ip6_tnl_xmit
);
1256 ip4ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1258 struct ip6_tnl
*t
= netdev_priv(dev
);
1259 const struct iphdr
*iph
;
1260 int encap_limit
= -1;
1268 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1270 tproto
= READ_ONCE(t
->parms
.proto
);
1271 if (tproto
!= IPPROTO_IPIP
&& tproto
!= 0)
1274 if (t
->parms
.collect_md
) {
1275 struct ip_tunnel_info
*tun_info
;
1276 const struct ip_tunnel_key
*key
;
1278 tun_info
= skb_tunnel_info(skb
);
1279 if (unlikely(!tun_info
|| !(tun_info
->mode
& IP_TUNNEL_INFO_TX
) ||
1280 ip_tunnel_info_af(tun_info
) != AF_INET6
))
1282 key
= &tun_info
->key
;
1283 memset(&fl6
, 0, sizeof(fl6
));
1284 fl6
.flowi6_proto
= IPPROTO_IPIP
;
1285 fl6
.saddr
= key
->u
.ipv6
.src
;
1286 fl6
.daddr
= key
->u
.ipv6
.dst
;
1287 fl6
.flowlabel
= key
->label
;
1290 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1291 encap_limit
= t
->parms
.encap_limit
;
1293 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
1294 fl6
.flowi6_proto
= IPPROTO_IPIP
;
1296 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
1297 dsfield
= ipv4_get_dsfield(iph
);
1299 dsfield
= ip6_tclass(t
->parms
.flowinfo
);
1300 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
1301 fl6
.flowi6_mark
= skb
->mark
;
1303 fl6
.flowi6_mark
= t
->parms
.fwmark
;
1306 fl6
.flowi6_uid
= sock_net_uid(dev_net(dev
), NULL
);
1307 dsfield
= INET_ECN_encapsulate(dsfield
, ipv4_get_dsfield(iph
));
1309 if (iptunnel_handle_offloads(skb
, SKB_GSO_IPXIP6
))
1312 skb_set_inner_ipproto(skb
, IPPROTO_IPIP
);
1314 err
= ip6_tnl_xmit(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
,
1317 /* XXX: send ICMP error even if DF is not set. */
1318 if (err
== -EMSGSIZE
)
1319 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
1328 ip6ip6_tnl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1330 struct ip6_tnl
*t
= netdev_priv(dev
);
1331 struct ipv6hdr
*ipv6h
;
1332 int encap_limit
= -1;
1340 ipv6h
= ipv6_hdr(skb
);
1341 tproto
= READ_ONCE(t
->parms
.proto
);
1342 if ((tproto
!= IPPROTO_IPV6
&& tproto
!= 0) ||
1343 ip6_tnl_addr_conflict(t
, ipv6h
))
1346 if (t
->parms
.collect_md
) {
1347 struct ip_tunnel_info
*tun_info
;
1348 const struct ip_tunnel_key
*key
;
1350 tun_info
= skb_tunnel_info(skb
);
1351 if (unlikely(!tun_info
|| !(tun_info
->mode
& IP_TUNNEL_INFO_TX
) ||
1352 ip_tunnel_info_af(tun_info
) != AF_INET6
))
1354 key
= &tun_info
->key
;
1355 memset(&fl6
, 0, sizeof(fl6
));
1356 fl6
.flowi6_proto
= IPPROTO_IPV6
;
1357 fl6
.saddr
= key
->u
.ipv6
.src
;
1358 fl6
.daddr
= key
->u
.ipv6
.dst
;
1359 fl6
.flowlabel
= key
->label
;
1362 offset
= ip6_tnl_parse_tlv_enc_lim(skb
, skb_network_header(skb
));
1363 /* ip6_tnl_parse_tlv_enc_lim() might have reallocated skb->head */
1364 ipv6h
= ipv6_hdr(skb
);
1366 struct ipv6_tlv_tnl_enc_lim
*tel
;
1368 tel
= (void *)&skb_network_header(skb
)[offset
];
1369 if (tel
->encap_limit
== 0) {
1370 icmpv6_send(skb
, ICMPV6_PARAMPROB
,
1371 ICMPV6_HDR_FIELD
, offset
+ 2);
1374 encap_limit
= tel
->encap_limit
- 1;
1375 } else if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
)) {
1376 encap_limit
= t
->parms
.encap_limit
;
1379 memcpy(&fl6
, &t
->fl
.u
.ip6
, sizeof(fl6
));
1380 fl6
.flowi6_proto
= IPPROTO_IPV6
;
1382 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_TCLASS
)
1383 dsfield
= ipv6_get_dsfield(ipv6h
);
1385 dsfield
= ip6_tclass(t
->parms
.flowinfo
);
1386 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FLOWLABEL
)
1387 fl6
.flowlabel
|= ip6_flowlabel(ipv6h
);
1388 if (t
->parms
.flags
& IP6_TNL_F_USE_ORIG_FWMARK
)
1389 fl6
.flowi6_mark
= skb
->mark
;
1391 fl6
.flowi6_mark
= t
->parms
.fwmark
;
1394 fl6
.flowi6_uid
= sock_net_uid(dev_net(dev
), NULL
);
1395 dsfield
= INET_ECN_encapsulate(dsfield
, ipv6_get_dsfield(ipv6h
));
1397 if (iptunnel_handle_offloads(skb
, SKB_GSO_IPXIP6
))
1400 skb_set_inner_ipproto(skb
, IPPROTO_IPV6
);
1402 err
= ip6_tnl_xmit(skb
, dev
, dsfield
, &fl6
, encap_limit
, &mtu
,
1405 if (err
== -EMSGSIZE
)
1406 icmpv6_send(skb
, ICMPV6_PKT_TOOBIG
, 0, mtu
);
1414 ip6_tnl_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1416 struct ip6_tnl
*t
= netdev_priv(dev
);
1417 struct net_device_stats
*stats
= &t
->dev
->stats
;
1420 if (!pskb_inet_may_pull(skb
))
1423 switch (skb
->protocol
) {
1424 case htons(ETH_P_IP
):
1425 ret
= ip4ip6_tnl_xmit(skb
, dev
);
1427 case htons(ETH_P_IPV6
):
1428 ret
= ip6ip6_tnl_xmit(skb
, dev
);
1437 return NETDEV_TX_OK
;
1441 stats
->tx_dropped
++;
1443 return NETDEV_TX_OK
;
1446 static void ip6_tnl_link_config(struct ip6_tnl
*t
)
1448 struct net_device
*dev
= t
->dev
;
1449 struct net_device
*tdev
= NULL
;
1450 struct __ip6_tnl_parm
*p
= &t
->parms
;
1451 struct flowi6
*fl6
= &t
->fl
.u
.ip6
;
1455 memcpy(dev
->dev_addr
, &p
->laddr
, sizeof(struct in6_addr
));
1456 memcpy(dev
->broadcast
, &p
->raddr
, sizeof(struct in6_addr
));
1458 /* Set up flowi template */
1459 fl6
->saddr
= p
->laddr
;
1460 fl6
->daddr
= p
->raddr
;
1461 fl6
->flowi6_oif
= p
->link
;
1464 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_TCLASS
))
1465 fl6
->flowlabel
|= IPV6_TCLASS_MASK
& p
->flowinfo
;
1466 if (!(p
->flags
&IP6_TNL_F_USE_ORIG_FLOWLABEL
))
1467 fl6
->flowlabel
|= IPV6_FLOWLABEL_MASK
& p
->flowinfo
;
1469 p
->flags
&= ~(IP6_TNL_F_CAP_XMIT
|IP6_TNL_F_CAP_RCV
|IP6_TNL_F_CAP_PER_PACKET
);
1470 p
->flags
|= ip6_tnl_get_cap(t
, &p
->laddr
, &p
->raddr
);
1472 if (p
->flags
&IP6_TNL_F_CAP_XMIT
&& p
->flags
&IP6_TNL_F_CAP_RCV
)
1473 dev
->flags
|= IFF_POINTOPOINT
;
1475 dev
->flags
&= ~IFF_POINTOPOINT
;
1478 t
->hlen
= t
->encap_hlen
+ t
->tun_hlen
;
1479 t_hlen
= t
->hlen
+ sizeof(struct ipv6hdr
);
1481 if (p
->flags
& IP6_TNL_F_CAP_XMIT
) {
1482 int strict
= (ipv6_addr_type(&p
->raddr
) &
1483 (IPV6_ADDR_MULTICAST
|IPV6_ADDR_LINKLOCAL
));
1485 struct rt6_info
*rt
= rt6_lookup(t
->net
,
1486 &p
->raddr
, &p
->laddr
,
1487 p
->link
, NULL
, strict
);
1493 if (!tdev
&& p
->link
)
1494 tdev
= __dev_get_by_index(t
->net
, p
->link
);
1497 dev
->hard_header_len
= tdev
->hard_header_len
+ t_hlen
;
1498 mtu
= min_t(unsigned int, tdev
->mtu
, IP6_MAX_MTU
);
1500 dev
->mtu
= mtu
- t_hlen
;
1501 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1504 if (dev
->mtu
< IPV6_MIN_MTU
)
1505 dev
->mtu
= IPV6_MIN_MTU
;
1511 * ip6_tnl_change - update the tunnel parameters
1512 * @t: tunnel to be changed
1513 * @p: tunnel configuration parameters
1516 * ip6_tnl_change() updates the tunnel parameters
1520 ip6_tnl_change(struct ip6_tnl
*t
, const struct __ip6_tnl_parm
*p
)
1522 t
->parms
.laddr
= p
->laddr
;
1523 t
->parms
.raddr
= p
->raddr
;
1524 t
->parms
.flags
= p
->flags
;
1525 t
->parms
.hop_limit
= p
->hop_limit
;
1526 t
->parms
.encap_limit
= p
->encap_limit
;
1527 t
->parms
.flowinfo
= p
->flowinfo
;
1528 t
->parms
.link
= p
->link
;
1529 t
->parms
.proto
= p
->proto
;
1530 t
->parms
.fwmark
= p
->fwmark
;
1531 dst_cache_reset(&t
->dst_cache
);
1532 ip6_tnl_link_config(t
);
1536 static int ip6_tnl_update(struct ip6_tnl
*t
, struct __ip6_tnl_parm
*p
)
1538 struct net
*net
= t
->net
;
1539 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1542 ip6_tnl_unlink(ip6n
, t
);
1544 err
= ip6_tnl_change(t
, p
);
1545 ip6_tnl_link(ip6n
, t
);
1546 netdev_state_change(t
->dev
);
1550 static int ip6_tnl0_update(struct ip6_tnl
*t
, struct __ip6_tnl_parm
*p
)
1552 /* for default tnl0 device allow to change only the proto */
1553 t
->parms
.proto
= p
->proto
;
1554 netdev_state_change(t
->dev
);
1559 ip6_tnl_parm_from_user(struct __ip6_tnl_parm
*p
, const struct ip6_tnl_parm
*u
)
1561 p
->laddr
= u
->laddr
;
1562 p
->raddr
= u
->raddr
;
1563 p
->flags
= u
->flags
;
1564 p
->hop_limit
= u
->hop_limit
;
1565 p
->encap_limit
= u
->encap_limit
;
1566 p
->flowinfo
= u
->flowinfo
;
1568 p
->proto
= u
->proto
;
1569 memcpy(p
->name
, u
->name
, sizeof(u
->name
));
1573 ip6_tnl_parm_to_user(struct ip6_tnl_parm
*u
, const struct __ip6_tnl_parm
*p
)
1575 u
->laddr
= p
->laddr
;
1576 u
->raddr
= p
->raddr
;
1577 u
->flags
= p
->flags
;
1578 u
->hop_limit
= p
->hop_limit
;
1579 u
->encap_limit
= p
->encap_limit
;
1580 u
->flowinfo
= p
->flowinfo
;
1582 u
->proto
= p
->proto
;
1583 memcpy(u
->name
, p
->name
, sizeof(u
->name
));
1587 * ip6_tnl_ioctl - configure ipv6 tunnels from userspace
1588 * @dev: virtual device associated with tunnel
1589 * @ifr: parameters passed from userspace
1590 * @cmd: command to be performed
1593 * ip6_tnl_ioctl() is used for managing IPv6 tunnels
1596 * The possible commands are the following:
1597 * %SIOCGETTUNNEL: get tunnel parameters for device
1598 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
1599 * %SIOCCHGTUNNEL: change tunnel parameters to those given
1600 * %SIOCDELTUNNEL: delete tunnel
1602 * The fallback device "ip6tnl0", created during module
1603 * initialization, can be used for creating other tunnel devices.
1607 * %-EFAULT if unable to copy data to or from userspace,
1608 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
1609 * %-EINVAL if passed tunnel parameters are invalid,
1610 * %-EEXIST if changing a tunnel's parameters would cause a conflict
1611 * %-ENODEV if attempting to change or delete a nonexisting device
1615 ip6_tnl_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1618 struct ip6_tnl_parm p
;
1619 struct __ip6_tnl_parm p1
;
1620 struct ip6_tnl
*t
= netdev_priv(dev
);
1621 struct net
*net
= t
->net
;
1622 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1624 memset(&p1
, 0, sizeof(p1
));
1628 if (dev
== ip6n
->fb_tnl_dev
) {
1629 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
))) {
1633 ip6_tnl_parm_from_user(&p1
, &p
);
1634 t
= ip6_tnl_locate(net
, &p1
, 0);
1636 t
= netdev_priv(dev
);
1638 memset(&p
, 0, sizeof(p
));
1640 ip6_tnl_parm_to_user(&p
, &t
->parms
);
1641 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
))) {
1648 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1651 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1654 if (p
.proto
!= IPPROTO_IPV6
&& p
.proto
!= IPPROTO_IPIP
&&
1657 ip6_tnl_parm_from_user(&p1
, &p
);
1658 t
= ip6_tnl_locate(net
, &p1
, cmd
== SIOCADDTUNNEL
);
1659 if (cmd
== SIOCCHGTUNNEL
) {
1661 if (t
->dev
!= dev
) {
1666 t
= netdev_priv(dev
);
1667 if (dev
== ip6n
->fb_tnl_dev
)
1668 err
= ip6_tnl0_update(t
, &p1
);
1670 err
= ip6_tnl_update(t
, &p1
);
1674 ip6_tnl_parm_to_user(&p
, &t
->parms
);
1675 if (copy_to_user(ifr
->ifr_ifru
.ifru_data
, &p
, sizeof(p
)))
1684 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1687 if (dev
== ip6n
->fb_tnl_dev
) {
1689 if (copy_from_user(&p
, ifr
->ifr_ifru
.ifru_data
, sizeof(p
)))
1692 ip6_tnl_parm_from_user(&p1
, &p
);
1693 t
= ip6_tnl_locate(net
, &p1
, 0);
1697 if (t
->dev
== ip6n
->fb_tnl_dev
)
1702 unregister_netdevice(dev
);
1711 * ip6_tnl_change_mtu - change mtu manually for tunnel device
1712 * @dev: virtual device associated with tunnel
1713 * @new_mtu: the new mtu
1717 * %-EINVAL if mtu too small
1720 int ip6_tnl_change_mtu(struct net_device
*dev
, int new_mtu
)
1722 struct ip6_tnl
*tnl
= netdev_priv(dev
);
1724 if (tnl
->parms
.proto
== IPPROTO_IPV6
) {
1725 if (new_mtu
< IPV6_MIN_MTU
)
1728 if (new_mtu
< ETH_MIN_MTU
)
1731 if (tnl
->parms
.proto
== IPPROTO_IPV6
|| tnl
->parms
.proto
== 0) {
1732 if (new_mtu
> IP6_MAX_MTU
- dev
->hard_header_len
)
1735 if (new_mtu
> IP_MAX_MTU
- dev
->hard_header_len
)
1741 EXPORT_SYMBOL(ip6_tnl_change_mtu
);
1743 int ip6_tnl_get_iflink(const struct net_device
*dev
)
1745 struct ip6_tnl
*t
= netdev_priv(dev
);
1747 return t
->parms
.link
;
1749 EXPORT_SYMBOL(ip6_tnl_get_iflink
);
1751 int ip6_tnl_encap_add_ops(const struct ip6_tnl_encap_ops
*ops
,
1754 if (num
>= MAX_IPTUN_ENCAP_OPS
)
1757 return !cmpxchg((const struct ip6_tnl_encap_ops
**)
1758 &ip6tun_encaps
[num
],
1759 NULL
, ops
) ? 0 : -1;
1761 EXPORT_SYMBOL(ip6_tnl_encap_add_ops
);
1763 int ip6_tnl_encap_del_ops(const struct ip6_tnl_encap_ops
*ops
,
1768 if (num
>= MAX_IPTUN_ENCAP_OPS
)
1771 ret
= (cmpxchg((const struct ip6_tnl_encap_ops
**)
1772 &ip6tun_encaps
[num
],
1773 ops
, NULL
) == ops
) ? 0 : -1;
1779 EXPORT_SYMBOL(ip6_tnl_encap_del_ops
);
1781 int ip6_tnl_encap_setup(struct ip6_tnl
*t
,
1782 struct ip_tunnel_encap
*ipencap
)
1786 memset(&t
->encap
, 0, sizeof(t
->encap
));
1788 hlen
= ip6_encap_hlen(ipencap
);
1792 t
->encap
.type
= ipencap
->type
;
1793 t
->encap
.sport
= ipencap
->sport
;
1794 t
->encap
.dport
= ipencap
->dport
;
1795 t
->encap
.flags
= ipencap
->flags
;
1797 t
->encap_hlen
= hlen
;
1798 t
->hlen
= t
->encap_hlen
+ t
->tun_hlen
;
1802 EXPORT_SYMBOL_GPL(ip6_tnl_encap_setup
);
1804 static const struct net_device_ops ip6_tnl_netdev_ops
= {
1805 .ndo_init
= ip6_tnl_dev_init
,
1806 .ndo_uninit
= ip6_tnl_dev_uninit
,
1807 .ndo_start_xmit
= ip6_tnl_start_xmit
,
1808 .ndo_do_ioctl
= ip6_tnl_ioctl
,
1809 .ndo_change_mtu
= ip6_tnl_change_mtu
,
1810 .ndo_get_stats
= ip6_get_stats
,
1811 .ndo_get_iflink
= ip6_tnl_get_iflink
,
1814 #define IPXIPX_FEATURES (NETIF_F_SG | \
1815 NETIF_F_FRAGLIST | \
1817 NETIF_F_GSO_SOFTWARE | \
1821 * ip6_tnl_dev_setup - setup virtual tunnel device
1822 * @dev: virtual device associated with tunnel
1825 * Initialize function pointers and device parameters
1828 static void ip6_tnl_dev_setup(struct net_device
*dev
)
1830 dev
->netdev_ops
= &ip6_tnl_netdev_ops
;
1831 dev
->needs_free_netdev
= true;
1832 dev
->priv_destructor
= ip6_dev_free
;
1834 dev
->type
= ARPHRD_TUNNEL6
;
1835 dev
->flags
|= IFF_NOARP
;
1836 dev
->addr_len
= sizeof(struct in6_addr
);
1837 dev
->features
|= NETIF_F_LLTX
;
1838 netif_keep_dst(dev
);
1840 dev
->features
|= IPXIPX_FEATURES
;
1841 dev
->hw_features
|= IPXIPX_FEATURES
;
1843 /* This perm addr will be used as interface identifier by IPv6 */
1844 dev
->addr_assign_type
= NET_ADDR_RANDOM
;
1845 eth_random_addr(dev
->perm_addr
);
1850 * ip6_tnl_dev_init_gen - general initializer for all tunnel devices
1851 * @dev: virtual device associated with tunnel
1855 ip6_tnl_dev_init_gen(struct net_device
*dev
)
1857 struct ip6_tnl
*t
= netdev_priv(dev
);
1862 t
->net
= dev_net(dev
);
1863 dev
->tstats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
1867 ret
= dst_cache_init(&t
->dst_cache
, GFP_KERNEL
);
1871 ret
= gro_cells_init(&t
->gro_cells
, dev
);
1876 t
->hlen
= t
->encap_hlen
+ t
->tun_hlen
;
1877 t_hlen
= t
->hlen
+ sizeof(struct ipv6hdr
);
1879 dev
->type
= ARPHRD_TUNNEL6
;
1880 dev
->hard_header_len
= LL_MAX_HEADER
+ t_hlen
;
1881 dev
->mtu
= ETH_DATA_LEN
- t_hlen
;
1882 if (!(t
->parms
.flags
& IP6_TNL_F_IGN_ENCAP_LIMIT
))
1884 dev
->min_mtu
= ETH_MIN_MTU
;
1885 dev
->max_mtu
= IP6_MAX_MTU
- dev
->hard_header_len
;
1890 dst_cache_destroy(&t
->dst_cache
);
1892 free_percpu(dev
->tstats
);
1899 * ip6_tnl_dev_init - initializer for all non fallback tunnel devices
1900 * @dev: virtual device associated with tunnel
1903 static int ip6_tnl_dev_init(struct net_device
*dev
)
1905 struct ip6_tnl
*t
= netdev_priv(dev
);
1906 int err
= ip6_tnl_dev_init_gen(dev
);
1910 ip6_tnl_link_config(t
);
1911 if (t
->parms
.collect_md
)
1912 netif_keep_dst(dev
);
1917 * ip6_fb_tnl_dev_init - initializer for fallback tunnel device
1918 * @dev: fallback device
1923 static int __net_init
ip6_fb_tnl_dev_init(struct net_device
*dev
)
1925 struct ip6_tnl
*t
= netdev_priv(dev
);
1926 struct net
*net
= dev_net(dev
);
1927 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
1929 t
->parms
.proto
= IPPROTO_IPV6
;
1932 rcu_assign_pointer(ip6n
->tnls_wc
[0], t
);
1936 static int ip6_tnl_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
1937 struct netlink_ext_ack
*extack
)
1941 if (!data
|| !data
[IFLA_IPTUN_PROTO
])
1944 proto
= nla_get_u8(data
[IFLA_IPTUN_PROTO
]);
1945 if (proto
!= IPPROTO_IPV6
&&
1946 proto
!= IPPROTO_IPIP
&&
1953 static void ip6_tnl_netlink_parms(struct nlattr
*data
[],
1954 struct __ip6_tnl_parm
*parms
)
1956 memset(parms
, 0, sizeof(*parms
));
1961 if (data
[IFLA_IPTUN_LINK
])
1962 parms
->link
= nla_get_u32(data
[IFLA_IPTUN_LINK
]);
1964 if (data
[IFLA_IPTUN_LOCAL
])
1965 parms
->laddr
= nla_get_in6_addr(data
[IFLA_IPTUN_LOCAL
]);
1967 if (data
[IFLA_IPTUN_REMOTE
])
1968 parms
->raddr
= nla_get_in6_addr(data
[IFLA_IPTUN_REMOTE
]);
1970 if (data
[IFLA_IPTUN_TTL
])
1971 parms
->hop_limit
= nla_get_u8(data
[IFLA_IPTUN_TTL
]);
1973 if (data
[IFLA_IPTUN_ENCAP_LIMIT
])
1974 parms
->encap_limit
= nla_get_u8(data
[IFLA_IPTUN_ENCAP_LIMIT
]);
1976 if (data
[IFLA_IPTUN_FLOWINFO
])
1977 parms
->flowinfo
= nla_get_be32(data
[IFLA_IPTUN_FLOWINFO
]);
1979 if (data
[IFLA_IPTUN_FLAGS
])
1980 parms
->flags
= nla_get_u32(data
[IFLA_IPTUN_FLAGS
]);
1982 if (data
[IFLA_IPTUN_PROTO
])
1983 parms
->proto
= nla_get_u8(data
[IFLA_IPTUN_PROTO
]);
1985 if (data
[IFLA_IPTUN_COLLECT_METADATA
])
1986 parms
->collect_md
= true;
1988 if (data
[IFLA_IPTUN_FWMARK
])
1989 parms
->fwmark
= nla_get_u32(data
[IFLA_IPTUN_FWMARK
]);
1992 static bool ip6_tnl_netlink_encap_parms(struct nlattr
*data
[],
1993 struct ip_tunnel_encap
*ipencap
)
1997 memset(ipencap
, 0, sizeof(*ipencap
));
2002 if (data
[IFLA_IPTUN_ENCAP_TYPE
]) {
2004 ipencap
->type
= nla_get_u16(data
[IFLA_IPTUN_ENCAP_TYPE
]);
2007 if (data
[IFLA_IPTUN_ENCAP_FLAGS
]) {
2009 ipencap
->flags
= nla_get_u16(data
[IFLA_IPTUN_ENCAP_FLAGS
]);
2012 if (data
[IFLA_IPTUN_ENCAP_SPORT
]) {
2014 ipencap
->sport
= nla_get_be16(data
[IFLA_IPTUN_ENCAP_SPORT
]);
2017 if (data
[IFLA_IPTUN_ENCAP_DPORT
]) {
2019 ipencap
->dport
= nla_get_be16(data
[IFLA_IPTUN_ENCAP_DPORT
]);
2025 static int ip6_tnl_newlink(struct net
*src_net
, struct net_device
*dev
,
2026 struct nlattr
*tb
[], struct nlattr
*data
[],
2027 struct netlink_ext_ack
*extack
)
2029 struct net
*net
= dev_net(dev
);
2030 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
2031 struct ip_tunnel_encap ipencap
;
2032 struct ip6_tnl
*nt
, *t
;
2035 nt
= netdev_priv(dev
);
2037 if (ip6_tnl_netlink_encap_parms(data
, &ipencap
)) {
2038 err
= ip6_tnl_encap_setup(nt
, &ipencap
);
2043 ip6_tnl_netlink_parms(data
, &nt
->parms
);
2045 if (nt
->parms
.collect_md
) {
2046 if (rtnl_dereference(ip6n
->collect_md_tun
))
2049 t
= ip6_tnl_locate(net
, &nt
->parms
, 0);
2054 err
= ip6_tnl_create2(dev
);
2055 if (!err
&& tb
[IFLA_MTU
])
2056 ip6_tnl_change_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
2061 static int ip6_tnl_changelink(struct net_device
*dev
, struct nlattr
*tb
[],
2062 struct nlattr
*data
[],
2063 struct netlink_ext_ack
*extack
)
2065 struct ip6_tnl
*t
= netdev_priv(dev
);
2066 struct __ip6_tnl_parm p
;
2067 struct net
*net
= t
->net
;
2068 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
2069 struct ip_tunnel_encap ipencap
;
2071 if (dev
== ip6n
->fb_tnl_dev
)
2074 if (ip6_tnl_netlink_encap_parms(data
, &ipencap
)) {
2075 int err
= ip6_tnl_encap_setup(t
, &ipencap
);
2080 ip6_tnl_netlink_parms(data
, &p
);
2084 t
= ip6_tnl_locate(net
, &p
, 0);
2089 t
= netdev_priv(dev
);
2091 return ip6_tnl_update(t
, &p
);
2094 static void ip6_tnl_dellink(struct net_device
*dev
, struct list_head
*head
)
2096 struct net
*net
= dev_net(dev
);
2097 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
2099 if (dev
!= ip6n
->fb_tnl_dev
)
2100 unregister_netdevice_queue(dev
, head
);
2103 static size_t ip6_tnl_get_size(const struct net_device
*dev
)
2106 /* IFLA_IPTUN_LINK */
2108 /* IFLA_IPTUN_LOCAL */
2109 nla_total_size(sizeof(struct in6_addr
)) +
2110 /* IFLA_IPTUN_REMOTE */
2111 nla_total_size(sizeof(struct in6_addr
)) +
2112 /* IFLA_IPTUN_TTL */
2114 /* IFLA_IPTUN_ENCAP_LIMIT */
2116 /* IFLA_IPTUN_FLOWINFO */
2118 /* IFLA_IPTUN_FLAGS */
2120 /* IFLA_IPTUN_PROTO */
2122 /* IFLA_IPTUN_ENCAP_TYPE */
2124 /* IFLA_IPTUN_ENCAP_FLAGS */
2126 /* IFLA_IPTUN_ENCAP_SPORT */
2128 /* IFLA_IPTUN_ENCAP_DPORT */
2130 /* IFLA_IPTUN_COLLECT_METADATA */
2132 /* IFLA_IPTUN_FWMARK */
2137 static int ip6_tnl_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
2139 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
2140 struct __ip6_tnl_parm
*parm
= &tunnel
->parms
;
2142 if (nla_put_u32(skb
, IFLA_IPTUN_LINK
, parm
->link
) ||
2143 nla_put_in6_addr(skb
, IFLA_IPTUN_LOCAL
, &parm
->laddr
) ||
2144 nla_put_in6_addr(skb
, IFLA_IPTUN_REMOTE
, &parm
->raddr
) ||
2145 nla_put_u8(skb
, IFLA_IPTUN_TTL
, parm
->hop_limit
) ||
2146 nla_put_u8(skb
, IFLA_IPTUN_ENCAP_LIMIT
, parm
->encap_limit
) ||
2147 nla_put_be32(skb
, IFLA_IPTUN_FLOWINFO
, parm
->flowinfo
) ||
2148 nla_put_u32(skb
, IFLA_IPTUN_FLAGS
, parm
->flags
) ||
2149 nla_put_u8(skb
, IFLA_IPTUN_PROTO
, parm
->proto
) ||
2150 nla_put_u32(skb
, IFLA_IPTUN_FWMARK
, parm
->fwmark
))
2151 goto nla_put_failure
;
2153 if (nla_put_u16(skb
, IFLA_IPTUN_ENCAP_TYPE
, tunnel
->encap
.type
) ||
2154 nla_put_be16(skb
, IFLA_IPTUN_ENCAP_SPORT
, tunnel
->encap
.sport
) ||
2155 nla_put_be16(skb
, IFLA_IPTUN_ENCAP_DPORT
, tunnel
->encap
.dport
) ||
2156 nla_put_u16(skb
, IFLA_IPTUN_ENCAP_FLAGS
, tunnel
->encap
.flags
))
2157 goto nla_put_failure
;
2159 if (parm
->collect_md
)
2160 if (nla_put_flag(skb
, IFLA_IPTUN_COLLECT_METADATA
))
2161 goto nla_put_failure
;
2169 struct net
*ip6_tnl_get_link_net(const struct net_device
*dev
)
2171 struct ip6_tnl
*tunnel
= netdev_priv(dev
);
2175 EXPORT_SYMBOL(ip6_tnl_get_link_net
);
2177 static const struct nla_policy ip6_tnl_policy
[IFLA_IPTUN_MAX
+ 1] = {
2178 [IFLA_IPTUN_LINK
] = { .type
= NLA_U32
},
2179 [IFLA_IPTUN_LOCAL
] = { .len
= sizeof(struct in6_addr
) },
2180 [IFLA_IPTUN_REMOTE
] = { .len
= sizeof(struct in6_addr
) },
2181 [IFLA_IPTUN_TTL
] = { .type
= NLA_U8
},
2182 [IFLA_IPTUN_ENCAP_LIMIT
] = { .type
= NLA_U8
},
2183 [IFLA_IPTUN_FLOWINFO
] = { .type
= NLA_U32
},
2184 [IFLA_IPTUN_FLAGS
] = { .type
= NLA_U32
},
2185 [IFLA_IPTUN_PROTO
] = { .type
= NLA_U8
},
2186 [IFLA_IPTUN_ENCAP_TYPE
] = { .type
= NLA_U16
},
2187 [IFLA_IPTUN_ENCAP_FLAGS
] = { .type
= NLA_U16
},
2188 [IFLA_IPTUN_ENCAP_SPORT
] = { .type
= NLA_U16
},
2189 [IFLA_IPTUN_ENCAP_DPORT
] = { .type
= NLA_U16
},
2190 [IFLA_IPTUN_COLLECT_METADATA
] = { .type
= NLA_FLAG
},
2191 [IFLA_IPTUN_FWMARK
] = { .type
= NLA_U32
},
2194 static struct rtnl_link_ops ip6_link_ops __read_mostly
= {
2196 .maxtype
= IFLA_IPTUN_MAX
,
2197 .policy
= ip6_tnl_policy
,
2198 .priv_size
= sizeof(struct ip6_tnl
),
2199 .setup
= ip6_tnl_dev_setup
,
2200 .validate
= ip6_tnl_validate
,
2201 .newlink
= ip6_tnl_newlink
,
2202 .changelink
= ip6_tnl_changelink
,
2203 .dellink
= ip6_tnl_dellink
,
2204 .get_size
= ip6_tnl_get_size
,
2205 .fill_info
= ip6_tnl_fill_info
,
2206 .get_link_net
= ip6_tnl_get_link_net
,
2209 static struct xfrm6_tunnel ip4ip6_handler __read_mostly
= {
2210 .handler
= ip4ip6_rcv
,
2211 .err_handler
= ip4ip6_err
,
2215 static struct xfrm6_tunnel ip6ip6_handler __read_mostly
= {
2216 .handler
= ip6ip6_rcv
,
2217 .err_handler
= ip6ip6_err
,
2221 static void __net_exit
ip6_tnl_destroy_tunnels(struct net
*net
, struct list_head
*list
)
2223 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
2224 struct net_device
*dev
, *aux
;
2228 for_each_netdev_safe(net
, dev
, aux
)
2229 if (dev
->rtnl_link_ops
== &ip6_link_ops
)
2230 unregister_netdevice_queue(dev
, list
);
2232 for (h
= 0; h
< IP6_TUNNEL_HASH_SIZE
; h
++) {
2233 t
= rtnl_dereference(ip6n
->tnls_r_l
[h
]);
2235 /* If dev is in the same netns, it has already
2236 * been added to the list by the previous loop.
2238 if (!net_eq(dev_net(t
->dev
), net
))
2239 unregister_netdevice_queue(t
->dev
, list
);
2240 t
= rtnl_dereference(t
->next
);
2245 static int __net_init
ip6_tnl_init_net(struct net
*net
)
2247 struct ip6_tnl_net
*ip6n
= net_generic(net
, ip6_tnl_net_id
);
2248 struct ip6_tnl
*t
= NULL
;
2251 ip6n
->tnls
[0] = ip6n
->tnls_wc
;
2252 ip6n
->tnls
[1] = ip6n
->tnls_r_l
;
2254 if (!net_has_fallback_tunnels(net
))
2257 ip6n
->fb_tnl_dev
= alloc_netdev(sizeof(struct ip6_tnl
), "ip6tnl0",
2258 NET_NAME_UNKNOWN
, ip6_tnl_dev_setup
);
2260 if (!ip6n
->fb_tnl_dev
)
2262 dev_net_set(ip6n
->fb_tnl_dev
, net
);
2263 ip6n
->fb_tnl_dev
->rtnl_link_ops
= &ip6_link_ops
;
2264 /* FB netdevice is special: we have one, and only one per netns.
2265 * Allowing to move it to another netns is clearly unsafe.
2267 ip6n
->fb_tnl_dev
->features
|= NETIF_F_NETNS_LOCAL
;
2269 err
= ip6_fb_tnl_dev_init(ip6n
->fb_tnl_dev
);
2273 err
= register_netdev(ip6n
->fb_tnl_dev
);
2277 t
= netdev_priv(ip6n
->fb_tnl_dev
);
2279 strcpy(t
->parms
.name
, ip6n
->fb_tnl_dev
->name
);
2283 free_netdev(ip6n
->fb_tnl_dev
);
2288 static void __net_exit
ip6_tnl_exit_batch_net(struct list_head
*net_list
)
2294 list_for_each_entry(net
, net_list
, exit_list
)
2295 ip6_tnl_destroy_tunnels(net
, &list
);
2296 unregister_netdevice_many(&list
);
2300 static struct pernet_operations ip6_tnl_net_ops
= {
2301 .init
= ip6_tnl_init_net
,
2302 .exit_batch
= ip6_tnl_exit_batch_net
,
2303 .id
= &ip6_tnl_net_id
,
2304 .size
= sizeof(struct ip6_tnl_net
),
2308 * ip6_tunnel_init - register protocol and reserve needed resources
2310 * Return: 0 on success
2313 static int __init
ip6_tunnel_init(void)
2317 if (!ipv6_mod_enabled())
2320 err
= register_pernet_device(&ip6_tnl_net_ops
);
2324 err
= xfrm6_tunnel_register(&ip4ip6_handler
, AF_INET
);
2326 pr_err("%s: can't register ip4ip6\n", __func__
);
2330 err
= xfrm6_tunnel_register(&ip6ip6_handler
, AF_INET6
);
2332 pr_err("%s: can't register ip6ip6\n", __func__
);
2335 err
= rtnl_link_register(&ip6_link_ops
);
2337 goto rtnl_link_failed
;
2342 xfrm6_tunnel_deregister(&ip6ip6_handler
, AF_INET6
);
2344 xfrm6_tunnel_deregister(&ip4ip6_handler
, AF_INET
);
2346 unregister_pernet_device(&ip6_tnl_net_ops
);
2352 * ip6_tunnel_cleanup - free resources and unregister protocol
2355 static void __exit
ip6_tunnel_cleanup(void)
2357 rtnl_link_unregister(&ip6_link_ops
);
2358 if (xfrm6_tunnel_deregister(&ip4ip6_handler
, AF_INET
))
2359 pr_info("%s: can't deregister ip4ip6\n", __func__
);
2361 if (xfrm6_tunnel_deregister(&ip6ip6_handler
, AF_INET6
))
2362 pr_info("%s: can't deregister ip6ip6\n", __func__
);
2364 unregister_pernet_device(&ip6_tnl_net_ops
);
2367 module_init(ip6_tunnel_init
);
2368 module_exit(ip6_tunnel_cleanup
);