2 * fs/cifs/smb2transport.c
4 * Copyright (C) International Business Machines Corp., 2002, 2011
6 * Author(s): Steve French (sfrench@us.ibm.com)
7 * Jeremy Allison (jra@samba.org) 2006
8 * Pavel Shilovsky (pshilovsky@samba.org) 2012
10 * This library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
18 * the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/list.h>
27 #include <linux/wait.h>
28 #include <linux/net.h>
29 #include <linux/delay.h>
30 #include <linux/uaccess.h>
31 #include <asm/processor.h>
32 #include <linux/mempool.h>
33 #include <linux/highmem.h>
36 #include "cifsproto.h"
37 #include "smb2proto.h"
38 #include "cifs_debug.h"
39 #include "smb2status.h"
43 smb2_calc_signature(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
)
46 unsigned char smb2_signature
[SMB2_HMACSHA256_SIZE
];
47 unsigned char *sigptr
= smb2_signature
;
48 struct kvec
*iov
= rqst
->rq_iov
;
49 int n_vec
= rqst
->rq_nvec
;
50 struct smb2_hdr
*smb2_pdu
= (struct smb2_hdr
*)iov
[0].iov_base
;
52 memset(smb2_signature
, 0x0, SMB2_HMACSHA256_SIZE
);
53 memset(smb2_pdu
->Signature
, 0x0, SMB2_SIGNATURE_SIZE
);
55 rc
= crypto_shash_setkey(server
->secmech
.hmacsha256
,
56 server
->session_key
.response
, SMB2_NTLMV2_SESSKEY_SIZE
);
58 cERROR(1, "%s: Could not update with response\n", __func__
);
62 rc
= crypto_shash_init(&server
->secmech
.sdeschmacsha256
->shash
);
64 cERROR(1, "%s: Could not init md5\n", __func__
);
68 for (i
= 0; i
< n_vec
; i
++) {
69 if (iov
[i
].iov_len
== 0)
71 if (iov
[i
].iov_base
== NULL
) {
72 cERROR(1, "null iovec entry");
76 * The first entry includes a length field (which does not get
77 * signed that occupies the first 4 bytes before the header).
80 if (iov
[0].iov_len
<= 8) /* cmd field at offset 9 */
81 break; /* nothing to sign or corrupt header */
84 &server
->secmech
.sdeschmacsha256
->shash
,
85 iov
[i
].iov_base
+ 4, iov
[i
].iov_len
- 4);
89 &server
->secmech
.sdeschmacsha256
->shash
,
90 iov
[i
].iov_base
, iov
[i
].iov_len
);
93 cERROR(1, "%s: Could not update with payload\n",
99 /* now hash over the rq_pages array */
100 for (i
= 0; i
< rqst
->rq_npages
; i
++) {
103 cifs_rqst_page_to_kvec(rqst
, i
, &p_iov
);
104 crypto_shash_update(&server
->secmech
.sdeschmacsha256
->shash
,
105 p_iov
.iov_base
, p_iov
.iov_len
);
106 kunmap(rqst
->rq_pages
[i
]);
109 rc
= crypto_shash_final(&server
->secmech
.sdeschmacsha256
->shash
,
112 cERROR(1, "%s: Could not generate sha256 hash\n", __func__
);
114 memcpy(smb2_pdu
->Signature
, sigptr
, SMB2_SIGNATURE_SIZE
);
119 /* must be called with server->srv_mutex held */
121 smb2_sign_rqst(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
)
124 struct smb2_hdr
*smb2_pdu
= rqst
->rq_iov
[0].iov_base
;
126 if (!(smb2_pdu
->Flags
& SMB2_FLAGS_SIGNED
) ||
127 server
->tcpStatus
== CifsNeedNegotiate
)
130 if (!server
->session_estab
) {
131 strncpy(smb2_pdu
->Signature
, "BSRSPYL", 8);
135 rc
= smb2_calc_signature(rqst
, server
);
141 smb2_verify_signature(struct smb_rqst
*rqst
, struct TCP_Server_Info
*server
)
144 char server_response_sig
[16];
145 struct smb2_hdr
*smb2_pdu
= (struct smb2_hdr
*)rqst
->rq_iov
[0].iov_base
;
147 if ((smb2_pdu
->Command
== SMB2_NEGOTIATE
) ||
148 (smb2_pdu
->Command
== SMB2_OPLOCK_BREAK
) ||
149 (!server
->session_estab
))
153 * BB what if signatures are supposed to be on for session but
154 * server does not send one? BB
157 /* Do not need to verify session setups with signature "BSRSPYL " */
158 if (memcmp(smb2_pdu
->Signature
, "BSRSPYL ", 8) == 0)
159 cFYI(1, "dummy signature received for smb command 0x%x",
163 * Save off the origiginal signature so we can modify the smb and check
164 * our calculated signature against what the server sent.
166 memcpy(server_response_sig
, smb2_pdu
->Signature
, SMB2_SIGNATURE_SIZE
);
168 memset(smb2_pdu
->Signature
, 0, SMB2_SIGNATURE_SIZE
);
170 mutex_lock(&server
->srv_mutex
);
171 rc
= smb2_calc_signature(rqst
, server
);
172 mutex_unlock(&server
->srv_mutex
);
177 if (memcmp(server_response_sig
, smb2_pdu
->Signature
,
178 SMB2_SIGNATURE_SIZE
))
185 * Set message id for the request. Should be called after wait_for_free_request
186 * and when srv_mutex is held.
189 smb2_seq_num_into_buf(struct TCP_Server_Info
*server
, struct smb2_hdr
*hdr
)
191 hdr
->MessageId
= get_next_mid(server
);
194 static struct mid_q_entry
*
195 smb2_mid_entry_alloc(const struct smb2_hdr
*smb_buffer
,
196 struct TCP_Server_Info
*server
)
198 struct mid_q_entry
*temp
;
200 if (server
== NULL
) {
201 cERROR(1, "Null TCP session in smb2_mid_entry_alloc");
205 temp
= mempool_alloc(cifs_mid_poolp
, GFP_NOFS
);
209 memset(temp
, 0, sizeof(struct mid_q_entry
));
210 temp
->mid
= smb_buffer
->MessageId
; /* always LE */
211 temp
->pid
= current
->pid
;
212 temp
->command
= smb_buffer
->Command
; /* Always LE */
213 temp
->when_alloc
= jiffies
;
214 temp
->server
= server
;
217 * The default is for the mid to be synchronous, so the
218 * default callback just wakes up the current task.
220 temp
->callback
= cifs_wake_up_task
;
221 temp
->callback_data
= current
;
224 atomic_inc(&midCount
);
225 temp
->mid_state
= MID_REQUEST_ALLOCATED
;
230 smb2_get_mid_entry(struct cifs_ses
*ses
, struct smb2_hdr
*buf
,
231 struct mid_q_entry
**mid
)
233 if (ses
->server
->tcpStatus
== CifsExiting
)
236 if (ses
->server
->tcpStatus
== CifsNeedReconnect
) {
237 cFYI(1, "tcp session dead - return to caller to retry");
241 if (ses
->status
!= CifsGood
) {
242 /* check if SMB2 session is bad because we are setting it up */
243 if ((buf
->Command
!= SMB2_SESSION_SETUP
) &&
244 (buf
->Command
!= SMB2_NEGOTIATE
))
246 /* else ok - we are setting up session */
248 *mid
= smb2_mid_entry_alloc(buf
, ses
->server
);
251 spin_lock(&GlobalMid_Lock
);
252 list_add_tail(&(*mid
)->qhead
, &ses
->server
->pending_mid_q
);
253 spin_unlock(&GlobalMid_Lock
);
258 smb2_check_receive(struct mid_q_entry
*mid
, struct TCP_Server_Info
*server
,
261 unsigned int len
= get_rfc1002_length(mid
->resp_buf
);
263 struct smb_rqst rqst
= { .rq_iov
= &iov
,
266 iov
.iov_base
= (char *)mid
->resp_buf
;
267 iov
.iov_len
= get_rfc1002_length(mid
->resp_buf
) + 4;
269 dump_smb(mid
->resp_buf
, min_t(u32
, 80, len
));
270 /* convert the length into a more usable form */
272 (server
->sec_mode
& (SECMODE_SIGN_REQUIRED
|SECMODE_SIGN_ENABLED
))) {
275 rc
= smb2_verify_signature(&rqst
, server
);
277 cERROR(1, "SMB signature verification returned error = "
281 return map_smb2_to_linux_error(mid
->resp_buf
, log_error
);
285 smb2_setup_request(struct cifs_ses
*ses
, struct smb_rqst
*rqst
)
288 struct smb2_hdr
*hdr
= (struct smb2_hdr
*)rqst
->rq_iov
[0].iov_base
;
289 struct mid_q_entry
*mid
;
291 smb2_seq_num_into_buf(ses
->server
, hdr
);
293 rc
= smb2_get_mid_entry(ses
, hdr
, &mid
);
296 rc
= smb2_sign_rqst(rqst
, ses
->server
);
298 cifs_delete_mid(mid
);
305 smb2_setup_async_request(struct TCP_Server_Info
*server
, struct smb_rqst
*rqst
)
308 struct smb2_hdr
*hdr
= (struct smb2_hdr
*)rqst
->rq_iov
[0].iov_base
;
309 struct mid_q_entry
*mid
;
311 smb2_seq_num_into_buf(server
, hdr
);
313 mid
= smb2_mid_entry_alloc(hdr
, server
);
315 return ERR_PTR(-ENOMEM
);
317 rc
= smb2_sign_rqst(rqst
, server
);
319 DeleteMidQEntry(mid
);