Merge branch 'for-arm' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/signal...
[firefly-linux-kernel-4.4.55.git] / net / l2tp / l2tp_core.c
index 89ff8c67943e8af26efe1bac80b306e297921ebd..32b2155e7ab496e93fe5c2b7396a94c8f7bcd09b 100644 (file)
@@ -18,6 +18,8 @@
  * published by the Free Software Foundation.
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
 #include <linux/module.h>
 #include <linux/string.h>
 #include <linux/list.h>
 #include <net/inet_common.h>
 #include <net/xfrm.h>
 #include <net/protocol.h>
+#include <net/inet6_connection_sock.h>
+#include <net/inet_ecn.h>
+#include <net/ip6_route.h>
+#include <net/ip6_checksum.h>
 
 #include <asm/byteorder.h>
 #include <linux/atomic.h>
 /* Default trace flags */
 #define L2TP_DEFAULT_DEBUG_FLAGS       0
 
-#define PRINTK(_mask, _type, _lvl, _fmt, args...)                      \
-       do {                                                            \
-               if ((_mask) & (_type))                                  \
-                       printk(_lvl "L2TP: " _fmt, ##args);             \
-       } while (0)
-
 /* Private data stored for received packets in the skb.
  */
 struct l2tp_skb_cb {
@@ -137,14 +137,20 @@ static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
                l2tp_tunnel_free(tunnel);
 }
 #ifdef L2TP_REFCNT_DEBUG
-#define l2tp_tunnel_inc_refcount(_t) do { \
-               printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
-               l2tp_tunnel_inc_refcount_1(_t);                         \
-       } while (0)
-#define l2tp_tunnel_dec_refcount(_t) do { \
-               printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
-               l2tp_tunnel_dec_refcount_1(_t);                         \
-       } while (0)
+#define l2tp_tunnel_inc_refcount(_t)                                   \
+do {                                                                   \
+       pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n",        \
+                __func__, __LINE__, (_t)->name,                        \
+                atomic_read(&_t->ref_count));                          \
+       l2tp_tunnel_inc_refcount_1(_t);                                 \
+} while (0)
+#define l2tp_tunnel_dec_refcount(_t)
+do {                                                                   \
+       pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n",        \
+                __func__, __LINE__, (_t)->name,                        \
+                atomic_read(&_t->ref_count));                          \
+       l2tp_tunnel_dec_refcount_1(_t);                                 \
+} while (0)
 #else
 #define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
 #define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
@@ -326,16 +332,20 @@ static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *sk
        struct sk_buff *skbp;
        struct sk_buff *tmp;
        u32 ns = L2TP_SKB_CB(skb)->ns;
+       struct l2tp_stats *sstats;
 
        spin_lock_bh(&session->reorder_q.lock);
+       sstats = &session->stats;
        skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
                if (L2TP_SKB_CB(skbp)->ns > ns) {
                        __skb_queue_before(&session->reorder_q, skbp, skb);
-                       PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
-                              "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
-                              session->name, ns, L2TP_SKB_CB(skbp)->ns,
-                              skb_queue_len(&session->reorder_q));
-                       session->stats.rx_oos_packets++;
+                       l2tp_dbg(session, L2TP_MSG_SEQ,
+                                "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
+                                session->name, ns, L2TP_SKB_CB(skbp)->ns,
+                                skb_queue_len(&session->reorder_q));
+                       u64_stats_update_begin(&sstats->syncp);
+                       sstats->rx_oos_packets++;
+                       u64_stats_update_end(&sstats->syncp);
                        goto out;
                }
        }
