@@ -650,54 +650,22 @@ static int pppol2tp_tunnel_mtu(const struct l2tp_tunnel *tunnel)
650
650
return mtu - PPPOL2TP_HEADER_OVERHEAD ;
651
651
}
652
652
653
- /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
654
- */
655
- static int pppol2tp_connect (struct socket * sock , struct sockaddr * uservaddr ,
656
- int sockaddr_len , int flags )
653
+ static struct l2tp_tunnel * pppol2tp_tunnel_get (struct net * net ,
654
+ const struct l2tp_connect_info * info ,
655
+ bool * new_tunnel )
657
656
{
658
- struct sock * sk = sock -> sk ;
659
- struct pppox_sock * po = pppox_sk (sk );
660
- struct l2tp_session * session = NULL ;
661
- struct l2tp_connect_info info ;
662
657
struct l2tp_tunnel * tunnel ;
663
- struct pppol2tp_session * ps ;
664
- struct l2tp_session_cfg cfg = { 0 , };
665
- bool drop_refcnt = false;
666
- bool drop_tunnel = false;
667
- bool new_session = false;
668
- bool new_tunnel = false;
669
658
int error ;
670
659
671
- error = pppol2tp_sockaddr_get_info (uservaddr , sockaddr_len , & info );
672
- if (error < 0 )
673
- return error ;
660
+ * new_tunnel = false;
674
661
675
- lock_sock (sk );
676
-
677
- /* Check for already bound sockets */
678
- error = - EBUSY ;
679
- if (sk -> sk_state & PPPOX_CONNECTED )
680
- goto end ;
681
-
682
- /* We don't supporting rebinding anyway */
683
- error = - EALREADY ;
684
- if (sk -> sk_user_data )
685
- goto end ; /* socket is already attached */
686
-
687
- /* Don't bind if tunnel_id is 0 */
688
- error = - EINVAL ;
689
- if (!info .tunnel_id )
690
- goto end ;
691
-
692
- tunnel = l2tp_tunnel_get (sock_net (sk ), info .tunnel_id );
693
- if (tunnel )
694
- drop_tunnel = true;
662
+ tunnel = l2tp_tunnel_get (net , info -> tunnel_id );
695
663
696
664
/* Special case: create tunnel context if session_id and
697
665
* peer_session_id is 0. Otherwise look up tunnel using supplied
698
666
* tunnel id.
699
667
*/
700
- if (!info . session_id && !info . peer_session_id ) {
668
+ if (!info -> session_id && !info -> peer_session_id ) {
701
669
if (!tunnel ) {
702
670
struct l2tp_tunnel_cfg tcfg = {
703
671
.encap = L2TP_ENCAPTYPE_UDP ,
@@ -706,40 +674,82 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
706
674
/* Prevent l2tp_tunnel_register() from trying to set up
707
675
* a kernel socket.
708
676
*/
709
- if (info .fd < 0 ) {
710
- error = - EBADF ;
711
- goto end ;
712
- }
677
+ if (info -> fd < 0 )
678
+ return ERR_PTR (- EBADF );
713
679
714
- error = l2tp_tunnel_create (info . fd ,
715
- info . version ,
716
- info . tunnel_id ,
717
- info . peer_tunnel_id , & tcfg ,
680
+ error = l2tp_tunnel_create (info -> fd ,
681
+ info -> version ,
682
+ info -> tunnel_id ,
683
+ info -> peer_tunnel_id , & tcfg ,
718
684
& tunnel );
719
685
if (error < 0 )
720
- goto end ;
686
+ return ERR_PTR ( error ) ;
721
687
722
688
l2tp_tunnel_inc_refcount (tunnel );
723
- error = l2tp_tunnel_register (tunnel , sock_net (sk ),
724
- & tcfg );
689
+ error = l2tp_tunnel_register (tunnel , net , & tcfg );
725
690
if (error < 0 ) {
726
691
kfree (tunnel );
727
- goto end ;
692
+ return ERR_PTR ( error ) ;
728
693
}
729
- drop_tunnel = true;
730
- new_tunnel = true;
694
+
695
+ * new_tunnel = true;
731
696
}
732
697
} else {
733
698
/* Error if we can't find the tunnel */
734
- error = - ENOENT ;
735
699
if (!tunnel )
736
- goto end ;
700
+ return ERR_PTR ( - ENOENT ) ;
737
701
738
702
/* Error if socket is not prepped */
739
- if (!tunnel -> sock )
740
- goto end ;
703
+ if (!tunnel -> sock ) {
704
+ l2tp_tunnel_dec_refcount (tunnel );
705
+ return ERR_PTR (- ENOENT );
706
+ }
741
707
}
742
708
709
+ return tunnel ;
710
+ }
711
+
712
+ /* connect() handler. Attach a PPPoX socket to a tunnel UDP socket
713
+ */
714
+ static int pppol2tp_connect (struct socket * sock , struct sockaddr * uservaddr ,
715
+ int sockaddr_len , int flags )
716
+ {
717
+ struct sock * sk = sock -> sk ;
718
+ struct pppox_sock * po = pppox_sk (sk );
719
+ struct l2tp_session * session = NULL ;
720
+ struct l2tp_connect_info info ;
721
+ struct l2tp_tunnel * tunnel ;
722
+ struct pppol2tp_session * ps ;
723
+ struct l2tp_session_cfg cfg = { 0 , };
724
+ bool drop_refcnt = false;
725
+ bool new_session = false;
726
+ bool new_tunnel = false;
727
+ int error ;
728
+
729
+ error = pppol2tp_sockaddr_get_info (uservaddr , sockaddr_len , & info );
730
+ if (error < 0 )
731
+ return error ;
732
+
733
+ /* Don't bind if tunnel_id is 0 */
734
+ if (!info .tunnel_id )
735
+ return - EINVAL ;
736
+
737
+ tunnel = pppol2tp_tunnel_get (sock_net (sk ), & info , & new_tunnel );
738
+ if (IS_ERR (tunnel ))
739
+ return PTR_ERR (tunnel );
740
+
741
+ lock_sock (sk );
742
+
743
+ /* Check for already bound sockets */
744
+ error = - EBUSY ;
745
+ if (sk -> sk_state & PPPOX_CONNECTED )
746
+ goto end ;
747
+
748
+ /* We don't supporting rebinding anyway */
749
+ error = - EALREADY ;
750
+ if (sk -> sk_user_data )
751
+ goto end ; /* socket is already attached */
752
+
743
753
if (tunnel -> peer_tunnel_id == 0 )
744
754
tunnel -> peer_tunnel_id = info .peer_tunnel_id ;
745
755
@@ -840,8 +850,7 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
840
850
}
841
851
if (drop_refcnt )
842
852
l2tp_session_dec_refcount (session );
843
- if (drop_tunnel )
844
- l2tp_tunnel_dec_refcount (tunnel );
853
+ l2tp_tunnel_dec_refcount (tunnel );
845
854
release_sock (sk );
846
855
847
856
return error ;
0 commit comments