__udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
 }
 
+static struct static_key udpv6_encap_needed __read_mostly;
+void udpv6_encap_enable(void)
+{
+       if (!static_key_enabled(&udpv6_encap_needed))
+               static_key_slow_inc(&udpv6_encap_needed);
+}
+EXPORT_SYMBOL(udpv6_encap_enable);
+
 int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
 {
        struct udp_sock *up = udp_sk(sk);
        if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
                goto drop;
 
+       if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
+               int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
+
+               /*
+                * This is an encapsulation socket so pass the skb to
+                * the socket's udp_encap_rcv() hook. Otherwise, just
+                * fall through and pass this up the UDP socket.
+                * up->encap_rcv() returns the following value:
+                * =0 if skb was successfully passed to the encap
+                *    handler or was discarded by it.
+                * >0 if skb should be passed on to UDP.
+                * <0 if skb should be resubmitted as proto -N
+                */
+
+               /* if we're overly short, let UDP handle it */
+               encap_rcv = ACCESS_ONCE(up->encap_rcv);
+               if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) {
+                       int ret;
+
+                       ret = encap_rcv(sk, skb);
+                       if (ret <= 0) {
+                               UDP_INC_STATS_BH(sock_net(sk),
+                                                UDP_MIB_INDATAGRAMS,
+                                                is_udplite);
+                               return -ret;
+                       }
+               }
+
+               /* FALLTHROUGH -- it's a UDP Packet */
+       }
+
        /*
         * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
         */