@@ -352,16 +362,23 @@ static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *
 {
        struct l2tp_tunnel *tunnel = session->tunnel;
        int length = L2TP_SKB_CB(skb)->length;
+       struct l2tp_stats *tstats, *sstats;
 
        /* We're about to requeue the skb, so return resources
         * to its current owner (a socket receive buffer).
         */
        skb_orphan(skb);
 
-       tunnel->stats.rx_packets++;
-       tunnel->stats.rx_bytes += length;
-       session->stats.rx_packets++;
-       session->stats.rx_bytes += length;
+       tstats = &tunnel->stats;
+       u64_stats_update_begin(&tstats->syncp);
+       sstats = &session->stats;
+       u64_stats_update_begin(&sstats->syncp);
+       tstats->rx_packets++;
+       tstats->rx_bytes += length;
+       sstats->rx_packets++;
+       sstats->rx_bytes += length;
+       u64_stats_update_end(&tstats->syncp);
+       u64_stats_update_end(&sstats->syncp);
 
        if (L2TP_SKB_CB(skb)->has_seq) {
                /* Bump our Nr */
@@ -371,8 +388,8 @@ static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *
                else
                        session->nr &= 0xffffff;
 
-               PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
-                      "%s: updated nr to %hu\n", session->name, session->nr);
+               l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n",
+                        session->name, session->nr);
        }
 
        /* call private receive handler */
@@ -392,6 +409,7 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
 {
        struct sk_buff *skb;
        struct sk_buff *tmp;
+       struct l2tp_stats *sstats;
 
        /* If the pkt at the head of the queue has the nr that we
         * expect to send up next, dequeue it and any other
@@ -399,16 +417,19 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
         */
 start:
        spin_lock_bh(&session->reorder_q.lock);
+       sstats = &session->stats;
        skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
                if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
-                       session->stats.rx_seq_discards++;
-                       session->stats.rx_errors++;
-                       PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
-                              "%s: oos pkt %u len %d discarded (too old), "
-                              "waiting for %u, reorder_q_len=%d\n",
-                              session->name, L2TP_SKB_CB(skb)->ns,
-                              L2TP_SKB_CB(skb)->length, session->nr,
-                              skb_queue_len(&session->reorder_q));
+                       u64_stats_update_begin(&sstats->syncp);
+                       sstats->rx_seq_discards++;
+                       sstats->rx_errors++;
+                       u64_stats_update_end(&sstats->syncp);
+                       l2tp_dbg(session, L2TP_MSG_SEQ,
+                                "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n",
+                                session->name, L2TP_SKB_CB(skb)->ns,
+                                L2TP_SKB_CB(skb)->length, session->nr,
+                                skb_queue_len(&session->reorder_q));
+                       session->reorder_skip = 1;
                        __skb_unlink(skb, &session->reorder_q);
                        kfree_skb(skb);
                        if (session->deref)
@@ -417,13 +438,20 @@ start:
                }
 
                if (L2TP_SKB_CB(skb)->has_seq) {
+                       if (session->reorder_skip) {
+                               l2tp_dbg(session, L2TP_MSG_SEQ,
+                                        "%s: advancing nr to next pkt: %u -> %u",
+                                        session->name, session->nr,
+                                        L2TP_SKB_CB(skb)->ns);
+                               session->reorder_skip = 0;
+                               session->nr = L2TP_SKB_CB(skb)->ns;
+                       }
                        if (L2TP_SKB_CB(skb)->ns != session->nr) {
-                               PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
-                                      "%s: holding oos pkt %u len %d, "
-                                      "waiting for %u, reorder_q_len=%d\n",
-                                      session->name, L2TP_SKB_CB(skb)->ns,
-                                      L2TP_SKB_CB(skb)->length, session->nr,
-                                      skb_queue_len(&session->reorder_q));
+                               l2tp_dbg(session, L2TP_MSG_SEQ,
+                                        "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n",
+                                        session->name, L2TP_SKB_CB(skb)->ns,
+                                        L2TP_SKB_CB(skb)->length, session->nr,
+                                        skb_queue_len(&session->reorder_q));
                                goto out;
                        }
                }
