smb2pdu.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * fs/cifs/smb2pdu.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2009, 2011
  5. * Etersoft, 2012
  6. * Author(s): Steve French (sfrench@us.ibm.com)
  7. * Pavel Shilovsky (pshilovsky@samba.org) 2012
  8. *
  9. * Contains the routines for constructing the SMB2 PDUs themselves
  10. *
  11. * This library is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published
  13. * by the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  19. * the GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
  26. /* Note that there are handle based routines which must be */
  27. /* treated slightly differently for reconnection purposes since we never */
  28. /* want to reuse a stale file handle and only the caller knows the file info */
  29. #include <linux/fs.h>
  30. #include <linux/kernel.h>
  31. #include <linux/vfs.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/xattr.h>
  34. #include "smb2pdu.h"
  35. #include "cifsglob.h"
  36. #include "cifsacl.h"
  37. #include "cifsproto.h"
  38. #include "smb2proto.h"
  39. #include "cifs_unicode.h"
  40. #include "cifs_debug.h"
  41. #include "ntlmssp.h"
  42. #include "smb2status.h"
  43. /*
  44. * The following table defines the expected "StructureSize" of SMB2 requests
  45. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
  46. *
  47. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  48. * indexed by command in host byte order.
  49. */
  50. static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  51. /* SMB2_NEGOTIATE */ 36,
  52. /* SMB2_SESSION_SETUP */ 25,
  53. /* SMB2_LOGOFF */ 4,
  54. /* SMB2_TREE_CONNECT */ 9,
  55. /* SMB2_TREE_DISCONNECT */ 4,
  56. /* SMB2_CREATE */ 57,
  57. /* SMB2_CLOSE */ 24,
  58. /* SMB2_FLUSH */ 24,
  59. /* SMB2_READ */ 49,
  60. /* SMB2_WRITE */ 49,
  61. /* SMB2_LOCK */ 48,
  62. /* SMB2_IOCTL */ 57,
  63. /* SMB2_CANCEL */ 4,
  64. /* SMB2_ECHO */ 4,
  65. /* SMB2_QUERY_DIRECTORY */ 33,
  66. /* SMB2_CHANGE_NOTIFY */ 32,
  67. /* SMB2_QUERY_INFO */ 41,
  68. /* SMB2_SET_INFO */ 33,
  69. /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
  70. };
  71. static void
  72. smb2_hdr_assemble(struct smb2_hdr *hdr, __le16 smb2_cmd /* command */ ,
  73. const struct cifs_tcon *tcon)
  74. {
  75. struct smb2_pdu *pdu = (struct smb2_pdu *)hdr;
  76. char *temp = (char *)hdr;
  77. /* lookup word count ie StructureSize from table */
  78. __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_cmd)];
  79. /*
  80. * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
  81. * largest operations (Create)
  82. */
  83. memset(temp, 0, 256);
  84. /* Note this is only network field converted to big endian */
  85. hdr->smb2_buf_length = cpu_to_be32(parmsize + sizeof(struct smb2_hdr)
  86. - 4 /* RFC 1001 length field itself not counted */);
  87. hdr->ProtocolId[0] = 0xFE;
  88. hdr->ProtocolId[1] = 'S';
  89. hdr->ProtocolId[2] = 'M';
  90. hdr->ProtocolId[3] = 'B';
  91. hdr->StructureSize = cpu_to_le16(64);
  92. hdr->Command = smb2_cmd;
  93. hdr->CreditRequest = cpu_to_le16(2); /* BB make this dynamic */
  94. hdr->ProcessId = cpu_to_le32((__u16)current->tgid);
  95. if (!tcon)
  96. goto out;
  97. hdr->TreeId = tcon->tid;
  98. /* Uid is not converted */
  99. if (tcon->ses)
  100. hdr->SessionId = tcon->ses->Suid;
  101. /* BB check following DFS flags BB */
  102. /* BB do we have to add check for SHI1005_FLAGS_DFS_ROOT too? */
  103. /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
  104. hdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
  105. /* BB how does SMB2 do case sensitive? */
  106. /* if (tcon->nocase)
  107. hdr->Flags |= SMBFLG_CASELESS; */
  108. /* if (tcon->ses && tcon->ses->server &&
  109. (tcon->ses->server->sec_mode & SECMODE_SIGN_REQUIRED))
  110. hdr->Flags |= SMB2_FLAGS_SIGNED; */
  111. out:
  112. pdu->StructureSize2 = cpu_to_le16(parmsize);
  113. return;
  114. }
  115. static int
  116. smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
  117. {
  118. int rc = 0;
  119. /* BB add missing code here */
  120. return rc;
  121. }
  122. /*
  123. * Allocate and return pointer to an SMB request hdr, and set basic
  124. * SMB information in the SMB header. If the return code is zero, this
  125. * function must have filled in request_buf pointer.
  126. */
  127. static int
  128. small_smb2_init(__le16 smb2_command, struct cifs_tcon *tcon,
  129. void **request_buf)
  130. {
  131. int rc = 0;
  132. rc = smb2_reconnect(smb2_command, tcon);
  133. if (rc)
  134. return rc;
  135. /* BB eventually switch this to SMB2 specific small buf size */
  136. *request_buf = cifs_small_buf_get();
  137. if (*request_buf == NULL) {
  138. /* BB should we add a retry in here if not a writepage? */
  139. return -ENOMEM;
  140. }
  141. smb2_hdr_assemble((struct smb2_hdr *) *request_buf, smb2_command, tcon);
  142. if (tcon != NULL) {
  143. #ifdef CONFIG_CIFS_STATS2
  144. /*
  145. uint16_t com_code = le16_to_cpu(smb2_command);
  146. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
  147. */
  148. #endif
  149. cifs_stats_inc(&tcon->num_smbs_sent);
  150. }
  151. return rc;
  152. }
  153. static void
  154. free_rsp_buf(int resp_buftype, void *rsp)
  155. {
  156. if (resp_buftype == CIFS_SMALL_BUFFER)
  157. cifs_small_buf_release(rsp);
  158. else if (resp_buftype == CIFS_LARGE_BUFFER)
  159. cifs_buf_release(rsp);
  160. }
  161. #define SMB2_NUM_PROT 1
  162. #define SMB2_PROT 0
  163. #define SMB21_PROT 1
  164. #define BAD_PROT 0xFFFF
  165. #define SMB2_PROT_ID 0x0202
  166. #define SMB21_PROT_ID 0x0210
  167. #define BAD_PROT_ID 0xFFFF
  168. static struct {
  169. int index;
  170. __le16 name;
  171. } smb2protocols[] = {
  172. {SMB2_PROT, cpu_to_le16(SMB2_PROT_ID)},
  173. {SMB21_PROT, cpu_to_le16(SMB21_PROT_ID)},
  174. {BAD_PROT, cpu_to_le16(BAD_PROT_ID)}
  175. };
  176. /*
  177. *
  178. * SMB2 Worker functions follow:
  179. *
  180. * The general structure of the worker functions is:
  181. * 1) Call smb2_init (assembles SMB2 header)
  182. * 2) Initialize SMB2 command specific fields in fixed length area of SMB
  183. * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
  184. * 4) Decode SMB2 command specific fields in the fixed length area
  185. * 5) Decode variable length data area (if any for this SMB2 command type)
  186. * 6) Call free smb buffer
  187. * 7) return
  188. *
  189. */
  190. int
  191. SMB2_negotiate(const unsigned int xid, struct cifs_ses *ses)
  192. {
  193. struct smb2_negotiate_req *req;
  194. struct smb2_negotiate_rsp *rsp;
  195. struct kvec iov[1];
  196. int rc = 0;
  197. int resp_buftype;
  198. struct TCP_Server_Info *server;
  199. unsigned int sec_flags;
  200. u16 i;
  201. u16 temp = 0;
  202. int blob_offset, blob_length;
  203. char *security_blob;
  204. int flags = CIFS_NEG_OP;
  205. cFYI(1, "Negotiate protocol");
  206. if (ses->server)
  207. server = ses->server;
  208. else {
  209. rc = -EIO;
  210. return rc;
  211. }
  212. rc = small_smb2_init(SMB2_NEGOTIATE, NULL, (void **) &req);
  213. if (rc)
  214. return rc;
  215. /* if any of auth flags (ie not sign or seal) are overriden use them */
  216. if (ses->overrideSecFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  217. sec_flags = ses->overrideSecFlg; /* BB FIXME fix sign flags?*/
  218. else /* if override flags set only sign/seal OR them with global auth */
  219. sec_flags = global_secflags | ses->overrideSecFlg;
  220. cFYI(1, "sec_flags 0x%x", sec_flags);
  221. req->hdr.SessionId = 0;
  222. for (i = 0; i < SMB2_NUM_PROT; i++)
  223. req->Dialects[i] = smb2protocols[i].name;
  224. req->DialectCount = cpu_to_le16(i);
  225. inc_rfc1001_len(req, i * 2);
  226. /* only one of SMB2 signing flags may be set in SMB2 request */
  227. if ((sec_flags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN)
  228. temp = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  229. else if (sec_flags & CIFSSEC_MAY_SIGN) /* MAY_SIGN is a single flag */
  230. temp = SMB2_NEGOTIATE_SIGNING_ENABLED;
  231. req->SecurityMode = cpu_to_le16(temp);
  232. req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
  233. iov[0].iov_base = (char *)req;
  234. /* 4 for rfc1002 length field */
  235. iov[0].iov_len = get_rfc1002_length(req) + 4;
  236. rc = SendReceive2(xid, ses, iov, 1, &resp_buftype, flags);
  237. rsp = (struct smb2_negotiate_rsp *)iov[0].iov_base;
  238. /*
  239. * No tcon so can't do
  240. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  241. */
  242. if (rc != 0)
  243. goto neg_exit;
  244. if (rsp == NULL) {
  245. rc = -EIO;
  246. goto neg_exit;
  247. }
  248. cFYI(1, "mode 0x%x", rsp->SecurityMode);
  249. if (rsp->DialectRevision == smb2protocols[SMB21_PROT].name)
  250. cFYI(1, "negotiated smb2.1 dialect");
  251. else if (rsp->DialectRevision == smb2protocols[SMB2_PROT].name)
  252. cFYI(1, "negotiated smb2 dialect");
  253. else {
  254. cERROR(1, "Illegal dialect returned by server %d",
  255. le16_to_cpu(rsp->DialectRevision));
  256. rc = -EIO;
  257. goto neg_exit;
  258. }
  259. server->dialect = le16_to_cpu(rsp->DialectRevision);
  260. server->maxBuf = le32_to_cpu(rsp->MaxTransactSize);
  261. server->max_read = le32_to_cpu(rsp->MaxReadSize);
  262. server->max_write = le32_to_cpu(rsp->MaxWriteSize);
  263. /* BB Do we need to validate the SecurityMode? */
  264. server->sec_mode = le16_to_cpu(rsp->SecurityMode);
  265. server->capabilities = le32_to_cpu(rsp->Capabilities);
  266. security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
  267. &rsp->hdr);
  268. if (blob_length == 0) {
  269. cERROR(1, "missing security blob on negprot");
  270. rc = -EIO;
  271. goto neg_exit;
  272. }
  273. #ifdef CONFIG_SMB2_ASN1 /* BB REMOVEME when updated asn1.c ready */
  274. rc = decode_neg_token_init(security_blob, blob_length,
  275. &server->sec_type);
  276. if (rc == 1)
  277. rc = 0;
  278. else if (rc == 0) {
  279. rc = -EIO;
  280. goto neg_exit;
  281. }
  282. #endif
  283. neg_exit:
  284. free_rsp_buf(resp_buftype, rsp);
  285. return rc;
  286. }