1414#include <linux/socket.h>
1515#include <linux/fcntl.h>
1616#include <linux/stat.h>
17+ #include <linux/string.h>
1718#include <asm/uaccess.h>
1819#include <linux/in.h>
1920#include <linux/net.h>
@@ -55,10 +56,11 @@ static int _send(struct socket *sock, const void *buff, int len)
5556struct ncp_request_reply {
5657 struct list_head req ;
5758 wait_queue_head_t wq ;
58- struct ncp_reply_header * reply_buf ;
59+ atomic_t refs ;
60+ unsigned char * reply_buf ;
5961 size_t datalen ;
6062 int result ;
61- enum { RQ_DONE , RQ_INPROGRESS , RQ_QUEUED , RQ_IDLE } status ;
63+ enum { RQ_DONE , RQ_INPROGRESS , RQ_QUEUED , RQ_IDLE , RQ_ABANDONED } status ;
6264 struct kvec * tx_ciov ;
6365 size_t tx_totallen ;
6466 size_t tx_iovlen ;
@@ -67,6 +69,32 @@ struct ncp_request_reply {
6769 u_int32_t sign [6 ];
6870};
6971
72+ static inline struct ncp_request_reply * ncp_alloc_req (void )
73+ {
74+ struct ncp_request_reply * req ;
75+
76+ req = kmalloc (sizeof (struct ncp_request_reply ), GFP_KERNEL );
77+ if (!req )
78+ return NULL ;
79+
80+ init_waitqueue_head (& req -> wq );
81+ atomic_set (& req -> refs , (1 ));
82+ req -> status = RQ_IDLE ;
83+
84+ return req ;
85+ }
86+
87+ static void ncp_req_get (struct ncp_request_reply * req )
88+ {
89+ atomic_inc (& req -> refs );
90+ }
91+
92+ static void ncp_req_put (struct ncp_request_reply * req )
93+ {
94+ if (atomic_dec_and_test (& req -> refs ))
95+ kfree (req );
96+ }
97+
7098void ncp_tcp_data_ready (struct sock * sk , int len )
7199{
72100 struct ncp_server * server = sk -> sk_user_data ;
@@ -101,14 +129,17 @@ void ncpdgram_timeout_call(unsigned long v)
101129 schedule_work (& server -> timeout_tq );
102130}
103131
104- static inline void ncp_finish_request (struct ncp_request_reply * req , int result )
132+ static inline void ncp_finish_request (struct ncp_server * server , struct ncp_request_reply * req , int result )
105133{
106134 req -> result = result ;
135+ if (req -> status != RQ_ABANDONED )
136+ memcpy (req -> reply_buf , server -> rxbuf , req -> datalen );
107137 req -> status = RQ_DONE ;
108138 wake_up_all (& req -> wq );
139+ ncp_req_put (req );
109140}
110141
111- static void __abort_ncp_connection (struct ncp_server * server , struct ncp_request_reply * aborted , int err )
142+ static void __abort_ncp_connection (struct ncp_server * server )
112143{
113144 struct ncp_request_reply * req ;
114145
@@ -118,31 +149,19 @@ static void __abort_ncp_connection(struct ncp_server *server, struct ncp_request
118149 req = list_entry (server -> tx .requests .next , struct ncp_request_reply , req );
119150
120151 list_del_init (& req -> req );
121- if (req == aborted ) {
122- ncp_finish_request (req , err );
123- } else {
124- ncp_finish_request (req , - EIO );
125- }
152+ ncp_finish_request (server , req , - EIO );
126153 }
127154 req = server -> rcv .creq ;
128155 if (req ) {
129156 server -> rcv .creq = NULL ;
130- if (req == aborted ) {
131- ncp_finish_request (req , err );
132- } else {
133- ncp_finish_request (req , - EIO );
134- }
157+ ncp_finish_request (server , req , - EIO );
135158 server -> rcv .ptr = NULL ;
136159 server -> rcv .state = 0 ;
137160 }
138161 req = server -> tx .creq ;
139162 if (req ) {
140163 server -> tx .creq = NULL ;
141- if (req == aborted ) {
142- ncp_finish_request (req , err );
143- } else {
144- ncp_finish_request (req , - EIO );
145- }
164+ ncp_finish_request (server , req , - EIO );
146165 }
147166}
148167
@@ -160,10 +179,12 @@ static inline void __ncp_abort_request(struct ncp_server *server, struct ncp_req
160179 break ;
161180 case RQ_QUEUED :
162181 list_del_init (& req -> req );
163- ncp_finish_request (req , err );
182+ ncp_finish_request (server , req , err );
164183 break ;
165184 case RQ_INPROGRESS :
166- __abort_ncp_connection (server , req , err );
185+ req -> status = RQ_ABANDONED ;
186+ break ;
187+ case RQ_ABANDONED :
167188 break ;
168189 }
169190}
@@ -177,7 +198,7 @@ static inline void ncp_abort_request(struct ncp_server *server, struct ncp_reque
177198
178199static inline void __ncptcp_abort (struct ncp_server * server )
179200{
180- __abort_ncp_connection (server , NULL , 0 );
201+ __abort_ncp_connection (server );
181202}
182203
183204static int ncpdgram_send (struct socket * sock , struct ncp_request_reply * req )
@@ -294,6 +315,11 @@ static void ncptcp_start_request(struct ncp_server *server, struct ncp_request_r
294315
295316static inline void __ncp_start_request (struct ncp_server * server , struct ncp_request_reply * req )
296317{
318+ /* we copy the data so that we do not depend on the caller
319+ staying alive */
320+ memcpy (server -> txbuf , req -> tx_iov [1 ].iov_base , req -> tx_iov [1 ].iov_len );
321+ req -> tx_iov [1 ].iov_base = server -> txbuf ;
322+
297323 if (server -> ncp_sock -> type == SOCK_STREAM )
298324 ncptcp_start_request (server , req );
299325 else
@@ -308,6 +334,7 @@ static int ncp_add_request(struct ncp_server *server, struct ncp_request_reply *
308334 printk (KERN_ERR "ncpfs: tcp: Server died\n" );
309335 return - EIO ;
310336 }
337+ ncp_req_get (req );
311338 if (server -> tx .creq || server -> rcv .creq ) {
312339 req -> status = RQ_QUEUED ;
313340 list_add_tail (& req -> req , & server -> tx .requests );
@@ -409,7 +436,7 @@ void ncpdgram_rcv_proc(struct work_struct *work)
409436 server -> timeout_last = NCP_MAX_RPC_TIMEOUT ;
410437 mod_timer (& server -> timeout_tm , jiffies + NCP_MAX_RPC_TIMEOUT );
411438 } else if (reply .type == NCP_REPLY ) {
412- result = _recv (sock , ( void * ) req -> reply_buf , req -> datalen , MSG_DONTWAIT );
439+ result = _recv (sock , server -> rxbuf , req -> datalen , MSG_DONTWAIT );
413440#ifdef CONFIG_NCPFS_PACKET_SIGNING
414441 if (result >= 0 && server -> sign_active && req -> tx_type != NCP_DEALLOC_SLOT_REQUEST ) {
415442 if (result < 8 + 8 ) {
@@ -419,7 +446,7 @@ void ncpdgram_rcv_proc(struct work_struct *work)
419446
420447 result -= 8 ;
421448 hdrl = sock -> sk -> sk_family == AF_INET ? 8 : 6 ;
422- if (sign_verify_reply (server , (( char * ) req -> reply_buf ) + hdrl , result - hdrl , cpu_to_le32 (result ), (( char * ) req -> reply_buf ) + result )) {
449+ if (sign_verify_reply (server , server -> rxbuf + hdrl , result - hdrl , cpu_to_le32 (result ), server -> rxbuf + result )) {
423450 printk (KERN_INFO "ncpfs: Signature violation\n" );
424451 result = - EIO ;
425452 }
@@ -428,7 +455,7 @@ void ncpdgram_rcv_proc(struct work_struct *work)
428455#endif
429456 del_timer (& server -> timeout_tm );
430457 server -> rcv .creq = NULL ;
431- ncp_finish_request (req , result );
458+ ncp_finish_request (server , req , result );
432459 __ncp_next_request (server );
433460 mutex_unlock (& server -> rcv .creq_mutex );
434461 continue ;
@@ -478,12 +505,6 @@ void ncpdgram_timeout_proc(struct work_struct *work)
478505 mutex_unlock (& server -> rcv .creq_mutex );
479506}
480507
481- static inline void ncp_init_req (struct ncp_request_reply * req )
482- {
483- init_waitqueue_head (& req -> wq );
484- req -> status = RQ_IDLE ;
485- }
486-
487508static int do_tcp_rcv (struct ncp_server * server , void * buffer , size_t len )
488509{
489510 int result ;
@@ -601,8 +622,8 @@ skipdata:;
601622 goto skipdata ;
602623 }
603624 req -> datalen = datalen - 8 ;
604- req -> reply_buf -> type = NCP_REPLY ;
605- server -> rcv .ptr = ( unsigned char * )( req -> reply_buf ) + 2 ;
625+ (( struct ncp_reply_header * ) server -> rxbuf ) -> type = NCP_REPLY ;
626+ server -> rcv .ptr = server -> rxbuf + 2 ;
606627 server -> rcv .len = datalen - 10 ;
607628 server -> rcv .state = 1 ;
608629 break ;
@@ -615,27 +636,27 @@ skipdata:;
615636 case 1 :
616637 req = server -> rcv .creq ;
617638 if (req -> tx_type != NCP_ALLOC_SLOT_REQUEST ) {
618- if (req -> reply_buf -> sequence != server -> sequence ) {
639+ if ((( struct ncp_reply_header * ) server -> rxbuf ) -> sequence != server -> sequence ) {
619640 printk (KERN_ERR "ncpfs: tcp: Bad sequence number\n" );
620641 __ncp_abort_request (server , req , - EIO );
621642 return - EIO ;
622643 }
623- if ((req -> reply_buf -> conn_low | (req -> reply_buf -> conn_high << 8 )) != server -> connection ) {
644+ if (((( struct ncp_reply_header * ) server -> rxbuf ) -> conn_low | ((( struct ncp_reply_header * ) server -> rxbuf ) -> conn_high << 8 )) != server -> connection ) {
624645 printk (KERN_ERR "ncpfs: tcp: Connection number mismatch\n" );
625646 __ncp_abort_request (server , req , - EIO );
626647 return - EIO ;
627648 }
628649 }
629650#ifdef CONFIG_NCPFS_PACKET_SIGNING
630651 if (server -> sign_active && req -> tx_type != NCP_DEALLOC_SLOT_REQUEST ) {
631- if (sign_verify_reply (server , ( unsigned char * )( req -> reply_buf ) + 6 , req -> datalen - 6 , cpu_to_be32 (req -> datalen + 16 ), & server -> rcv .buf .type )) {
652+ if (sign_verify_reply (server , server -> rxbuf + 6 , req -> datalen - 6 , cpu_to_be32 (req -> datalen + 16 ), & server -> rcv .buf .type )) {
632653 printk (KERN_ERR "ncpfs: tcp: Signature violation\n" );
633654 __ncp_abort_request (server , req , - EIO );
634655 return - EIO ;
635656 }
636657 }
637658#endif
638- ncp_finish_request (req , req -> datalen );
659+ ncp_finish_request (server , req , req -> datalen );
639660 nextreq :;
640661 __ncp_next_request (server );
641662 case 2 :
@@ -645,7 +666,7 @@ skipdata:;
645666 server -> rcv .state = 0 ;
646667 break ;
647668 case 3 :
648- ncp_finish_request (server -> rcv .creq , - EIO );
669+ ncp_finish_request (server , server -> rcv .creq , - EIO );
649670 goto nextreq ;
650671 case 5 :
651672 info_server (server , 0 , server -> unexpected_packet .data , server -> unexpected_packet .len );
@@ -675,28 +696,39 @@ void ncp_tcp_tx_proc(struct work_struct *work)
675696}
676697
677698static int do_ncp_rpc_call (struct ncp_server * server , int size ,
678- struct ncp_reply_header * reply_buf , int max_reply_size )
699+ unsigned char * reply_buf , int max_reply_size )
679700{
680701 int result ;
681- struct ncp_request_reply req ;
682-
683- ncp_init_req (& req );
684- req .reply_buf = reply_buf ;
685- req .datalen = max_reply_size ;
686- req .tx_iov [1 ].iov_base = server -> packet ;
687- req .tx_iov [1 ].iov_len = size ;
688- req .tx_iovlen = 1 ;
689- req .tx_totallen = size ;
690- req .tx_type = * (u_int16_t * )server -> packet ;
691-
692- result = ncp_add_request (server , & req );
693- if (result < 0 ) {
694- return result ;
695- }
696- if (wait_event_interruptible (req .wq , req .status == RQ_DONE )) {
697- ncp_abort_request (server , & req , - EIO );
702+ struct ncp_request_reply * req ;
703+
704+ req = ncp_alloc_req ();
705+ if (!req )
706+ return - ENOMEM ;
707+
708+ req -> reply_buf = reply_buf ;
709+ req -> datalen = max_reply_size ;
710+ req -> tx_iov [1 ].iov_base = server -> packet ;
711+ req -> tx_iov [1 ].iov_len = size ;
712+ req -> tx_iovlen = 1 ;
713+ req -> tx_totallen = size ;
714+ req -> tx_type = * (u_int16_t * )server -> packet ;
715+
716+ result = ncp_add_request (server , req );
717+ if (result < 0 )
718+ goto out ;
719+
720+ if (wait_event_interruptible (req -> wq , req -> status == RQ_DONE )) {
721+ ncp_abort_request (server , req , - EINTR );
722+ result = - EINTR ;
723+ goto out ;
698724 }
699- return req .result ;
725+
726+ result = req -> result ;
727+
728+ out :
729+ ncp_req_put (req );
730+
731+ return result ;
700732}
701733
702734/*
@@ -751,11 +783,6 @@ static int ncp_do_request(struct ncp_server *server, int size,
751783
752784 DDPRINTK ("do_ncp_rpc_call returned %d\n" , result );
753785
754- if (result < 0 ) {
755- /* There was a problem with I/O, so the connections is
756- * no longer usable. */
757- ncp_invalidate_conn (server );
758- }
759786 return result ;
760787}
761788
0 commit comments