@@ -446,21 +474,43 @@ static inline int l2tp_verify_udp_checksum(struct sock *sk,
 {
        struct udphdr *uh = udp_hdr(skb);
        u16 ulen = ntohs(uh->len);
-       struct inet_sock *inet;
        __wsum psum;
 
-       if (sk->sk_no_check || skb_csum_unnecessary(skb) || !uh->check)
+       if (sk->sk_no_check || skb_csum_unnecessary(skb))
                return 0;
 
-       inet = inet_sk(sk);
-       psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, ulen,
-                                 IPPROTO_UDP, 0);
-
-       if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
-           !csum_fold(csum_add(psum, skb->csum)))
-               return 0;
-
-       skb->csum = psum;
+#if IS_ENABLED(CONFIG_IPV6)
+       if (sk->sk_family == PF_INET6) {
+               if (!uh->check) {
+                       LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n");
+                       return 1;
+               }
+               if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
+                   !csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
+                                    &ipv6_hdr(skb)->daddr, ulen,
+                                    IPPROTO_UDP, skb->csum)) {
+                       skb->ip_summed = CHECKSUM_UNNECESSARY;
+                       return 0;
+               }
+               skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
+                                                        &ipv6_hdr(skb)->daddr,
+                                                        skb->len, IPPROTO_UDP,
+                                                        0));
+       } else
+#endif
+       {
+               struct inet_sock *inet;
+               if (!uh->check)
+                       return 0;
+               inet = inet_sk(sk);
+               psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
+                                         ulen, IPPROTO_UDP, 0);
+
+               if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
+                   !csum_fold(csum_add(psum, skb->csum)))
+                       return 0;
+               skb->csum = psum;
+       }
 
        return __skb_checksum_complete(skb);
 }
@@ -532,6 +582,7 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
        struct l2tp_tunnel *tunnel = session->tunnel;
        int offset;
        u32 ns, nr;
+       struct l2tp_stats *sstats = &session->stats;
 
        /* The ref count is increased since we now hold a pointer to
         * the session. Take care to decrement the refcnt when exiting
@@ -544,10 +595,13 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
        /* Parse and check optional cookie */
        if (session->peer_cookie_len > 0) {
                if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
-                       PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
-                              "%s: cookie mismatch (%u/%u). Discarding.\n",
-                              tunnel->name, tunnel->tunnel_id, session->session_id);
-                       session->stats.rx_cookie_discards++;
+                       l2tp_info(tunnel, L2TP_MSG_DATA,
+                                 "%s: cookie mismatch (%u/%u). Discarding.\n",
+                                 tunnel->name, tunnel->tunnel_id,
+                                 session->session_id);
+                       u64_stats_update_begin(&sstats->syncp);
+                       sstats->rx_cookie_discards++;
+                       u64_stats_update_end(&sstats->syncp);
                        goto discard;
                }
                ptr += session->peer_cookie_len;
@@ -573,9 +627,9 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
                        L2TP_SKB_CB(skb)->ns = ns;
                        L2TP_SKB_CB(skb)->has_seq = 1;
 
-                       PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
-                              "%s: recv data ns=%u, nr=%u, session nr=%u\n",
-                              session->name, ns, nr, session->nr);
+                       l2tp_dbg(session, L2TP_MSG_SEQ,
+                                "%s: recv data ns=%u, nr=%u, session nr=%u\n",
+                                session->name, ns, nr, session->nr);
                }
        } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
                u32 l2h = ntohl(*(__be32 *) ptr);
@@ -587,9 +641,9 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
                        L2TP_SKB_CB(skb)->ns = ns;
                        L2TP_SKB_CB(skb)->has_seq = 1;
 
-                       PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
-                              "%s: recv data ns=%u, session nr=%u\n",
-                              session->name, ns, session->nr);
+                       l2tp_dbg(session, L2TP_MSG_SEQ,
+                                "%s: recv data ns=%u, session nr=%u\n",
+                                session->name, ns, session->nr);
                }
        }
 
@@ -602,9 +656,9 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
                 * configure it so.
                 */
                if ((!session->lns_mode) && (!session->send_seq)) {
-                       PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
-                              "%s: requested to enable seq numbers by LNS\n",
-                              session->name);
+                       l2tp_info(session, L2TP_MSG_SEQ,
+                                 "%s: requested to enable seq numbers by LNS\n",
+                                 session->name);
                        session->send_seq = -1;
                        l2tp_session_set_header_len(session, tunnel->version);
                }
