CIFS: Move trans2 processing to ops struct
[profile/ivi/kernel-x86-ivi.git] / fs / cifs / smb1ops.c
1 /*
2  *  SMB1 (CIFS) version specific operations
3  *
4  *  Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
5  *
6  *  This library is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License v2 as published
8  *  by the Free Software Foundation.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *  the GNU Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public License
16  *  along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 #include "cifsglob.h"
21 #include "cifsproto.h"
22 #include "cifs_debug.h"
23 #include "cifspdu.h"
24
25 /*
26  * An NT cancel request header looks just like the original request except:
27  *
28  * The Command is SMB_COM_NT_CANCEL
29  * The WordCount is zeroed out
30  * The ByteCount is zeroed out
31  *
32  * This function mangles an existing request buffer into a
33  * SMB_COM_NT_CANCEL request and then sends it.
34  */
35 static int
36 send_nt_cancel(struct TCP_Server_Info *server, void *buf,
37                struct mid_q_entry *mid)
38 {
39         int rc = 0;
40         struct smb_hdr *in_buf = (struct smb_hdr *)buf;
41
42         /* -4 for RFC1001 length and +2 for BCC field */
43         in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4  + 2);
44         in_buf->Command = SMB_COM_NT_CANCEL;
45         in_buf->WordCount = 0;
46         put_bcc(0, in_buf);
47
48         mutex_lock(&server->srv_mutex);
49         rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
50         if (rc) {
51                 mutex_unlock(&server->srv_mutex);
52                 return rc;
53         }
54         rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
55         mutex_unlock(&server->srv_mutex);
56
57         cFYI(1, "issued NT_CANCEL for mid %u, rc = %d",
58                 in_buf->Mid, rc);
59
60         return rc;
61 }
62
63 static bool
64 cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
65 {
66         return ob1->netfid == ob2->netfid;
67 }
68
69 static unsigned int
70 cifs_read_data_offset(char *buf)
71 {
72         READ_RSP *rsp = (READ_RSP *)buf;
73         return le16_to_cpu(rsp->DataOffset);
74 }
75
76 static unsigned int
77 cifs_read_data_length(char *buf)
78 {
79         READ_RSP *rsp = (READ_RSP *)buf;
80         return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
81                le16_to_cpu(rsp->DataLength);
82 }
83
84 static struct mid_q_entry *
85 cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
86 {
87         struct smb_hdr *buf = (struct smb_hdr *)buffer;
88         struct mid_q_entry *mid;
89
90         spin_lock(&GlobalMid_Lock);
91         list_for_each_entry(mid, &server->pending_mid_q, qhead) {
92                 if (mid->mid == buf->Mid &&
93                     mid->mid_state == MID_REQUEST_SUBMITTED &&
94                     le16_to_cpu(mid->command) == buf->Command) {
95                         spin_unlock(&GlobalMid_Lock);
96                         return mid;
97                 }
98         }
99         spin_unlock(&GlobalMid_Lock);
100         return NULL;
101 }
102
103 static void
104 cifs_add_credits(struct TCP_Server_Info *server, const unsigned int add)
105 {
106         spin_lock(&server->req_lock);
107         server->credits += add;
108         server->in_flight--;
109         spin_unlock(&server->req_lock);
110         wake_up(&server->request_q);
111 }
112
113 static void
114 cifs_set_credits(struct TCP_Server_Info *server, const int val)
115 {
116         spin_lock(&server->req_lock);
117         server->credits = val;
118         server->oplocks = val > 1 ? enable_oplocks : false;
119         spin_unlock(&server->req_lock);
120 }
121
122 static int *
123 cifs_get_credits_field(struct TCP_Server_Info *server)
124 {
125         return &server->credits;
126 }
127
128 /*
129  * Find a free multiplex id (SMB mid). Otherwise there could be
130  * mid collisions which might cause problems, demultiplexing the
131  * wrong response to this request. Multiplex ids could collide if
132  * one of a series requests takes much longer than the others, or
133  * if a very large number of long lived requests (byte range
134  * locks or FindNotify requests) are pending. No more than
135  * 64K-1 requests can be outstanding at one time. If no
136  * mids are available, return zero. A future optimization
137  * could make the combination of mids and uid the key we use
138  * to demultiplex on (rather than mid alone).
139  * In addition to the above check, the cifs demultiplex
140  * code already used the command code as a secondary
141  * check of the frame and if signing is negotiated the
142  * response would be discarded if the mid were the same
143  * but the signature was wrong. Since the mid is not put in the
144  * pending queue until later (when it is about to be dispatched)
145  * we do have to limit the number of outstanding requests
146  * to somewhat less than 64K-1 although it is hard to imagine
147  * so many threads being in the vfs at one time.
148  */
149 static __u64
150 cifs_get_next_mid(struct TCP_Server_Info *server)
151 {
152         __u64 mid = 0;
153         __u16 last_mid, cur_mid;
154         bool collision;
155
156         spin_lock(&GlobalMid_Lock);
157
158         /* mid is 16 bit only for CIFS/SMB */
159         cur_mid = (__u16)((server->CurrentMid) & 0xffff);
160         /* we do not want to loop forever */
161         last_mid = cur_mid;
162         cur_mid++;
163
164         /*
165          * This nested loop looks more expensive than it is.
166          * In practice the list of pending requests is short,
167          * fewer than 50, and the mids are likely to be unique
168          * on the first pass through the loop unless some request
169          * takes longer than the 64 thousand requests before it
170          * (and it would also have to have been a request that
171          * did not time out).
172          */
173         while (cur_mid != last_mid) {
174                 struct mid_q_entry *mid_entry;
175                 unsigned int num_mids;
176
177                 collision = false;
178                 if (cur_mid == 0)
179                         cur_mid++;
180
181                 num_mids = 0;
182                 list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
183                         ++num_mids;
184                         if (mid_entry->mid == cur_mid &&
185                             mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
186                                 /* This mid is in use, try a different one */
187                                 collision = true;
188                                 break;
189                         }
190                 }
191
192                 /*
193                  * if we have more than 32k mids in the list, then something
194                  * is very wrong. Possibly a local user is trying to DoS the
195                  * box by issuing long-running calls and SIGKILL'ing them. If
196                  * we get to 2^16 mids then we're in big trouble as this
197                  * function could loop forever.
198                  *
199                  * Go ahead and assign out the mid in this situation, but force
200                  * an eventual reconnect to clean out the pending_mid_q.
201                  */
202                 if (num_mids > 32768)
203                         server->tcpStatus = CifsNeedReconnect;
204
205                 if (!collision) {
206                         mid = (__u64)cur_mid;
207                         server->CurrentMid = mid;
208                         break;
209                 }
210                 cur_mid++;
211         }
212         spin_unlock(&GlobalMid_Lock);
213         return mid;
214 }
215
216 /*
217         return codes:
218                 0       not a transact2, or all data present
219                 >0      transact2 with that much data missing
220                 -EINVAL invalid transact2
221  */
222 static int
223 check2ndT2(char *buf)
224 {
225         struct smb_hdr *pSMB = (struct smb_hdr *)buf;
226         struct smb_t2_rsp *pSMBt;
227         int remaining;
228         __u16 total_data_size, data_in_this_rsp;
229
230         if (pSMB->Command != SMB_COM_TRANSACTION2)
231                 return 0;
232
233         /* check for plausible wct, bcc and t2 data and parm sizes */
234         /* check for parm and data offset going beyond end of smb */
235         if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
236                 cFYI(1, "invalid transact2 word count");
237                 return -EINVAL;
238         }
239
240         pSMBt = (struct smb_t2_rsp *)pSMB;
241
242         total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
243         data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
244
245         if (total_data_size == data_in_this_rsp)
246                 return 0;
247         else if (total_data_size < data_in_this_rsp) {
248                 cFYI(1, "total data %d smaller than data in frame %d",
249                         total_data_size, data_in_this_rsp);
250                 return -EINVAL;
251         }
252
253         remaining = total_data_size - data_in_this_rsp;
254
255         cFYI(1, "missing %d bytes from transact2, check next response",
256                 remaining);
257         if (total_data_size > CIFSMaxBufSize) {
258                 cERROR(1, "TotalDataSize %d is over maximum buffer %d",
259                         total_data_size, CIFSMaxBufSize);
260                 return -EINVAL;
261         }
262         return remaining;
263 }
264
265 static int
266 coalesce_t2(char *second_buf, struct smb_hdr *target_hdr)
267 {
268         struct smb_t2_rsp *pSMBs = (struct smb_t2_rsp *)second_buf;
269         struct smb_t2_rsp *pSMBt  = (struct smb_t2_rsp *)target_hdr;
270         char *data_area_of_tgt;
271         char *data_area_of_src;
272         int remaining;
273         unsigned int byte_count, total_in_tgt;
274         __u16 tgt_total_cnt, src_total_cnt, total_in_src;
275
276         src_total_cnt = get_unaligned_le16(&pSMBs->t2_rsp.TotalDataCount);
277         tgt_total_cnt = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
278
279         if (tgt_total_cnt != src_total_cnt)
280                 cFYI(1, "total data count of primary and secondary t2 differ "
281                         "source=%hu target=%hu", src_total_cnt, tgt_total_cnt);
282
283         total_in_tgt = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
284
285         remaining = tgt_total_cnt - total_in_tgt;
286
287         if (remaining < 0) {
288                 cFYI(1, "Server sent too much data. tgt_total_cnt=%hu "
289                         "total_in_tgt=%hu", tgt_total_cnt, total_in_tgt);
290                 return -EPROTO;
291         }
292
293         if (remaining == 0) {
294                 /* nothing to do, ignore */
295                 cFYI(1, "no more data remains");
296                 return 0;
297         }
298
299         total_in_src = get_unaligned_le16(&pSMBs->t2_rsp.DataCount);
300         if (remaining < total_in_src)
301                 cFYI(1, "transact2 2nd response contains too much data");
302
303         /* find end of first SMB data area */
304         data_area_of_tgt = (char *)&pSMBt->hdr.Protocol +
305                                 get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
306
307         /* validate target area */
308         data_area_of_src = (char *)&pSMBs->hdr.Protocol +
309                                 get_unaligned_le16(&pSMBs->t2_rsp.DataOffset);
310
311         data_area_of_tgt += total_in_tgt;
312
313         total_in_tgt += total_in_src;
314         /* is the result too big for the field? */
315         if (total_in_tgt > USHRT_MAX) {
316                 cFYI(1, "coalesced DataCount too large (%u)", total_in_tgt);
317                 return -EPROTO;
318         }
319         put_unaligned_le16(total_in_tgt, &pSMBt->t2_rsp.DataCount);
320
321         /* fix up the BCC */
322         byte_count = get_bcc(target_hdr);
323         byte_count += total_in_src;
324         /* is the result too big for the field? */
325         if (byte_count > USHRT_MAX) {
326                 cFYI(1, "coalesced BCC too large (%u)", byte_count);
327                 return -EPROTO;
328         }
329         put_bcc(byte_count, target_hdr);
330
331         byte_count = be32_to_cpu(target_hdr->smb_buf_length);
332         byte_count += total_in_src;
333         /* don't allow buffer to overflow */
334         if (byte_count > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
335                 cFYI(1, "coalesced BCC exceeds buffer size (%u)", byte_count);
336                 return -ENOBUFS;
337         }
338         target_hdr->smb_buf_length = cpu_to_be32(byte_count);
339
340         /* copy second buffer into end of first buffer */
341         memcpy(data_area_of_tgt, data_area_of_src, total_in_src);
342
343         if (remaining != total_in_src) {
344                 /* more responses to go */
345                 cFYI(1, "waiting for more secondary responses");
346                 return 1;
347         }
348
349         /* we are done */
350         cFYI(1, "found the last secondary response");
351         return 0;
352 }
353
354 static bool
355 cifs_check_trans2(struct mid_q_entry *mid, struct TCP_Server_Info *server,
356                   char *buf, int malformed)
357 {
358         if (malformed)
359                 return false;
360         if (check2ndT2(buf) <= 0)
361                 return false;
362         mid->multiRsp = true;
363         if (mid->resp_buf) {
364                 /* merge response - fix up 1st*/
365                 malformed = coalesce_t2(buf, mid->resp_buf);
366                 if (malformed > 0)
367                         return true;
368                 /* All parts received or packet is malformed. */
369                 mid->multiEnd = true;
370                 dequeue_mid(mid, malformed);
371                 return true;
372         }
373         if (!server->large_buf) {
374                 /*FIXME: switch to already allocated largebuf?*/
375                 cERROR(1, "1st trans2 resp needs bigbuf");
376         } else {
377                 /* Have first buffer */
378                 mid->resp_buf = buf;
379                 mid->large_buf = true;
380                 server->bigbuf = NULL;
381         }
382         return true;
383 }
384
385 struct smb_version_operations smb1_operations = {
386         .send_cancel = send_nt_cancel,
387         .compare_fids = cifs_compare_fids,
388         .setup_request = cifs_setup_request,
389         .check_receive = cifs_check_receive,
390         .add_credits = cifs_add_credits,
391         .set_credits = cifs_set_credits,
392         .get_credits_field = cifs_get_credits_field,
393         .get_next_mid = cifs_get_next_mid,
394         .read_data_offset = cifs_read_data_offset,
395         .read_data_length = cifs_read_data_length,
396         .map_error = map_smb_to_linux_error,
397         .find_mid = cifs_find_mid,
398         .check_message = checkSMB,
399         .dump_detail = cifs_dump_detail,
400         .is_oplock_break = is_valid_oplock_break,
401         .check_trans2 = cifs_check_trans2,
402 };
403
404 struct smb_version_values smb1_values = {
405         .version_string = SMB1_VERSION_STRING,
406         .large_lock_type = LOCKING_ANDX_LARGE_FILES,
407         .exclusive_lock_type = 0,
408         .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
409         .unlock_lock_type = 0,
410         .header_size = sizeof(struct smb_hdr),
411         .max_header_size = MAX_CIFS_HDR_SIZE,
412         .read_rsp_size = sizeof(READ_RSP),
413 };