aboutsummaryrefslogtreecommitdiffstats
path: root/net/l2tp/l2tp_core.c
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
commit8dea78da5cee153b8af9c07a2745f6c55057fe12 (patch)
treea8f4d49d63b1ecc92f2fddceba0655b2472c5bd9 /net/l2tp/l2tp_core.c
parent406089d01562f1e2bf9f089fd7637009ebaad589 (diff)
Patched in Tegra support.
Diffstat (limited to 'net/l2tp/l2tp_core.c')
-rw-r--r--net/l2tp/l2tp_core.c554
1 files changed, 189 insertions, 365 deletions
diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c
index 1a9f3723c13..34b2ddeacb6 100644
--- a/net/l2tp/l2tp_core.c
+++ b/net/l2tp/l2tp_core.c
@@ -18,8 +18,6 @@
18 * published by the Free Software Foundation. 18 * published by the Free Software Foundation.
19 */ 19 */
20 20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/module.h> 21#include <linux/module.h>
24#include <linux/string.h> 22#include <linux/string.h>
25#include <linux/list.h> 23#include <linux/list.h>
@@ -55,10 +53,6 @@
55#include <net/inet_common.h> 53#include <net/inet_common.h>
56#include <net/xfrm.h> 54#include <net/xfrm.h>
57#include <net/protocol.h> 55#include <net/protocol.h>
58#include <net/inet6_connection_sock.h>
59#include <net/inet_ecn.h>
60#include <net/ip6_route.h>
61#include <net/ip6_checksum.h>
62 56
63#include <asm/byteorder.h> 57#include <asm/byteorder.h>
64#include <linux/atomic.h> 58#include <linux/atomic.h>
@@ -88,6 +82,12 @@
88/* Default trace flags */ 82/* Default trace flags */
89#define L2TP_DEFAULT_DEBUG_FLAGS 0 83#define L2TP_DEFAULT_DEBUG_FLAGS 0
90 84
85#define PRINTK(_mask, _type, _lvl, _fmt, args...) \
86 do { \
87 if ((_mask) & (_type)) \
88 printk(_lvl "L2TP: " _fmt, ##args); \
89 } while (0)
90
91/* Private data stored for received packets in the skb. 91/* Private data stored for received packets in the skb.
92 */ 92 */
93struct l2tp_skb_cb { 93struct l2tp_skb_cb {
@@ -137,20 +137,14 @@ static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
137 l2tp_tunnel_free(tunnel); 137 l2tp_tunnel_free(tunnel);
138} 138}
139#ifdef L2TP_REFCNT_DEBUG 139#ifdef L2TP_REFCNT_DEBUG
140#define l2tp_tunnel_inc_refcount(_t) \ 140#define l2tp_tunnel_inc_refcount(_t) do { \
141do { \ 141 printk(KERN_DEBUG "l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
142 pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \ 142 l2tp_tunnel_inc_refcount_1(_t); \
143 __func__, __LINE__, (_t)->name, \ 143 } while (0)
144 atomic_read(&_t->ref_count)); \ 144#define l2tp_tunnel_dec_refcount(_t) do { \
145 l2tp_tunnel_inc_refcount_1(_t); \ 145 printk(KERN_DEBUG "l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_t)->name, atomic_read(&_t->ref_count)); \
146} while (0) 146 l2tp_tunnel_dec_refcount_1(_t); \
147#define l2tp_tunnel_dec_refcount(_t) 147 } while (0)
148do { \
149 pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
150 __func__, __LINE__, (_t)->name, \
151 atomic_read(&_t->ref_count)); \
152 l2tp_tunnel_dec_refcount_1(_t); \
153} while (0)
154#else 148#else
155#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t) 149#define l2tp_tunnel_inc_refcount(t) l2tp_tunnel_inc_refcount_1(t)
156#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t) 150#define l2tp_tunnel_dec_refcount(t) l2tp_tunnel_dec_refcount_1(t)
@@ -332,20 +326,16 @@ static void l2tp_recv_queue_skb(struct l2tp_session *session, struct sk_buff *sk
332 struct sk_buff *skbp; 326 struct sk_buff *skbp;
333 struct sk_buff *tmp; 327 struct sk_buff *tmp;
334 u32 ns = L2TP_SKB_CB(skb)->ns; 328 u32 ns = L2TP_SKB_CB(skb)->ns;
335 struct l2tp_stats *sstats;
336 329
337 spin_lock_bh(&session->reorder_q.lock); 330 spin_lock_bh(&session->reorder_q.lock);
338 sstats = &session->stats;
339 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) { 331 skb_queue_walk_safe(&session->reorder_q, skbp, tmp) {
340 if (L2TP_SKB_CB(skbp)->ns > ns) { 332 if (L2TP_SKB_CB(skbp)->ns > ns) {
341 __skb_queue_before(&session->reorder_q, skbp, skb); 333 __skb_queue_before(&session->reorder_q, skbp, skb);
342 l2tp_dbg(session, L2TP_MSG_SEQ, 334 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
343 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n", 335 "%s: pkt %hu, inserted before %hu, reorder_q len=%d\n",
344 session->name, ns, L2TP_SKB_CB(skbp)->ns, 336 session->name, ns, L2TP_SKB_CB(skbp)->ns,
345 skb_queue_len(&session->reorder_q)); 337 skb_queue_len(&session->reorder_q));
346 u64_stats_update_begin(&sstats->syncp); 338 session->stats.rx_oos_packets++;
347 sstats->rx_oos_packets++;
348 u64_stats_update_end(&sstats->syncp);
349 goto out; 339 goto out;
350 } 340 }
351 } 341 }
@@ -362,23 +352,16 @@ static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *
362{ 352{
363 struct l2tp_tunnel *tunnel = session->tunnel; 353 struct l2tp_tunnel *tunnel = session->tunnel;
364 int length = L2TP_SKB_CB(skb)->length; 354 int length = L2TP_SKB_CB(skb)->length;
365 struct l2tp_stats *tstats, *sstats;
366 355
367 /* We're about to requeue the skb, so return resources 356 /* We're about to requeue the skb, so return resources
368 * to its current owner (a socket receive buffer). 357 * to its current owner (a socket receive buffer).
369 */ 358 */
370 skb_orphan(skb); 359 skb_orphan(skb);
371 360
372 tstats = &tunnel->stats; 361 tunnel->stats.rx_packets++;
373 u64_stats_update_begin(&tstats->syncp); 362 tunnel->stats.rx_bytes += length;
374 sstats = &session->stats; 363 session->stats.rx_packets++;
375 u64_stats_update_begin(&sstats->syncp); 364 session->stats.rx_bytes += length;
376 tstats->rx_packets++;
377 tstats->rx_bytes += length;
378 sstats->rx_packets++;
379 sstats->rx_bytes += length;
380 u64_stats_update_end(&tstats->syncp);
381 u64_stats_update_end(&sstats->syncp);
382 365
383 if (L2TP_SKB_CB(skb)->has_seq) { 366 if (L2TP_SKB_CB(skb)->has_seq) {
384 /* Bump our Nr */ 367 /* Bump our Nr */
@@ -388,8 +371,8 @@ static void l2tp_recv_dequeue_skb(struct l2tp_session *session, struct sk_buff *
388 else 371 else
389 session->nr &= 0xffffff; 372 session->nr &= 0xffffff;
390 373
391 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated nr to %hu\n", 374 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
392 session->name, session->nr); 375 "%s: updated nr to %hu\n", session->name, session->nr);
393 } 376 }
394 377
395 /* call private receive handler */ 378 /* call private receive handler */
@@ -409,27 +392,22 @@ static void l2tp_recv_dequeue(struct l2tp_session *session)
409{ 392{
410 struct sk_buff *skb; 393 struct sk_buff *skb;
411 struct sk_buff *tmp; 394 struct sk_buff *tmp;
412 struct l2tp_stats *sstats;
413 395
414 /* If the pkt at the head of the queue has the nr that we 396 /* If the pkt at the head of the queue has the nr that we
415 * expect to send up next, dequeue it and any other 397 * expect to send up next, dequeue it and any other
416 * in-sequence packets behind it. 398 * in-sequence packets behind it.
417 */ 399 */
418start:
419 spin_lock_bh(&session->reorder_q.lock); 400 spin_lock_bh(&session->reorder_q.lock);
420 sstats = &session->stats;
421 skb_queue_walk_safe(&session->reorder_q, skb, tmp) { 401 skb_queue_walk_safe(&session->reorder_q, skb, tmp) {
422 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) { 402 if (time_after(jiffies, L2TP_SKB_CB(skb)->expires)) {
423 u64_stats_update_begin(&sstats->syncp); 403 session->stats.rx_seq_discards++;
424 sstats->rx_seq_discards++; 404 session->stats.rx_errors++;
425 sstats->rx_errors++; 405 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
426 u64_stats_update_end(&sstats->syncp); 406 "%s: oos pkt %u len %d discarded (too old), "
427 l2tp_dbg(session, L2TP_MSG_SEQ, 407 "waiting for %u, reorder_q_len=%d\n",
428 "%s: oos pkt %u len %d discarded (too old), waiting for %u, reorder_q_len=%d\n", 408 session->name, L2TP_SKB_CB(skb)->ns,
429 session->name, L2TP_SKB_CB(skb)->ns, 409 L2TP_SKB_CB(skb)->length, session->nr,
430 L2TP_SKB_CB(skb)->length, session->nr, 410 skb_queue_len(&session->reorder_q));
431 skb_queue_len(&session->reorder_q));
432 session->reorder_skip = 1;
433 __skb_unlink(skb, &session->reorder_q); 411 __skb_unlink(skb, &session->reorder_q);
434 kfree_skb(skb); 412 kfree_skb(skb);
435 if (session->deref) 413 if (session->deref)
@@ -438,20 +416,13 @@ start:
438 } 416 }
439 417
440 if (L2TP_SKB_CB(skb)->has_seq) { 418 if (L2TP_SKB_CB(skb)->has_seq) {
441 if (session->reorder_skip) {
442 l2tp_dbg(session, L2TP_MSG_SEQ,
443 "%s: advancing nr to next pkt: %u -> %u",
444 session->name, session->nr,
445 L2TP_SKB_CB(skb)->ns);
446 session->reorder_skip = 0;
447 session->nr = L2TP_SKB_CB(skb)->ns;
448 }
449 if (L2TP_SKB_CB(skb)->ns != session->nr) { 419 if (L2TP_SKB_CB(skb)->ns != session->nr) {
450 l2tp_dbg(session, L2TP_MSG_SEQ, 420 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
451 "%s: holding oos pkt %u len %d, waiting for %u, reorder_q_len=%d\n", 421 "%s: holding oos pkt %u len %d, "
452 session->name, L2TP_SKB_CB(skb)->ns, 422 "waiting for %u, reorder_q_len=%d\n",
453 L2TP_SKB_CB(skb)->length, session->nr, 423 session->name, L2TP_SKB_CB(skb)->ns,
454 skb_queue_len(&session->reorder_q)); 424 L2TP_SKB_CB(skb)->length, session->nr,
425 skb_queue_len(&session->reorder_q));
455 goto out; 426 goto out;
456 } 427 }
457 } 428 }
@@ -462,7 +433,7 @@ start:
462 */ 433 */
463 spin_unlock_bh(&session->reorder_q.lock); 434 spin_unlock_bh(&session->reorder_q.lock);
464 l2tp_recv_dequeue_skb(session, skb); 435 l2tp_recv_dequeue_skb(session, skb);
465 goto start; 436 spin_lock_bh(&session->reorder_q.lock);
466 } 437 }
467 438
468out: 439out:
@@ -474,43 +445,21 @@ static inline int l2tp_verify_udp_checksum(struct sock *sk,
474{ 445{
475 struct udphdr *uh = udp_hdr(skb); 446 struct udphdr *uh = udp_hdr(skb);
476 u16 ulen = ntohs(uh->len); 447 u16 ulen = ntohs(uh->len);
448 struct inet_sock *inet;
477 __wsum psum; 449 __wsum psum;
478 450
479 if (sk->sk_no_check || skb_csum_unnecessary(skb)) 451 if (sk->sk_no_check || skb_csum_unnecessary(skb) || !uh->check)
480 return 0; 452 return 0;
481 453
482#if IS_ENABLED(CONFIG_IPV6) 454 inet = inet_sk(sk);
483 if (sk->sk_family == PF_INET6) { 455 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr, ulen,
484 if (!uh->check) { 456 IPPROTO_UDP, 0);
485 LIMIT_NETDEBUG(KERN_INFO "L2TP: IPv6: checksum is 0\n"); 457
486 return 1; 458 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
487 } 459 !csum_fold(csum_add(psum, skb->csum)))
488 if ((skb->ip_summed == CHECKSUM_COMPLETE) && 460 return 0;
489 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 461
490 &ipv6_hdr(skb)->daddr, ulen, 462 skb->csum = psum;
491 IPPROTO_UDP, skb->csum)) {
492 skb->ip_summed = CHECKSUM_UNNECESSARY;
493 return 0;
494 }
495 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
496 &ipv6_hdr(skb)->daddr,
497 skb->len, IPPROTO_UDP,
498 0));
499 } else
500#endif
501 {
502 struct inet_sock *inet;
503 if (!uh->check)
504 return 0;
505 inet = inet_sk(sk);
506 psum = csum_tcpudp_nofold(inet->inet_saddr, inet->inet_daddr,
507 ulen, IPPROTO_UDP, 0);
508
509 if ((skb->ip_summed == CHECKSUM_COMPLETE) &&
510 !csum_fold(csum_add(psum, skb->csum)))
511 return 0;
512 skb->csum = psum;
513 }
514 463
515 return __skb_checksum_complete(skb); 464 return __skb_checksum_complete(skb);
516} 465}
@@ -582,7 +531,6 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
582 struct l2tp_tunnel *tunnel = session->tunnel; 531 struct l2tp_tunnel *tunnel = session->tunnel;
583 int offset; 532 int offset;
584 u32 ns, nr; 533 u32 ns, nr;
585 struct l2tp_stats *sstats = &session->stats;
586 534
587 /* The ref count is increased since we now hold a pointer to 535 /* The ref count is increased since we now hold a pointer to
588 * the session. Take care to decrement the refcnt when exiting 536 * the session. Take care to decrement the refcnt when exiting
@@ -595,13 +543,10 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
595 /* Parse and check optional cookie */ 543 /* Parse and check optional cookie */
596 if (session->peer_cookie_len > 0) { 544 if (session->peer_cookie_len > 0) {
597 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) { 545 if (memcmp(ptr, &session->peer_cookie[0], session->peer_cookie_len)) {
598 l2tp_info(tunnel, L2TP_MSG_DATA, 546 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
599 "%s: cookie mismatch (%u/%u). Discarding.\n", 547 "%s: cookie mismatch (%u/%u). Discarding.\n",
600 tunnel->name, tunnel->tunnel_id, 548 tunnel->name, tunnel->tunnel_id, session->session_id);
601 session->session_id); 549 session->stats.rx_cookie_discards++;
602 u64_stats_update_begin(&sstats->syncp);
603 sstats->rx_cookie_discards++;
604 u64_stats_update_end(&sstats->syncp);
605 goto discard; 550 goto discard;
606 } 551 }
607 ptr += session->peer_cookie_len; 552 ptr += session->peer_cookie_len;
@@ -627,9 +572,9 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
627 L2TP_SKB_CB(skb)->ns = ns; 572 L2TP_SKB_CB(skb)->ns = ns;
628 L2TP_SKB_CB(skb)->has_seq = 1; 573 L2TP_SKB_CB(skb)->has_seq = 1;
629 574
630 l2tp_dbg(session, L2TP_MSG_SEQ, 575 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
631 "%s: recv data ns=%u, nr=%u, session nr=%u\n", 576 "%s: recv data ns=%u, nr=%u, session nr=%u\n",
632 session->name, ns, nr, session->nr); 577 session->name, ns, nr, session->nr);
633 } 578 }
634 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) { 579 } else if (session->l2specific_type == L2TP_L2SPECTYPE_DEFAULT) {
635 u32 l2h = ntohl(*(__be32 *) ptr); 580 u32 l2h = ntohl(*(__be32 *) ptr);
@@ -641,9 +586,9 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
641 L2TP_SKB_CB(skb)->ns = ns; 586 L2TP_SKB_CB(skb)->ns = ns;
642 L2TP_SKB_CB(skb)->has_seq = 1; 587 L2TP_SKB_CB(skb)->has_seq = 1;
643 588
644 l2tp_dbg(session, L2TP_MSG_SEQ, 589 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
645 "%s: recv data ns=%u, session nr=%u\n", 590 "%s: recv data ns=%u, session nr=%u\n",
646 session->name, ns, session->nr); 591 session->name, ns, session->nr);
647 } 592 }
648 } 593 }
649 594
@@ -656,9 +601,9 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
656 * configure it so. 601 * configure it so.
657 */ 602 */
658 if ((!session->lns_mode) && (!session->send_seq)) { 603 if ((!session->lns_mode) && (!session->send_seq)) {
659 l2tp_info(session, L2TP_MSG_SEQ, 604 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
660 "%s: requested to enable seq numbers by LNS\n", 605 "%s: requested to enable seq numbers by LNS\n",
661 session->name); 606 session->name);
662 session->send_seq = -1; 607 session->send_seq = -1;
663 l2tp_session_set_header_len(session, tunnel->version); 608 l2tp_session_set_header_len(session, tunnel->version);
664 } 609 }
@@ -667,12 +612,10 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
667 * If user has configured mandatory sequence numbers, discard. 612 * If user has configured mandatory sequence numbers, discard.
668 */ 613 */
669 if (session->recv_seq) { 614 if (session->recv_seq) {
670 l2tp_warn(session, L2TP_MSG_SEQ, 615 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
671 "%s: recv data has no seq numbers when required. Discarding.\n", 616 "%s: recv data has no seq numbers when required. "
672 session->name); 617 "Discarding\n", session->name);
673 u64_stats_update_begin(&sstats->syncp); 618 session->stats.rx_seq_discards++;
674 sstats->rx_seq_discards++;
675 u64_stats_update_end(&sstats->syncp);
676 goto discard; 619 goto discard;
677 } 620 }
678 621
@@ -682,18 +625,16 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
682 * LAC is broken. Discard the frame. 625 * LAC is broken. Discard the frame.
683 */ 626 */
684 if ((!session->lns_mode) && (session->send_seq)) { 627 if ((!session->lns_mode) && (session->send_seq)) {
685 l2tp_info(session, L2TP_MSG_SEQ, 628 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_INFO,
686 "%s: requested to disable seq numbers by LNS\n", 629 "%s: requested to disable seq numbers by LNS\n",
687 session->name); 630 session->name);
688 session->send_seq = 0; 631 session->send_seq = 0;
689 l2tp_session_set_header_len(session, tunnel->version); 632 l2tp_session_set_header_len(session, tunnel->version);
690 } else if (session->send_seq) { 633 } else if (session->send_seq) {
691 l2tp_warn(session, L2TP_MSG_SEQ, 634 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_WARNING,
692 "%s: recv data has no seq numbers when required. Discarding.\n", 635 "%s: recv data has no seq numbers when required. "
693 session->name); 636 "Discarding\n", session->name);
694 u64_stats_update_begin(&sstats->syncp); 637 session->stats.rx_seq_discards++;
695 sstats->rx_seq_discards++;
696 u64_stats_update_end(&sstats->syncp);
697 goto discard; 638 goto discard;
698 } 639 }
699 } 640 }
@@ -747,14 +688,13 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
747 * packets 688 * packets
748 */ 689 */
749 if (L2TP_SKB_CB(skb)->ns != session->nr) { 690 if (L2TP_SKB_CB(skb)->ns != session->nr) {
750 u64_stats_update_begin(&sstats->syncp); 691 session->stats.rx_seq_discards++;
751 sstats->rx_seq_discards++; 692 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
752 u64_stats_update_end(&sstats->syncp); 693 "%s: oos pkt %u len %d discarded, "
753 l2tp_dbg(session, L2TP_MSG_SEQ, 694 "waiting for %u, reorder_q_len=%d\n",
754 "%s: oos pkt %u len %d discarded, waiting for %u, reorder_q_len=%d\n", 695 session->name, L2TP_SKB_CB(skb)->ns,
755 session->name, L2TP_SKB_CB(skb)->ns, 696 L2TP_SKB_CB(skb)->length, session->nr,
756 L2TP_SKB_CB(skb)->length, session->nr, 697 skb_queue_len(&session->reorder_q));
757 skb_queue_len(&session->reorder_q));
758 goto discard; 698 goto discard;
759 } 699 }
760 skb_queue_tail(&session->reorder_q, skb); 700 skb_queue_tail(&session->reorder_q, skb);
@@ -775,9 +715,7 @@ void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
775 return; 715 return;
776 716
777discard: 717discard:
778 u64_stats_update_begin(&sstats->syncp); 718 session->stats.rx_errors++;
779 sstats->rx_errors++;
780 u64_stats_update_end(&sstats->syncp);
781 kfree_skb(skb); 719 kfree_skb(skb);
782 720
783 if (session->deref) 721 if (session->deref)
@@ -800,9 +738,9 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
800 unsigned char *ptr, *optr; 738 unsigned char *ptr, *optr;
801 u16 hdrflags; 739 u16 hdrflags;
802 u32 tunnel_id, session_id; 740 u32 tunnel_id, session_id;
741 int offset;
803 u16 version; 742 u16 version;
804 int length; 743 int length;
805 struct l2tp_stats *tstats;
806 744
807 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb)) 745 if (tunnel->sock && l2tp_verify_udp_checksum(tunnel->sock, skb))
808 goto discard_bad_csum; 746 goto discard_bad_csum;
@@ -812,24 +750,29 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
812 750
813 /* Short packet? */ 751 /* Short packet? */
814 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) { 752 if (!pskb_may_pull(skb, L2TP_HDR_SIZE_SEQ)) {
815 l2tp_info(tunnel, L2TP_MSG_DATA, 753 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
816 "%s: recv short packet (len=%d)\n", 754 "%s: recv short packet (len=%d)\n", tunnel->name, skb->len);
817 tunnel->name, skb->len);
818 goto error; 755 goto error;
819 } 756 }
820 757
758 /* Point to L2TP header */
759 optr = ptr = skb->data;
760
821 /* Trace packet contents, if enabled */ 761 /* Trace packet contents, if enabled */
822 if (tunnel->debug & L2TP_MSG_DATA) { 762 if (tunnel->debug & L2TP_MSG_DATA) {
823 length = min(32u, skb->len); 763 length = min(32u, skb->len);
824 if (!pskb_may_pull(skb, length)) 764 if (!pskb_may_pull(skb, length))
825 goto error; 765 goto error;
826 766
827 pr_debug("%s: recv\n", tunnel->name); 767 printk(KERN_DEBUG "%s: recv: ", tunnel->name);
828 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, skb->data, length);
829 }
830 768
831 /* Point to L2TP header */ 769 offset = 0;
832 optr = ptr = skb->data; 770 do {
771 printk(" %02X", ptr[offset]);
772 } while (++offset < length);
773
774 printk("\n");
775 }
833 776
834 /* Get L2TP header flags */ 777 /* Get L2TP header flags */
835 hdrflags = ntohs(*(__be16 *) ptr); 778 hdrflags = ntohs(*(__be16 *) ptr);
@@ -837,9 +780,9 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
837 /* Check protocol version */ 780 /* Check protocol version */
838 version = hdrflags & L2TP_HDR_VER_MASK; 781 version = hdrflags & L2TP_HDR_VER_MASK;
839 if (version != tunnel->version) { 782 if (version != tunnel->version) {
840 l2tp_info(tunnel, L2TP_MSG_DATA, 783 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
841 "%s: recv protocol version mismatch: got %d expected %d\n", 784 "%s: recv protocol version mismatch: got %d expected %d\n",
842 tunnel->name, version, tunnel->version); 785 tunnel->name, version, tunnel->version);
843 goto error; 786 goto error;
844 } 787 }
845 788
@@ -848,9 +791,8 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
848 791
849 /* If type is control packet, it is handled by userspace. */ 792 /* If type is control packet, it is handled by userspace. */
850 if (hdrflags & L2TP_HDRFLAG_T) { 793 if (hdrflags & L2TP_HDRFLAG_T) {
851 l2tp_dbg(tunnel, L2TP_MSG_DATA, 794 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
852 "%s: recv control packet, len=%d\n", 795 "%s: recv control packet, len=%d\n", tunnel->name, length);
853 tunnel->name, length);
854 goto error; 796 goto error;
855 } 797 }
856 798
@@ -878,9 +820,9 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
878 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id); 820 session = l2tp_session_find(tunnel->l2tp_net, tunnel, session_id);
879 if (!session || !session->recv_skb) { 821 if (!session || !session->recv_skb) {
880 /* Not found? Pass to userspace to deal with */ 822 /* Not found? Pass to userspace to deal with */
881 l2tp_info(tunnel, L2TP_MSG_DATA, 823 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_INFO,
882 "%s: no session found (%u/%u). Passing up.\n", 824 "%s: no session found (%u/%u). Passing up.\n",
883 tunnel->name, tunnel_id, session_id); 825 tunnel->name, tunnel_id, session_id);
884 goto error; 826 goto error;
885 } 827 }
886 828
@@ -891,10 +833,7 @@ static int l2tp_udp_recv_core(struct l2tp_tunnel *tunnel, struct sk_buff *skb,
891discard_bad_csum: 833discard_bad_csum:
892 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name); 834 LIMIT_NETDEBUG("%s: UDP: bad checksum\n", tunnel->name);
893 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0); 835 UDP_INC_STATS_USER(tunnel->l2tp_net, UDP_MIB_INERRORS, 0);
894 tstats = &tunnel->stats; 836 tunnel->stats.rx_errors++;
895 u64_stats_update_begin(&tstats->syncp);
896 tstats->rx_errors++;
897 u64_stats_update_end(&tstats->syncp);
898 kfree_skb(skb); 837 kfree_skb(skb);
899 838
900 return 0; 839 return 0;
@@ -920,8 +859,8 @@ int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
920 if (tunnel == NULL) 859 if (tunnel == NULL)
921 goto pass_up; 860 goto pass_up;
922 861
923 l2tp_dbg(tunnel, L2TP_MSG_DATA, "%s: received %d bytes\n", 862 PRINTK(tunnel->debug, L2TP_MSG_DATA, KERN_DEBUG,
924 tunnel->name, skb->len); 863 "%s: received %d bytes\n", tunnel->name, skb->len);
925 864
926 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook)) 865 if (l2tp_udp_recv_core(tunnel, skb, tunnel->recv_payload_hook))
927 goto pass_up_put; 866 goto pass_up_put;
@@ -963,8 +902,8 @@ static int l2tp_build_l2tpv2_header(struct l2tp_session *session, void *buf)
963 *bufp++ = 0; 902 *bufp++ = 0;
964 session->ns++; 903 session->ns++;
965 session->ns &= 0xffff; 904 session->ns &= 0xffff;
966 l2tp_dbg(session, L2TP_MSG_SEQ, "%s: updated ns to %u\n", 905 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
967 session->name, session->ns); 906 "%s: updated ns to %u\n", session->name, session->ns);
968 } 907 }
969 908
970 return bufp - optr; 909 return bufp - optr;
@@ -1000,9 +939,8 @@ static int l2tp_build_l2tpv3_header(struct l2tp_session *session, void *buf)
1000 l2h = 0x40000000 | session->ns; 939 l2h = 0x40000000 | session->ns;
1001 session->ns++; 940 session->ns++;
1002 session->ns &= 0xffffff; 941 session->ns &= 0xffffff;
1003 l2tp_dbg(session, L2TP_MSG_SEQ, 942 PRINTK(session->debug, L2TP_MSG_SEQ, KERN_DEBUG,
1004 "%s: updated ns to %u\n", 943 "%s: updated ns to %u\n", session->name, session->ns);
1005 session->name, session->ns);
1006 } 944 }
1007 945
1008 *((__be32 *) bufp) = htonl(l2h); 946 *((__be32 *) bufp) = htonl(l2h);
@@ -1021,50 +959,46 @@ static int l2tp_xmit_core(struct l2tp_session *session, struct sk_buff *skb,
1021 struct l2tp_tunnel *tunnel = session->tunnel; 959 struct l2tp_tunnel *tunnel = session->tunnel;
1022 unsigned int len = skb->len; 960 unsigned int len = skb->len;
1023 int error; 961 int error;
1024 struct l2tp_stats *tstats, *sstats;
1025 962
1026 /* Debug */ 963 /* Debug */
1027 if (session->send_seq) 964 if (session->send_seq)
1028 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes, ns=%u\n", 965 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
1029 session->name, data_len, session->ns - 1); 966 "%s: send %Zd bytes, ns=%u\n", session->name,
967 data_len, session->ns - 1);
1030 else 968 else
1031 l2tp_dbg(session, L2TP_MSG_DATA, "%s: send %Zd bytes\n", 969 PRINTK(session->debug, L2TP_MSG_DATA, KERN_DEBUG,
1032 session->name, data_len); 970 "%s: send %Zd bytes\n", session->name, data_len);
1033 971
1034 if (session->debug & L2TP_MSG_DATA) { 972 if (session->debug & L2TP_MSG_DATA) {
973 int i;
1035 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 974 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1036 unsigned char *datap = skb->data + uhlen; 975 unsigned char *datap = skb->data + uhlen;
1037 976
1038 pr_debug("%s: xmit\n", session->name); 977 printk(KERN_DEBUG "%s: xmit:", session->name);
1039 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, 978 for (i = 0; i < (len - uhlen); i++) {
1040 datap, min_t(size_t, 32, len - uhlen)); 979 printk(" %02X", *datap++);
980 if (i == 31) {
981 printk(" ...");
982 break;
983 }
984 }
985 printk("\n");
1041 } 986 }
1042 987
1043 /* Queue the packet to IP for output */ 988 /* Queue the packet to IP for output */
1044 skb->local_df = 1; 989 skb->local_df = 1;
1045#if IS_ENABLED(CONFIG_IPV6) 990 error = ip_queue_xmit(skb, fl);
1046 if (skb->sk->sk_family == PF_INET6)
1047 error = inet6_csk_xmit(skb, NULL);
1048 else
1049#endif
1050 error = ip_queue_xmit(skb, fl);
1051 991
1052 /* Update stats */ 992 /* Update stats */
1053 tstats = &tunnel->stats;
1054 u64_stats_update_begin(&tstats->syncp);
1055 sstats = &session->stats;
1056 u64_stats_update_begin(&sstats->syncp);
1057 if (error >= 0) { 993 if (error >= 0) {
1058 tstats->tx_packets++; 994 tunnel->stats.tx_packets++;
1059 tstats->tx_bytes += len; 995 tunnel->stats.tx_bytes += len;
1060 sstats->tx_packets++; 996 session->stats.tx_packets++;
1061 sstats->tx_bytes += len; 997 session->stats.tx_bytes += len;
1062 } else { 998 } else {
1063 tstats->tx_errors++; 999 tunnel->stats.tx_errors++;
1064 sstats->tx_errors++; 1000 session->stats.tx_errors++;
1065 } 1001 }
1066 u64_stats_update_end(&tstats->syncp);
1067 u64_stats_update_end(&sstats->syncp);
1068 1002
1069 return 0; 1003 return 0;
1070} 1004}
@@ -1086,31 +1020,6 @@ static inline void l2tp_skb_set_owner_w(struct sk_buff *skb, struct sock *sk)
1086 skb->destructor = l2tp_sock_wfree; 1020 skb->destructor = l2tp_sock_wfree;
1087} 1021}
1088 1022
1089#if IS_ENABLED(CONFIG_IPV6)
1090static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1091 int udp_len)
1092{
1093 struct ipv6_pinfo *np = inet6_sk(sk);
1094 struct udphdr *uh = udp_hdr(skb);
1095
1096 if (!skb_dst(skb) || !skb_dst(skb)->dev ||
1097 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1098 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1099 skb->ip_summed = CHECKSUM_UNNECESSARY;
1100 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len,
1101 IPPROTO_UDP, csum);
1102 if (uh->check == 0)
1103 uh->check = CSUM_MANGLED_0;
1104 } else {
1105 skb->ip_summed = CHECKSUM_PARTIAL;
1106 skb->csum_start = skb_transport_header(skb) - skb->head;
1107 skb->csum_offset = offsetof(struct udphdr, check);
1108 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr,
1109 udp_len, IPPROTO_UDP, 0);
1110 }
1111}
1112#endif
1113
1114/* If caller requires the skb to have a ppp header, the header must be 1023/* If caller requires the skb to have a ppp header, the header must be
1115 * inserted in the skb data before calling this function. 1024 * inserted in the skb data before calling this function.
1116 */ 1025 */
@@ -1128,7 +1037,6 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
1128 int headroom; 1037 int headroom;
1129 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0; 1038 int uhlen = (tunnel->encap == L2TP_ENCAPTYPE_UDP) ? sizeof(struct udphdr) : 0;
1130 int udp_len; 1039 int udp_len;
1131 int ret = NET_XMIT_SUCCESS;
1132 1040
1133 /* Check that there's enough headroom in the skb to insert IP, 1041 /* Check that there's enough headroom in the skb to insert IP,
1134 * UDP and L2TP headers. If not enough, expand it to 1042 * UDP and L2TP headers. If not enough, expand it to
@@ -1138,8 +1046,8 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
1138 uhlen + hdr_len; 1046 uhlen + hdr_len;
1139 old_headroom = skb_headroom(skb); 1047 old_headroom = skb_headroom(skb);
1140 if (skb_cow_head(skb, headroom)) { 1048 if (skb_cow_head(skb, headroom)) {
1141 kfree_skb(skb); 1049 dev_kfree_skb(skb);
1142 return NET_XMIT_DROP; 1050 goto abort;
1143 } 1051 }
1144 1052
1145 new_headroom = skb_headroom(skb); 1053 new_headroom = skb_headroom(skb);
@@ -1157,14 +1065,13 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
1157 1065
1158 bh_lock_sock(sk); 1066 bh_lock_sock(sk);
1159 if (sock_owned_by_user(sk)) { 1067 if (sock_owned_by_user(sk)) {
1160 kfree_skb(skb); 1068 dev_kfree_skb(skb);
1161 ret = NET_XMIT_DROP;
1162 goto out_unlock; 1069 goto out_unlock;
1163 } 1070 }
1164 1071
1165 /* Get routing info from the tunnel socket */ 1072 /* Get routing info from the tunnel socket */
1166 skb_dst_drop(skb); 1073 skb_dst_drop(skb);
1167 skb_dst_set(skb, dst_clone(__sk_dst_check(sk, 0))); 1074 skb_dst_set(skb, dst_clone(__sk_dst_get(sk)));
1168 1075
1169 inet = inet_sk(sk); 1076 inet = inet_sk(sk);
1170 fl = &inet->cork.fl; 1077 fl = &inet->cork.fl;
@@ -1181,11 +1088,6 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
1181 uh->check = 0; 1088 uh->check = 0;
1182 1089
1183 /* Calculate UDP checksum if configured to do so */ 1090 /* Calculate UDP checksum if configured to do so */
1184#if IS_ENABLED(CONFIG_IPV6)
1185 if (sk->sk_family == PF_INET6)
1186 l2tp_xmit_ipv6_csum(sk, skb, udp_len);
1187 else
1188#endif
1189 if (sk->sk_no_check == UDP_CSUM_NOXMIT) 1091 if (sk->sk_no_check == UDP_CSUM_NOXMIT)
1190 skb->ip_summed = CHECKSUM_NONE; 1092 skb->ip_summed = CHECKSUM_NONE;
1191 else if ((skb_dst(skb) && skb_dst(skb)->dev) && 1093 else if ((skb_dst(skb) && skb_dst(skb)->dev) &&
@@ -1217,7 +1119,8 @@ int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len
1217out_unlock: 1119out_unlock:
1218 bh_unlock_sock(sk); 1120 bh_unlock_sock(sk);
1219 1121
1220 return ret; 1122abort:
1123 return 0;
1221} 1124}
1222EXPORT_SYMBOL_GPL(l2tp_xmit_skb); 1125EXPORT_SYMBOL_GPL(l2tp_xmit_skb);
1223 1126
@@ -1237,7 +1140,8 @@ static void l2tp_tunnel_destruct(struct sock *sk)
1237 if (tunnel == NULL) 1140 if (tunnel == NULL)
1238 goto end; 1141 goto end;
1239 1142
1240 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing...\n", tunnel->name); 1143 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1144 "%s: closing...\n", tunnel->name);
1241 1145
1242 /* Close all sessions */ 1146 /* Close all sessions */
1243 l2tp_tunnel_closeall(tunnel); 1147 l2tp_tunnel_closeall(tunnel);
@@ -1279,8 +1183,8 @@ static void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel)
1279 1183
1280 BUG_ON(tunnel == NULL); 1184 BUG_ON(tunnel == NULL);
1281 1185
1282 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: closing all sessions...\n", 1186 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1283 tunnel->name); 1187 "%s: closing all sessions...\n", tunnel->name);
1284 1188
1285 write_lock_bh(&tunnel->hlist_lock); 1189 write_lock_bh(&tunnel->hlist_lock);
1286 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) { 1190 for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
@@ -1288,8 +1192,8 @@ again:
1288 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) { 1192 hlist_for_each_safe(walk, tmp, &tunnel->session_hlist[hash]) {
1289 session = hlist_entry(walk, struct l2tp_session, hlist); 1193 session = hlist_entry(walk, struct l2tp_session, hlist);
1290 1194
1291 l2tp_info(session, L2TP_MSG_CONTROL, 1195 PRINTK(session->debug, L2TP_MSG_CONTROL, KERN_INFO,
1292 "%s: closing session\n", session->name); 1196 "%s: closing session\n", session->name);
1293 1197
1294 hlist_del_init(&session->hlist); 1198 hlist_del_init(&session->hlist);
1295 1199
@@ -1342,15 +1246,17 @@ static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
1342 BUG_ON(atomic_read(&tunnel->ref_count) != 0); 1246 BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1343 BUG_ON(tunnel->sock != NULL); 1247 BUG_ON(tunnel->sock != NULL);
1344 1248
1345 l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name); 1249 PRINTK(tunnel->debug, L2TP_MSG_CONTROL, KERN_INFO,
1250 "%s: free...\n", tunnel->name);
1346 1251
1347 /* Remove from tunnel list */ 1252 /* Remove from tunnel list */
1348 spin_lock_bh(&pn->l2tp_tunnel_list_lock); 1253 spin_lock_bh(&pn->l2tp_tunnel_list_lock);
1349 list_del_rcu(&tunnel->list); 1254 list_del_rcu(&tunnel->list);
1350 kfree_rcu(tunnel, rcu);
1351 spin_unlock_bh(&pn->l2tp_tunnel_list_lock); 1255 spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
1256 synchronize_rcu();
1352 1257
1353 atomic_dec(&l2tp_tunnel_count); 1258 atomic_dec(&l2tp_tunnel_count);
1259 kfree(tunnel);
1354} 1260}
1355 1261
1356/* Create a socket for the tunnel, if one isn't set up by 1262/* Create a socket for the tunnel, if one isn't set up by
@@ -1361,69 +1267,31 @@ static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2t
1361{ 1267{
1362 int err = -EINVAL; 1268 int err = -EINVAL;
1363 struct sockaddr_in udp_addr; 1269 struct sockaddr_in udp_addr;
1364#if IS_ENABLED(CONFIG_IPV6)
1365 struct sockaddr_in6 udp6_addr;
1366 struct sockaddr_l2tpip6 ip6_addr;
1367#endif
1368 struct sockaddr_l2tpip ip_addr; 1270 struct sockaddr_l2tpip ip_addr;
1369 struct socket *sock = NULL; 1271 struct socket *sock = NULL;
1370 1272
1371 switch (cfg->encap) { 1273 switch (cfg->encap) {
1372 case L2TP_ENCAPTYPE_UDP: 1274 case L2TP_ENCAPTYPE_UDP:
1373#if IS_ENABLED(CONFIG_IPV6) 1275 err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1374 if (cfg->local_ip6 && cfg->peer_ip6) { 1276 if (err < 0)
1375 err = sock_create(AF_INET6, SOCK_DGRAM, 0, sockp); 1277 goto out;
1376 if (err < 0)
1377 goto out;
1378
1379 sock = *sockp;
1380
1381 memset(&udp6_addr, 0, sizeof(udp6_addr));
1382 udp6_addr.sin6_family = AF_INET6;
1383 memcpy(&udp6_addr.sin6_addr, cfg->local_ip6,
1384 sizeof(udp6_addr.sin6_addr));
1385 udp6_addr.sin6_port = htons(cfg->local_udp_port);
1386 err = kernel_bind(sock, (struct sockaddr *) &udp6_addr,
1387 sizeof(udp6_addr));
1388 if (err < 0)
1389 goto out;
1390
1391 udp6_addr.sin6_family = AF_INET6;
1392 memcpy(&udp6_addr.sin6_addr, cfg->peer_ip6,
1393 sizeof(udp6_addr.sin6_addr));
1394 udp6_addr.sin6_port = htons(cfg->peer_udp_port);
1395 err = kernel_connect(sock,
1396 (struct sockaddr *) &udp6_addr,
1397 sizeof(udp6_addr), 0);
1398 if (err < 0)
1399 goto out;
1400 } else
1401#endif
1402 {
1403 err = sock_create(AF_INET, SOCK_DGRAM, 0, sockp);
1404 if (err < 0)
1405 goto out;
1406 1278
1407 sock = *sockp; 1279 sock = *sockp;
1408 1280
1409 memset(&udp_addr, 0, sizeof(udp_addr)); 1281 memset(&udp_addr, 0, sizeof(udp_addr));
1410 udp_addr.sin_family = AF_INET; 1282 udp_addr.sin_family = AF_INET;
1411 udp_addr.sin_addr = cfg->local_ip; 1283 udp_addr.sin_addr = cfg->local_ip;
1412 udp_addr.sin_port = htons(cfg->local_udp_port); 1284 udp_addr.sin_port = htons(cfg->local_udp_port);
1413 err = kernel_bind(sock, (struct sockaddr *) &udp_addr, 1285 err = kernel_bind(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
1414 sizeof(udp_addr)); 1286 if (err < 0)
1415 if (err < 0) 1287 goto out;
1416 goto out;
1417 1288
1418 udp_addr.sin_family = AF_INET; 1289 udp_addr.sin_family = AF_INET;
1419 udp_addr.sin_addr = cfg->peer_ip; 1290 udp_addr.sin_addr = cfg->peer_ip;
1420 udp_addr.sin_port = htons(cfg->peer_udp_port); 1291 udp_addr.sin_port = htons(cfg->peer_udp_port);
1421 err = kernel_connect(sock, 1292 err = kernel_connect(sock, (struct sockaddr *) &udp_addr, sizeof(udp_addr), 0);
1422 (struct sockaddr *) &udp_addr, 1293 if (err < 0)
1423 sizeof(udp_addr), 0); 1294 goto out;
1424 if (err < 0)
1425 goto out;
1426 }
1427 1295
1428 if (!cfg->use_udp_checksums) 1296 if (!cfg->use_udp_checksums)
1429 sock->sk->sk_no_check = UDP_CSUM_NOXMIT; 1297 sock->sk->sk_no_check = UDP_CSUM_NOXMIT;
@@ -1431,61 +1299,27 @@ static int l2tp_tunnel_sock_create(u32 tunnel_id, u32 peer_tunnel_id, struct l2t
1431 break; 1299 break;
1432 1300
1433 case L2TP_ENCAPTYPE_IP: 1301 case L2TP_ENCAPTYPE_IP:
1434#if IS_ENABLED(CONFIG_IPV6) 1302 err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP, sockp);
1435 if (cfg->local_ip6 && cfg->peer_ip6) { 1303 if (err < 0)
1436 err = sock_create(AF_INET6, SOCK_DGRAM, IPPROTO_L2TP, 1304 goto out;
1437 sockp);
1438 if (err < 0)
1439 goto out;
1440
1441 sock = *sockp;
1442
1443 memset(&ip6_addr, 0, sizeof(ip6_addr));
1444 ip6_addr.l2tp_family = AF_INET6;
1445 memcpy(&ip6_addr.l2tp_addr, cfg->local_ip6,
1446 sizeof(ip6_addr.l2tp_addr));
1447 ip6_addr.l2tp_conn_id = tunnel_id;
1448 err = kernel_bind(sock, (struct sockaddr *) &ip6_addr,
1449 sizeof(ip6_addr));
1450 if (err < 0)
1451 goto out;
1452 1305
1453 ip6_addr.l2tp_family = AF_INET6; 1306 sock = *sockp;
1454 memcpy(&ip6_addr.l2tp_addr, cfg->peer_ip6,
1455 sizeof(ip6_addr.l2tp_addr));
1456 ip6_addr.l2tp_conn_id = peer_tunnel_id;
1457 err = kernel_connect(sock,
1458 (struct sockaddr *) &ip6_addr,
1459 sizeof(ip6_addr), 0);
1460 if (err < 0)
1461 goto out;
1462 } else
1463#endif
1464 {
1465 err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_L2TP,
1466 sockp);
1467 if (err < 0)
1468 goto out;
1469 1307
1470 sock = *sockp; 1308 memset(&ip_addr, 0, sizeof(ip_addr));
1309 ip_addr.l2tp_family = AF_INET;
1310 ip_addr.l2tp_addr = cfg->local_ip;
1311 ip_addr.l2tp_conn_id = tunnel_id;
1312 err = kernel_bind(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr));
1313 if (err < 0)
1314 goto out;
1471 1315
1472 memset(&ip_addr, 0, sizeof(ip_addr)); 1316 ip_addr.l2tp_family = AF_INET;
1473 ip_addr.l2tp_family = AF_INET; 1317 ip_addr.l2tp_addr = cfg->peer_ip;
1474 ip_addr.l2tp_addr = cfg->local_ip; 1318 ip_addr.l2tp_conn_id = peer_tunnel_id;
1475 ip_addr.l2tp_conn_id = tunnel_id; 1319 err = kernel_connect(sock, (struct sockaddr *) &ip_addr, sizeof(ip_addr), 0);
1476 err = kernel_bind(sock, (struct sockaddr *) &ip_addr, 1320 if (err < 0)
1477 sizeof(ip_addr)); 1321 goto out;
1478 if (err < 0)
1479 goto out;
1480 1322
1481 ip_addr.l2tp_family = AF_INET;
1482 ip_addr.l2tp_addr = cfg->peer_ip;
1483 ip_addr.l2tp_conn_id = peer_tunnel_id;
1484 err = kernel_connect(sock, (struct sockaddr *) &ip_addr,
1485 sizeof(ip_addr), 0);
1486 if (err < 0)
1487 goto out;
1488 }
1489 break; 1323 break;
1490 1324
1491 default: 1325 default:
@@ -1501,8 +1335,6 @@ out:
1501 return err; 1335 return err;
1502} 1336}
1503 1337
1504static struct lock_class_key l2tp_socket_class;
1505
1506int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp) 1338int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp)
1507{ 1339{
1508 struct l2tp_tunnel *tunnel = NULL; 1340 struct l2tp_tunnel *tunnel = NULL;
@@ -1524,7 +1356,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
1524 err = -EBADF; 1356 err = -EBADF;
1525 sock = sockfd_lookup(fd, &err); 1357 sock = sockfd_lookup(fd, &err);
1526 if (!sock) { 1358 if (!sock) {
1527 pr_err("tunl %hu: sockfd_lookup(fd=%d) returned %d\n", 1359 printk(KERN_ERR "tunl %hu: sockfd_lookup(fd=%d) returned %d\n",
1528 tunnel_id, fd, err); 1360 tunnel_id, fd, err);
1529 goto err; 1361 goto err;
1530 } 1362 }
@@ -1540,7 +1372,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
1540 case L2TP_ENCAPTYPE_UDP: 1372 case L2TP_ENCAPTYPE_UDP:
1541 err = -EPROTONOSUPPORT; 1373 err = -EPROTONOSUPPORT;
1542 if (sk->sk_protocol != IPPROTO_UDP) { 1374 if (sk->sk_protocol != IPPROTO_UDP) {
1543 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1375 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1544 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP); 1376 tunnel_id, fd, sk->sk_protocol, IPPROTO_UDP);
1545 goto err; 1377 goto err;
1546 } 1378 }
@@ -1548,7 +1380,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
1548 case L2TP_ENCAPTYPE_IP: 1380 case L2TP_ENCAPTYPE_IP:
1549 err = -EPROTONOSUPPORT; 1381 err = -EPROTONOSUPPORT;
1550 if (sk->sk_protocol != IPPROTO_L2TP) { 1382 if (sk->sk_protocol != IPPROTO_L2TP) {
1551 pr_err("tunl %hu: fd %d wrong protocol, got %d, expected %d\n", 1383 printk(KERN_ERR "tunl %hu: fd %d wrong protocol, got %d, expected %d\n",
1552 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP); 1384 tunnel_id, fd, sk->sk_protocol, IPPROTO_L2TP);
1553 goto err; 1385 goto err;
1554 } 1386 }
@@ -1591,12 +1423,6 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
1591 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */ 1423 /* Mark socket as an encapsulation socket. See net/ipv4/udp.c */
1592 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP; 1424 udp_sk(sk)->encap_type = UDP_ENCAP_L2TPINUDP;
1593 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv; 1425 udp_sk(sk)->encap_rcv = l2tp_udp_encap_recv;
1594#if IS_ENABLED(CONFIG_IPV6)
1595 if (sk->sk_family == PF_INET6)
1596 udpv6_encap_enable();
1597 else
1598#endif
1599 udp_encap_enable();
1600 } 1426 }
1601 1427
1602 sk->sk_user_data = tunnel; 1428 sk->sk_user_data = tunnel;
@@ -1607,8 +1433,6 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
1607 tunnel->old_sk_destruct = sk->sk_destruct; 1433 tunnel->old_sk_destruct = sk->sk_destruct;
1608 sk->sk_destruct = &l2tp_tunnel_destruct; 1434 sk->sk_destruct = &l2tp_tunnel_destruct;
1609 tunnel->sock = sk; 1435 tunnel->sock = sk;
1610 lockdep_set_class_and_name(&sk->sk_lock.slock, &l2tp_socket_class, "l2tp_sock");
1611
1612 sk->sk_allocation = GFP_ATOMIC; 1436 sk->sk_allocation = GFP_ATOMIC;
1613 1437
1614 /* Add tunnel to our list */ 1438 /* Add tunnel to our list */
@@ -1752,7 +1576,7 @@ struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunn
1752 1576
1753 session->session_id = session_id; 1577 session->session_id = session_id;
1754 session->peer_session_id = peer_session_id; 1578 session->peer_session_id = peer_session_id;
1755 session->nr = 0; 1579 session->nr = 1;
1756 1580
1757 sprintf(&session->name[0], "sess %u/%u", 1581 sprintf(&session->name[0], "sess %u/%u",
1758 tunnel->tunnel_id, session->session_id); 1582 tunnel->tunnel_id, session->session_id);
@@ -1858,7 +1682,7 @@ static int __init l2tp_init(void)
1858 if (rc) 1682 if (rc)
1859 goto out; 1683 goto out;
1860 1684
1861 pr_info("L2TP core driver, %s\n", L2TP_DRV_VERSION); 1685 printk(KERN_INFO "L2TP core driver, %s\n", L2TP_DRV_VERSION);
1862 1686
1863out: 1687out:
1864 return rc; 1688 return rc;