@@ -613,10 +667,12 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
                 * If user has configured mandatory sequence numbers, discard.
                 */
                if (session->recv_seq) {
-                       PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
-                              "%s: recv data has no seq numbers when required. "
-                              "Discarding\n", session->name);
-                       session->stats.rx_seq_discards++;
+                       l2tp_warn(session, L2TP_MSG_SEQ,
+                                 "%s: recv data has no seq numbers when required. Discarding.\n",
+                                 session->name);
+                       u64_stats_update_begin(&sstats->syncp);
+                       sstats->rx_seq_discards++;
+                       u64_stats_update_end(&sstats->syncp);
                        goto discard;
                }
 
@@ -626,16 +682,18 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
                 * LAC is broken. Discard the frame.
                 */
                if ((!session->lns_mode) && (session->send_seq)) {
-                       PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
-                              "%s: requested to disable seq numbers by LNS\n",
-                              session->name);
+                       l2tp_info(session, L2TP_MSG_SEQ,
+                                 "%s: requested to disable seq numbers by LNS\n",
+                                 session->name);
                        session->send_seq = 0;
                        l2tp_session_set_header_len(session, tunnel->version);
                } else if (session->send_seq) {
-                       PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
-                              "%s: recv data has no seq numbers when required. "
-                              "Discarding\n", session->name);
-                       session->stats.rx_seq_discards++;
+                       l2tp_warn(session, L2TP_MSG_SEQ,
+                                 "%s: recv data has no seq numbers when required. Discarding.\n",
+                                 session->name);
+                       u64_stats_update_begin(&sstats->syncp);
+                       sstats->rx_seq_discards++;
+                       u64_stats_update_end(&sstats->syncp);
                        goto discard;
                }
        }
@@ -689,13 +747,14 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
                         * packets
                         */
                        if (L2TP_SKB_CB(skb)->ns != session->nr) {
-                               session->stats.rx_seq_discards++;
-                               PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
-                                      "%s: oos pkt %u len %d discarded, "
-                                      "waiting for %u, reorder_q_len=%d\n",
-                                      session->name, L2TP_SKB_CB(skb)->ns,
-                                      L2TP_SKB_CB(skb)->length, session->nr,
-                                      skb_queue_len(&session->reorder_q));
+                               u64_stats_update_begin(&sstats->syncp);
+                               sstats->rx_seq_discards++;
+                               u64_stats_update_end(&sstats->syncp);
+                               l2tp_dbg(session, L2TP_MSG_SEQ,
+                                        "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n",
+                                        session->name, L2TP_SKB_CB(skb)->ns,
+                                        L2TP_SKB_CB(skb)->length, session->nr,
+                                        skb_queue_len(&session->reorder_q));
                                goto discard;
                        }
                        skb_queue_tail(&session->reorder_q, skb);
@@ -716,7 +775,9 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
        return;
 
 discard:
-       session->stats.rx_errors++;
+       u64_stats_update_begin(&sstats->syncp);
+       sstats->rx_errors++;
+       u64_stats_update_end(&sstats->syncp);
        kfree_skb(skb);
 
        if (session->deref)
@@ -739,9 +800,9 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
        unsigned char *ptr, *optr;
        u16 hdrflags;
        u32 tunnel_id, session_id;
-       int offset;
        u16 version;
        int length;
+       struct l2tp_stats *tstats;
 
        if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
                goto discard_bad_csum;
@@ -751,8 +812,9 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
 
        /* Short packet? */
        if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
-               PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
-                      "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
+               l2tp_info(tunnel, L2TP_MSG_DATA,
+                         "%s: recv short packet (len=%d)\n",
+                         tunnel->name, skb->len);
                goto error;
        }
 
@@ -762,14 +824,8 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
                if (!pskb_may_pull(skb, length))
                        goto error;
 
-               printk(KERN_DEBUG "%s: recv: ", tunnel->name);
-
-               offset = 0;
-               do {
-                       printk(" %02X", skb->data[offset]);
-               } while (++offset < length);
-
-               printk("\n");
+               pr_debug("%s: recv\n", tunnel->name);
+               print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
        }
 
        /* Point to L2TP header */
@@ -781,9 +837,9 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
        /* Check protocol version */
        version = hdrflags & L2TP_HDR_VER_MASK;
        if (version != tunnel->version) {
-               PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
-                      "%s: recv protocol version mismatch: got %d expected %d\n",
-                      tunnel->name, version, tunnel->version);
+               l2tp_info(tunnel, L2TP_MSG_DATA,
+                         "%s: recv protocol version mismatch: got %d expected %d\n",
+                         tunnel->name, version, tunnel->version);
                goto error;
        }
 
@@ -792,8 +848,9 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
 
        /* If type is control packet, it is handled by userspace. */
        if (hdrflags & L2TP_HDRFLAG_T) {
-               PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
-                      "%s: recv control packet, len=%d\n", tunnel->name, length);
+               l2tp_dbg(tunnel, L2TP_MSG_DATA,
+                        "%s: recv control packet, len=%d\n",
+                        tunnel->name, length);
                goto error;
        }
 
@@ -821,9 +878,9 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
        session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
        if (!session || !session->recv_skb) {
                /* Not found? Pass to userspace to deal with */
-               PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
-                      "%s: no session found (%u/%u). Passing up.\n",
-                      tunnel->name, tunnel_id, session_id);
+               l2tp_info(tunnel, L2TP_MSG_DATA,
+                         "%s: no session found (%u/%u). Passing up.\n",
+                         tunnel->name, tunnel_id, session_id);
                goto error;
        }
 
@@ -834,7 +891,10 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
 discard_bad_csum:
        LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
        UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
-       tunnel->stats.rx_errors++;
+       tstats = &tunnel->stats;
+       u64_stats_update_begin(&tstats->syncp);
+       tstats->rx_errors++;
+       u64_stats_update_end(&tstats->syncp);
        kfree_skb(skb);
 
        return 0;
@@ -860,8 +920,8 @@ int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
        if (tunnel == NULL)
                goto pass_up;
 
-       PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
-              "%s: received %d bytes\n", tunnel->name, skb->len);
+       l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n",
+                tunnel->name, skb->len);
 
        if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
                goto pass_up_put;
@@ -903,8 +963,8 @@ static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
                *bufp++ = 0;
                session->ns++;
                session->ns &= 0xffff;
-               PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
-                      "%s: updated ns to %u\n", session->name, session->ns);
+               l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n",
+                        session->name, session->ns);
        }
 
        return bufp - optr;
@@ -940,8 +1000,9 @@ static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
                                l2h = 0x40000000 | session->ns;
                                session->ns++;
                                session->ns &= 0xffffff;
-                               PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
-                                      "%s: updated ns to %u\n", session->name, session->ns);
+                               l2tp_dbg(session, L2TP_MSG_SEQ,
+                                        "%s: updated ns to %u\n",
+                                        session->name, session->ns);
                        }
 
                        *((__be32 *) bufp) = htonl(l2h);
@@ -960,46 +1021,50 @@ static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
        struct l2tp_tunnel *tunnel = session->tunnel;
        unsigned int len = skb->len;
        int error;
+       struct l2tp_stats *tstats, *sstats;
 
        /* Debug */
        if (session->send_seq)
-               PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
-                      "%s: send %Zd bytes, ns=%u\n", session->name,
-                      data_len, session->ns - 1);
+               l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n",
+                        session->name, data_len, session->ns - 1);
        else
-               PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
-                      "%s: send %Zd bytes\n", session->name, data_len);
+               l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n",
+                        session->name, data_len);
 
        if (session->debug & L2TP_MSG_DATA) {
-               int i;
                int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
                unsigned char *datap = skb->data + uhlen;
 
-               printk(KERN_DEBUG "%s: xmit:", session->name);
-               for (i = 0; i < (len - uhlen); i++) {
-                       printk(" %02X", *datap++);
-                       if (i == 31) {
-                               printk(" ...");
-                               break;
-                       }
-               }
-               printk("\n");
+               pr_debug("%s: xmit\n", session->name);
+               print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
+                                    datap, min_t(size_t, 32, len - uhlen));
        }
 
        /* Queue the packet to IP for output */
        skb->local_df = 1;
-       error = ip_queue_xmit(skb, fl);
+#if IS_ENABLED(CONFIG_IPV6)
+       if (skb->sk->sk_family == PF_INET6)
+               error = inet6_csk_xmit(skb, NULL);
+       else
+#endif
+               error = ip_queue_xmit(skb, fl);
 
        /* Update stats */
+       tstats = &tunnel->stats;
+       u64_stats_update_begin(&tstats->syncp);
+       sstats = &session->stats;
+       u64_stats_update_begin(&sstats->syncp);
        if (error >= 0) {
-               tunnel->stats.tx_packets++;
-               tunnel->stats.tx_bytes += len;
-               session->stats.tx_packets++;
-               session->stats.tx_bytes += len;
+               tstats->tx_packets++;
+               tstats->tx_bytes += len;
+               sstats->tx_packets++;
+               sstats->tx_bytes += len;
        } else {
-               tunnel->stats.tx_errors++;
-               session->stats.tx_errors++;
+               tstats->tx_errors++;
+               sstats->tx_errors++;
        }
+       u64_stats_update_end(&tstats->syncp);
+       u64_stats_update_end(&sstats->syncp);
 
        return 0;
 }
@@ -1021,6 +1086,31 @@ static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
        skb->destructor = l2tp_sock_wfree;
 }
 
+#if IS_ENABLED(CONFIG_IPV6)
+static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
+                               int udp_len)
+{
+       struct ipv6_pinfo *np = inet6_sk(sk);
+       struct udphdr *uh = udp_hdr(skb);
+
+       if (!skb_dst(skb) || !skb_dst(skb)->dev ||
+           !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
+               __wsum csum = skb_checksum(skb, 0, udp_len, 0);
+               skb->ip_summed = CHECKSUM_UNNECESSARY;
+               uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
+                                           IPPROTO_UDP, csum);
+               if (uh->check == 0)
+                       uh->check = CSUM_MANGLED_0;
+       } else {
+               skb->ip_summed = CHECKSUM_PARTIAL;
+               skb->csum_start = skb_transport_header(skb) - skb->head;
+               skb->csum_offset = offsetof(struct udphdr, check);
+               uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
+                                            udp_len, IPPROTO_UDP, 0);
+       }
+}
+#endif
+
 /* If caller requires the skb to have a ppp header, the header must be
  * inserted in the skb data before calling this function.
  */
@@ -1089,6 +1179,11 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
                uh->check = 0;
 
                /* Calculate UDP checksum if configured to do so */
+#if IS_ENABLED(CONFIG_IPV6)
+               if (sk->sk_family == PF_INET6)
+                       l2tp_xmit_ipv6_csum(sk, skb, udp_len);
+               else
+#endif
                if (sk->sk_no_check == UDP_CSUM_NOXMIT)
                        skb->ip_summed = CHECKSUM_NONE;
                else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
@@ -1141,8 +1236,7 @@ static void l2tp_tunnel_destruct(struct sock *sk)
        if (tunnel == NULL)
                goto end;
 
-       PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
-              "%s: closing...\n", tunnel->name);
+       l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name);
 
        /* Close all sessions */
        l2tp_tunnel_closeall(tunnel);
@@ -1184,8 +1278,8 @@ static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
 
        BUG_ON(tunnel == NULL);
 
-       PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
-              "%s: closing all sessions...\n", tunnel->name);
+       l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n",
+                 tunnel->name);
 
        write_lock_bh(&tunnel->hlist_lock);
        for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
@@ -1193,8 +1287,8 @@ again:
                hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
                        session = hlist_entry(walk, struct l2tp_session, hlist);
 
-                       PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
-                              "%s: closing session\n", session->name);
+                       l2tp_info(session, L2TP_MSG_CONTROL,
+                                 "%s: closing session\n", session->name);
 
                        hlist_del_init(&session->hlist);
 
@@ -1247,8 +1341,7 @@ static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
        BUG_ON(atomic_read(&tunnel->ref_count) != 0);
        BUG_ON(tunnel->sock != NULL);
 
-       PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
-              "%s: free...\n", tunnel->name);
+       l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
 
        /* Remove from tunnel list */
        spin_lock_bh(&pn->l2tp_tunnel_list_lock);
@@ -1268,31 +1361,69 @@ static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2t
 {
        int err = -EINVAL;
        struct sockaddr_in udp_addr;
+#if IS_ENABLED(CONFIG_IPV6)
+       struct sockaddr_in6 udp6_addr;
+       struct sockaddr_l2tpip6 ip6_addr;
+#endif
        struct sockaddr_l2tpip ip_addr;
        struct socket *sock = NULL;
 
        switch (cfg->encap) {
        case L2TP_ENCAPTYPE_UDP:
-               err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
-               if (err < 0)
-                       goto out;
+#if IS_ENABLED(CONFIG_IPV6)
+               if (cfg->local_ip6 && cfg->peer_ip6) {
+                       err = sock_create(AF_INET6, SOCK_DGRAM, 0, sockp);
+                       if (err < 0)
+                               goto out;
 
-               sock = *sockp;
+                       sock = *sockp;
 
-               memset(&udp_addr, 0, sizeof(udp_addr));
-               udp_addr.sin_family = AF_INET;
-               udp_addr.sin_addr = cfg->local_ip;
-               udp_addr.sin_port = htons(cfg->local_udp_port);
-               err = kernel_bind(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
-               if (err < 0)
-                       goto out;
+                       memset(&udp6_addr, 0, sizeof(udp6_addr));
+                       udp6_addr.sin6_family = AF_INET6;
+                       memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
+                              sizeof(udp6_addr.sin6_addr));
+                       udp6_addr.sin6_port = htons(cfg->local_udp_port);
+                       err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
+                                         sizeof(udp6_addr));
+                       if (err < 0)
+                               goto out;
 
-               udp_addr.sin_family = AF_INET;
-               udp_addr.sin_addr = cfg->peer_ip;
-               udp_addr.sin_port = htons(cfg->peer_udp_port);
-               err = kernel_connect(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr), 0);
-               if (err < 0)
-                       goto out;
+                       udp6_addr.sin6_family = AF_INET6;
+                       memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
+                              sizeof(udp6_addr.sin6_addr));
+                       udp6_addr.sin6_port = htons(cfg->peer_udp_port);
+                       err = kernel_connect(sock,
+                                            (struct sockaddr *) &udp6_addr,
+                                            sizeof(udp6_addr), 0);
+                       if (err < 0)
+                               goto out;
+               } else
+#endif
+               {
+                       err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
+                       if (err < 0)
+                               goto out;
+
+                       sock = *sockp;
+
+                       memset(&udp_addr, 0, sizeof(udp_addr));
+                       udp_addr.sin_family = AF_INET;
+                       udp_addr.sin_addr = cfg->local_ip;
+                       udp_addr.sin_port = htons(cfg->local_udp_port);
+                       err = kernel_bind(sock, (struct sockaddr *) &udp_addr,
+                                         sizeof(udp_addr));
+                       if (err < 0)
+                               goto out;
+
+                       udp_addr.sin_family = AF_INET;
+                       udp_addr.sin_addr = cfg->peer_ip;
+                       udp_addr.sin_port = htons(cfg->peer_udp_port);
+                       err = kernel_connect(sock,
+                                            (struct sockaddr *) &udp_addr,
+                                            sizeof(udp_addr), 0);
+                       if (err < 0)
+                               goto out;
+               }
 
                if (!cfg->use_udp_checksums)
                        sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
@@ -1300,27 +1431,61 @@ static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2t
                break;
 
        case L2TP_ENCAPTYPE_IP:
-               err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp);
-               if (err < 0)
-                       goto out;
+#if IS_ENABLED(CONFIG_IPV6)
+               if (cfg->local_ip6 && cfg->peer_ip6) {
+                       err = sock_create(AF_INET6, SOCK_DGRAM, IPPROTO_L2TP,
+                                         sockp);
+                       if (err < 0)
+                               goto out;
 
-               sock = *sockp;
+                       sock = *sockp;
 
-               memset(&ip_addr, 0, sizeof(ip_addr));
-               ip_addr.l2tp_family = AF_INET;
-               ip_addr.l2tp_addr = cfg->local_ip;
-               ip_addr.l2tp_conn_id = tunnel_id;
-               err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr));
-               if (err < 0)
-                       goto out;
+                       memset(&ip6_addr, 0, sizeof(ip6_addr));
+                       ip6_addr.l2tp_family = AF_INET6;
+                       memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
+                              sizeof(ip6_addr.l2tp_addr));
+                       ip6_addr.l2tp_conn_id = tunnel_id;
+                       err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
+                                         sizeof(ip6_addr));
+                       if (err < 0)
+                               goto out;
 
-               ip_addr.l2tp_family = AF_INET;
-               ip_addr.l2tp_addr = cfg->peer_ip;
-               ip_addr.l2tp_conn_id = peer_tunnel_id;
-               err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0);
-               if (err < 0)
-                       goto out;
+                       ip6_addr.l2tp_family = AF_INET6;
+                       memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
+                              sizeof(ip6_addr.l2tp_addr));
+                       ip6_addr.l2tp_conn_id = peer_tunnel_id;
+                       err = kernel_connect(sock,
+                                            (struct sockaddr *) &ip6_addr,
+                                            sizeof(ip6_addr), 0);
+                       if (err < 0)
+                               goto out;
+               } else
+#endif
+               {
+                       err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP,
+                                         sockp);
+                       if (err < 0)
+                               goto out;
+
+                       sock = *sockp;
+
+                       memset(&ip_addr, 0, sizeof(ip_addr));
+                       ip_addr.l2tp_family = AF_INET;
+                       ip_addr.l2tp_addr = cfg->local_ip;
+                       ip_addr.l2tp_conn_id = tunnel_id;
+                       err = kernel_bind(sock, (struct sockaddr *) &ip_addr,
+                                         sizeof(ip_addr));
+                       if (err < 0)
+                               goto out;
 
+                       ip_addr.l2tp_family = AF_INET;
+                       ip_addr.l2tp_addr = cfg->peer_ip;
+                       ip_addr.l2tp_conn_id = peer_tunnel_id;
+                       err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
+                                            sizeof(ip_addr), 0);
+                       if (err < 0)
+                               goto out;
+               }
                break;
 
        default:
@@ -1357,7 +1522,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
                err = -EBADF;
                sock = sockfd_lookup(fd, &err);
                if (!sock) {
-                       printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
+                       pr_err("tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
                               tunnel_id, fd, err);
                        goto err;
                }
@@ -1373,7 +1538,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
        case L2TP_ENCAPTYPE_UDP:
                err = -EPROTONOSUPPORT;
                if (sk->sk_protocol != IPPROTO_UDP) {
-                       printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
+                       pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
                               tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
                        goto err;
                }
@@ -1381,7 +1546,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
        case L2TP_ENCAPTYPE_IP:
                err = -EPROTONOSUPPORT;
                if (sk->sk_protocol != IPPROTO_L2TP) {
-                       printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
+                       pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
                               tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
                        goto err;
                }
@@ -1424,6 +1589,12 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
                /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
                udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
                udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
+#if IS_ENABLED(CONFIG_IPV6)
+               if (sk->sk_family == PF_INET6)
+                       udpv6_encap_enable();
+               else
+#endif
+               udp_encap_enable();
        }
 
        sk->sk_user_data = tunnel;
@@ -1577,7 +1748,7 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn
 
                session->session_id = session_id;
                session->peer_session_id = peer_session_id;
-               session->nr = 1;
+               session->nr = 0;
 
                sprintf(&session->name[0], "sess %u/%u",
                        tunnel->tunnel_id, session->session_id);
@@ -1683,7 +1854,7 @@ static int __init l2tp_init(void)
        if (rc)
                goto out;
 
-       printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
+       pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION);
 
 out:
        return rc;