1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2019 Nexenta Systems, Inc.  All rights reserved.
14  * Copyright 2022 RackTop Systems, Inc.
15  */
16 
17 /*
18  * Dispatch function for SMB2_NEGOTIATE
19  */
20 
21 #include <smbsrv/smb2_kproto.h>
22 #include <smbsrv/smb2.h>
23 #include <sys/random.h>
24 
25 /*
26  * Note from [MS-SMB2] Sec. 2.2.3:  Windows servers return
27  * invalid parameter if the dialect count is greater than 64
28  * This is here (and not in smb2.h) because this is technically
29  * an implementation detail, not protocol specification.
30  */
31 #define	SMB2_NEGOTIATE_MAX_DIALECTS	64
32 
33 static int smb2_negotiate_common(smb_request_t *, uint16_t);
34 
35 /* List of supported capabilities.  Can be patched for testing. */
36 uint32_t smb2srv_capabilities =
37 	SMB2_CAP_DFS |
38 	SMB2_CAP_LEASING |
39 	SMB2_CAP_LARGE_MTU |
40 	SMB2_CAP_PERSISTENT_HANDLES |
41 	SMB2_CAP_ENCRYPTION;
42 
43 /* These are the only capabilities defined for SMB2.X */
44 #define	SMB_2X_CAPS (SMB2_CAP_DFS | SMB2_CAP_LEASING | SMB2_CAP_LARGE_MTU)
45 
46 /*
47  * These are not intended as customer tunables, but dev. & test folks
48  * might want to adjust them (with caution).
49  *
50  * smb2_tcp_bufsize is the TCP buffer size, applied to the network socket
51  * with setsockopt SO_SNDBUF, SO_RCVBUF.  These set the TCP window size.
52  * This is also used as a "sanity limit" for internal send/reply message
53  * allocations.  Note that with compounding SMB2 messages may contain
54  * multiple requests/responses.  This size should be large enough for
55  * at least a few SMB2 requests, and at least 2X smb2_max_rwsize.
56  *
57  * smb2_max_rwsize is what we put in the SMB2 negotiate response to tell
58  * the client the largest read and write request size we'll support.
59  * For now, we're using contiguous allocations, so keep this at 64KB
60  * so that (even with message overhead) allocations stay below 128KB,
61  * avoiding kmem_alloc -> page_create_va thrashing.
62  *
63  * smb2_max_trans is the largest "transact" send or receive, which is
64  * used for directory listings and info set/get operations.
65  */
66 uint32_t smb2_tcp_bufsize = (1<<22);	/* 4MB */
67 uint32_t smb2_max_rwsize = (1<<16);	/* 64KB */
68 uint32_t smb2_max_trans  = (1<<16);	/* 64KB */
69 
70 /*
71  * With clients (e.g. HP scanners) that don't advertise SMB2_CAP_LARGE_MTU
72  * (including all clients using dialect < SMB 2.1), use a "conservative" value
73  * for max r/w size because some older clients misbehave with larger values.
74  * 64KB is recommended in the [MS-SMB2] spec.  (3.3.5.3.1 SMB 2.1 or SMB 3.x
75  * Support) as the minimum so we'll use that.
76  */
77 uint32_t smb2_old_rwsize = (1<<16);	/* 64KB */
78 
79 /*
80  * List of all SMB2 versions we implement.  Note that the
81  * versions we support may be limited by the
82  * _cfg.skc_max_protocol and min_protocol settings.
83  */
84 static uint16_t smb2_versions[] = {
85 	0x202,	/* SMB 2.002 */
86 	0x210,	/* SMB 2.1 */
87 	0x300,	/* SMB 3.0 */
88 	0x302,	/* SMB 3.02 */
89 	0x311,	/* SMB 3.11 */
90 };
91 static uint16_t smb2_nversions =
92     sizeof (smb2_versions) / sizeof (smb2_versions[0]);
93 
94 enum smb2_neg_ctx_type {
95 	SMB2_PREAUTH_INTEGRITY_CAPS		= 1,
96 	SMB2_ENCRYPTION_CAPS			= 2,
97 	SMB2_COMPRESSION_CAPS			= 3,	/* not imlemented */
98 	SMB2_NETNAME_NEGOTIATE_CONTEXT_ID	= 5	/* not imlemented */
99 };
100 
101 typedef struct smb2_negotiate_ctx {
102 	uint16_t	type;
103 	uint16_t	datalen;
104 } smb2_neg_ctx_t;
105 
106 #define	SMB31_PREAUTH_CTX_SALT_LEN	32
107 
108 /*
109  * SMB 3.1.1 originally specified a single hashing algorithm - SHA-512 - and
110  * two encryption ones - AES-128-CCM and AES-128-GCM.
111  * Windows Server 2022 and Windows 11 introduced two further encryption
112  * algorithms - AES-256-CCM and AES-256-GCM.
113  */
114 #define	MAX_HASHID_NUM	(1)
115 #define	MAX_CIPHER_NUM	(4)
116 
117 typedef struct smb2_preauth_integrity_caps {
118 	uint16_t	picap_hash_count;
119 	uint16_t	picap_salt_len;
120 	uint16_t	picap_hash_id;
121 	uint8_t		picap_salt[SMB31_PREAUTH_CTX_SALT_LEN];
122 } smb2_preauth_caps_t;
123 
124 typedef struct smb2_encryption_caps {
125 	uint16_t	encap_cipher_count;
126 	uint16_t	encap_cipher_ids[MAX_CIPHER_NUM];
127 } smb2_encrypt_caps_t;
128 
129 /*
130  * The contexts we support
131  */
132 typedef struct smb2_preauth_neg_ctx {
133 	smb2_neg_ctx_t		neg_ctx;
134 	smb2_preauth_caps_t	preauth_caps;
135 } smb2_preauth_neg_ctx_t;
136 
137 typedef struct smb2_encrypt_neg_ctx {
138 	smb2_neg_ctx_t		neg_ctx;
139 	smb2_encrypt_caps_t	encrypt_caps;
140 } smb2_encrypt_neg_ctx_t;
141 
142 typedef struct smb2_neg_ctxs {
143 	uint32_t		offset;
144 	uint16_t		count;
145 	smb2_preauth_neg_ctx_t	preauth_ctx;
146 	smb2_encrypt_neg_ctx_t	encrypt_ctx;
147 } smb2_neg_ctxs_t;
148 
149 #define	NEG_CTX_INFO_OFFSET	(SMB2_HDR_SIZE + 28)
150 #define	NEG_CTX_OFFSET_OFFSET	(SMB2_HDR_SIZE + 64)
151 #define	NEG_CTX_MAX_COUNT	(16)
152 #define	NEG_CTX_MAX_DATALEN	(256)
153 
154 #define	STATUS_SMB_NO_PREAUTH_INEGRITY_HASH_OVERLAP	(0xC05D0000)
155 
156 #define	STATUS_PREAUTH_HASH_OVERLAP \
157     STATUS_SMB_NO_PREAUTH_INEGRITY_HASH_OVERLAP
158 
159 #define	SMB3_CIPHER_ENABLED(c, f)	((c) <= SMB3_CIPHER_MAX && \
160     SMB3_CIPHER_BIT(c) & (f))
161 
162 typedef struct smb2_arg_negotiate {
163 	struct smb2_neg_ctxs	neg_in_ctxs;
164 	struct smb2_neg_ctxs	neg_out_ctxs;
165 	uint16_t		neg_dialect_cnt;
166 	uint16_t		neg_dialects[SMB2_NEGOTIATE_MAX_DIALECTS];
167 	uint16_t		neg_highest_dialect;
168 } smb2_arg_negotiate_t;
169 
170 
171 static boolean_t
172 smb2_supported_version(smb_session_t *s, uint16_t version)
173 {
174 	int i;
175 
176 	if (version > s->s_cfg.skc_max_protocol ||
177 	    version < s->s_cfg.skc_min_protocol)
178 		return (B_FALSE);
179 	for (i = 0; i < smb2_nversions; i++)
180 		if (version == smb2_versions[i])
181 			return (B_TRUE);
182 	return (B_FALSE);
183 }
184 
185 static uint16_t
186 smb2_find_best_dialect(smb_session_t *s, uint16_t cl_versions[],
187     uint16_t version_cnt)
188 {
189 	uint16_t best_version = 0;
190 	int i;
191 
192 	for (i = 0; i < version_cnt; i++)
193 		if (smb2_supported_version(s, cl_versions[i]) &&
194 		    best_version < cl_versions[i])
195 			best_version = cl_versions[i];
196 
197 	return (best_version);
198 }
199 
200 /*
201  * This function should be called only for dialect >= 0x311
202  * Negotiate context list should contain exactly one
203  * SMB2_PREAUTH_INTEGRITY_CAPS context.
204  * Otherwise STATUS_INVALID_PARAMETER.
205  * It should contain at least 1 hash algorith what server does support.
206  * Otehrwise STATUS_SMB_NO_PREAUTH_INEGRITY_HASH_OVERLAP.
207  */
208 static uint32_t
209 smb31_decode_neg_ctxs(smb_request_t *sr)
210 {
211 	smb_session_t *s = sr->session;
212 	smb2_arg_negotiate_t *nego = sr->arg.other;
213 	smb2_neg_ctxs_t *neg_ctxs = &nego->neg_in_ctxs;
214 	smb2_preauth_caps_t *picap = &neg_ctxs->preauth_ctx.preauth_caps;
215 	smb2_encrypt_caps_t *encap = &neg_ctxs->encrypt_ctx.encrypt_caps;
216 	boolean_t found_sha512 = B_FALSE;
217 	boolean_t found_cipher = B_FALSE;
218 	uint16_t ciphers = sr->sr_server->sv_cfg.skc_encrypt_cipher;
219 	uint32_t status = 0;
220 	int32_t skip;
221 	int found_preauth_ctx = 0;
222 	int found_encrypt_ctx = 0;
223 	int cnt, i;
224 	int rc;
225 
226 	/*
227 	 * There should be exactly 1 SMB2_PREAUTH_INTEGRITY_CAPS negotiate ctx.
228 	 * SMB2_ENCRYPTION_CAPS is optional one.
229 	 * If there is no contexts or there are to many then stop parsing.
230 	 */
231 	cnt = neg_ctxs->count;
232 	if (cnt < 1 || cnt > NEG_CTX_MAX_COUNT) {
233 		status = NT_STATUS_INVALID_PARAMETER;
234 		goto errout;
235 	}
236 
237 	/*
238 	 * Cannot proceed parsing if the first context isn't aligned by 8.
239 	 */
240 	if (neg_ctxs->offset % 8 != 0) {
241 		status = NT_STATUS_INVALID_PARAMETER;
242 		goto errout;
243 	}
244 
245 	if ((skip = neg_ctxs->offset - sr->command.chain_offset) != 0 &&
246 	    smb_mbc_decodef(&sr->command, "#.", skip) != 0) {
247 		status = NT_STATUS_INVALID_PARAMETER;
248 		goto errout;
249 	}
250 
251 	/*
252 	 * Parse negotiate contexts. Ignore non-decoding errors to fill
253 	 * as much as possible data for dtrace probe.
254 	 */
255 	for (i = 0; i < cnt; i++) {
256 		smb2_neg_ctx_t neg_ctx;
257 		int32_t ctx_end_off;
258 		int32_t ctx_next_off;
259 
260 		if (i > 0) {
261 			if ((skip = ctx_next_off - ctx_end_off) != 0 &&
262 			    smb_mbc_decodef(&sr->command, "#.", skip) != 0) {
263 				status = NT_STATUS_INVALID_PARAMETER;
264 				goto errout;
265 			}
266 		}
267 
268 		rc = smb_mbc_decodef(
269 		    &sr->command, "ww4.",
270 		    &neg_ctx.type,	/* w */
271 		    &neg_ctx.datalen);	/* w */
272 		if (rc != 0) {
273 			status = NT_STATUS_INVALID_PARAMETER;
274 			goto errout;
275 		}
276 
277 		/*
278 		 * We got something crazy
279 		 */
280 		if (neg_ctx.datalen > NEG_CTX_MAX_DATALEN) {
281 			status = NT_STATUS_INVALID_PARAMETER;
282 			goto errout;
283 		}
284 
285 		ctx_end_off = sr->command.chain_offset + neg_ctx.datalen;
286 		ctx_next_off = P2ROUNDUP(ctx_end_off, 8);
287 
288 		switch (neg_ctx.type) {
289 		case SMB2_PREAUTH_INTEGRITY_CAPS:
290 			memcpy(&neg_ctxs->preauth_ctx.neg_ctx, &neg_ctx,
291 			    sizeof (neg_ctx));
292 
293 			if (found_preauth_ctx++ != 0) {
294 				status = NT_STATUS_INVALID_PARAMETER;
295 				continue;
296 			}
297 
298 			rc = smb_mbc_decodef(
299 			    &sr->command, "ww",
300 			    &picap->picap_hash_count,	/* w */
301 			    &picap->picap_salt_len);	/* w */
302 			if (rc != 0 || picap->picap_hash_count >
303 			    MAX_HASHID_NUM) {
304 				status = NT_STATUS_INVALID_PARAMETER;
305 				goto errout;
306 			}
307 
308 			/*
309 			 * Get hash id
310 			 */
311 			rc = smb_mbc_decodef(
312 			    &sr->command, "#w",
313 			    picap->picap_hash_count,
314 			    &picap->picap_hash_id);	/* w */
315 			if (rc != 0) {
316 				status = NT_STATUS_INVALID_PARAMETER;
317 				goto errout;
318 			}
319 
320 			/*
321 			 * Get salt
322 			 */
323 			rc = smb_mbc_decodef(
324 			    &sr->command, "#c",
325 			    sizeof (picap->picap_salt),
326 			    &picap->picap_salt[0]);	/* w */
327 			if (rc != 0) {
328 				status = NT_STATUS_INVALID_PARAMETER;
329 				goto errout;
330 			}
331 
332 			/*
333 			 * In SMB 0x311 there should be exactly 1 preauth
334 			 * negotiate context, and there should be exactly 1
335 			 * hash value in the list - SHA512.
336 			 */
337 			if (picap->picap_hash_count != 1) {
338 				status = NT_STATUS_INVALID_PARAMETER;
339 				continue;
340 			}
341 
342 			if (picap->picap_hash_id == SMB3_HASH_SHA512)
343 				found_sha512 = B_TRUE;
344 			break;
345 		case SMB2_ENCRYPTION_CAPS:
346 			memcpy(&neg_ctxs->preauth_ctx.neg_ctx, &neg_ctx,
347 			    sizeof (neg_ctx));
348 
349 			if (found_encrypt_ctx++ != 0) {
350 				status = NT_STATUS_INVALID_PARAMETER;
351 				continue;
352 			}
353 
354 			rc = smb_mbc_decodef(
355 			    &sr->command, "w",
356 			    &encap->encap_cipher_count);	/* w */
357 			if (rc != 0 || encap->encap_cipher_count >
358 			    MAX_CIPHER_NUM) {
359 				status = NT_STATUS_INVALID_PARAMETER;
360 				goto errout;
361 			}
362 
363 			/*
364 			 * Get cipher list
365 			 */
366 			rc = smb_mbc_decodef(
367 			    &sr->command, "#w",
368 			    encap->encap_cipher_count,
369 			    &encap->encap_cipher_ids[0]);	/* w */
370 			if (rc != 0) {
371 				status = NT_STATUS_INVALID_PARAMETER;
372 				goto errout;
373 			}
374 
375 			/*
376 			 * Select the first enabled cipher.
377 			 * Client should list more prioritized ciphers first.
378 			 */
379 			for (int k = 0; k < encap->encap_cipher_count; k++) {
380 				uint16_t c = encap->encap_cipher_ids[k];
381 
382 				if (SMB3_CIPHER_ENABLED(c, ciphers)) {
383 					s->smb31_enc_cipherid = c;
384 					found_cipher = B_TRUE;
385 					break;
386 				}
387 			}
388 			break;
389 		default:
390 			;
391 		}
392 	}
393 
394 	if (status)
395 		goto errout;
396 
397 	/* Not found mandatory SMB2_PREAUTH_INTEGRITY_CAPS ctx */
398 	if (found_preauth_ctx != 1 || found_encrypt_ctx > 1) {
399 		status = NT_STATUS_INVALID_PARAMETER;
400 		goto errout;
401 	}
402 
403 	if (!found_sha512) {
404 		status = STATUS_PREAUTH_HASH_OVERLAP;
405 		goto errout;
406 	}
407 
408 	s->smb31_preauth_hashid = SMB3_HASH_SHA512;
409 
410 	if (!found_cipher)
411 		s->smb31_enc_cipherid = 0;
412 
413 errout:
414 	return (status);
415 }
416 
417 static int
418 smb31_encode_neg_ctxs(smb_request_t *sr)
419 {
420 	smb_session_t *s = sr->session;
421 	smb2_arg_negotiate_t *nego = sr->arg.other;
422 	smb2_neg_ctxs_t *neg_ctxs = &nego->neg_out_ctxs;
423 	smb2_preauth_caps_t *picap = &neg_ctxs->preauth_ctx.preauth_caps;
424 	smb2_encrypt_caps_t *encap = &neg_ctxs->encrypt_ctx.encrypt_caps;
425 	uint16_t salt_len = sizeof (picap->picap_salt);
426 	uint32_t preauth_ctx_len = 6 + salt_len;
427 	uint32_t enc_ctx_len = 4;
428 	uint32_t neg_ctx_off = NEG_CTX_OFFSET_OFFSET +
429 	    P2ROUNDUP(sr->sr_cfg->skc_negtok_len, 8);
430 	uint32_t rc;
431 
432 	bzero(neg_ctxs, sizeof (*neg_ctxs));
433 
434 	if ((rc = smb_mbc_put_align(&sr->reply, 8)) != 0)
435 		return (rc);
436 
437 	ASSERT3S(neg_ctx_off, ==, sr->reply.chain_offset);
438 
439 	encap->encap_cipher_ids[0] = s->smb31_enc_cipherid;
440 	picap->picap_hash_id = s->smb31_preauth_hashid;
441 	picap->picap_salt_len = salt_len;
442 
443 	(void) random_get_pseudo_bytes(picap->picap_salt, salt_len);
444 
445 	rc = smb_mbc_encodef(
446 	    &sr->reply, "ww4.",
447 	    SMB2_PREAUTH_INTEGRITY_CAPS,
448 	    preauth_ctx_len
449 	    /* 4. */); /* reserved */
450 	if (rc != 0)
451 		return (rc);
452 
453 	rc = smb_mbc_encodef(
454 	    &sr->reply, "www#c",
455 	    1,				/* hash algo count */
456 	    salt_len,			/* salt length */
457 	    s->smb31_preauth_hashid,	/* hash id */
458 	    salt_len,			/* salt length */
459 	    picap->picap_salt);
460 
461 	/* aligned on 8-bytes boundary */
462 	if (rc != 0 || s->smb31_enc_cipherid == 0) {
463 		cmn_err(CE_NOTE, "Encryption is not supported");
464 		return (rc);
465 	}
466 
467 	if ((rc = smb_mbc_put_align(&sr->reply, 8)) != 0)
468 		return (rc);
469 
470 	rc = smb_mbc_encodef(
471 	    &sr->reply, "ww4.",
472 	    SMB2_ENCRYPTION_CAPS,
473 	    enc_ctx_len
474 	    /* 4. */); /* reserved */
475 
476 	rc = smb_mbc_encodef(
477 	    &sr->reply, "ww",
478 	    1,				/* cipher count */
479 	    s->smb31_enc_cipherid);	/* encrypt. cipher id */
480 
481 	return (rc);
482 }
483 
484 /*
485  * Helper for the (SMB1) smb_com_negotiate().  This is the
486  * very unusual protocol interaction where an SMB1 negotiate
487  * gets an SMB2 negotiate response.  This is the normal way
488  * clients first find out if the server supports SMB2.
489  *
490  * Note: This sends an SMB2 reply _itself_ and then returns
491  * SDRC_NO_REPLY so the caller will not send an SMB1 reply.
492  * Also, this is called directly from the reader thread, so
493  * we know this is the only thread using this session.
494  * Otherwise, this is similar to smb2_newrq_negotiate().
495  *
496  * The caller frees this request.
497  */
498 smb_sdrc_t
499 smb1_negotiate_smb2(smb_request_t *sr)
500 {
501 	smb_session_t *s = sr->session;
502 	smb_arg_negotiate_t *negprot = sr->sr_negprot;
503 	uint16_t smb2_version;
504 
505 	/*
506 	 * Note: In the SMB1 negotiate command handler, we
507 	 * agreed with one of the SMB2 dialects.  If that
508 	 * dialect was "SMB 2.002", we'll respond here with
509 	 * version 0x202 and negotiation is done.  If that
510 	 * dialect was "SMB 2.???", we'll respond here with
511 	 * the "wildcard" version 0x2FF, and the client will
512 	 * come back with an SMB2 negotiate.
513 	 */
514 	switch (negprot->ni_dialect) {
515 	case DIALECT_SMB2002:	/* SMB 2.002 (a.k.a. SMB2.0) */
516 		smb2_version = SMB_VERS_2_002;
517 		s->dialect = smb2_version;
518 		s->s_state = SMB_SESSION_STATE_NEGOTIATED;
519 		/* Allow normal SMB2 requests now. */
520 		s->newrq_func = smb2sr_newrq;
521 		break;
522 	case DIALECT_SMB2XXX:	/* SMB 2.??? (wildcard vers) */
523 		/*
524 		 * Expecting an SMB2 negotiate next, so keep the
525 		 * initial s->newrq_func.
526 		 */
527 		smb2_version = 0x2FF;
528 		break;
529 	default:
530 		return (SDRC_DROP_VC);
531 	}
532 
533 	/*
534 	 * We did not decode an SMB2 header, so make sure
535 	 * the SMB2 header fields are initialized.
536 	 * (Most are zero from smb_request_alloc.)
537 	 * Also, the SMB1 common dispatch code reserved space
538 	 * for an SMB1 header, which we need to undo here.
539 	 */
540 	sr->smb2_reply_hdr = sr->reply.chain_offset = 0;
541 	sr->smb2_cmd_code = SMB2_NEGOTIATE;
542 	sr->smb2_hdr_flags = SMB2_FLAGS_SERVER_TO_REDIR;
543 
544 	/*
545 	 * Also setup SMB2 negotiate args (empty here).
546 	 * SMB1 args free'd by smb_srm_fini(sr)
547 	 */
548 	sr->arg.other = smb_srm_zalloc(sr, sizeof (smb2_arg_negotiate_t));
549 
550 	(void) smb2_encode_header(sr, B_FALSE);
551 	if (smb2_negotiate_common(sr, smb2_version) != 0)
552 		sr->smb2_status = NT_STATUS_INTERNAL_ERROR;
553 	if (sr->smb2_status != 0)
554 		smb2sr_put_error(sr, sr->smb2_status);
555 	(void) smb2_encode_header(sr, B_TRUE);
556 
557 	smb2_send_reply(sr);
558 
559 	/*
560 	 * We sent the reply, so tell the SMB1 dispatch
561 	 * it should NOT (also) send a reply.
562 	 */
563 	return (SDRC_NO_REPLY);
564 }
565 
566 /*
567  * SMB2 Negotiate gets special handling.  This is called directly by
568  * the reader thread (see smbsr_newrq_initial) with what _should_ be
569  * an SMB2 Negotiate.  Only the "\feSMB" header has been checked
570  * when this is called, so this needs to check the SMB command,
571  * if it's Negotiate execute it, then send the reply, etc.
572  *
573  * Since this is called directly from the reader thread, we
574  * know this is the only thread currently using this session.
575  * This has to duplicate some of what smb2sr_work does as a
576  * result of bypassing the normal dispatch mechanism.
577  *
578  * The caller always frees this request.
579  *
580  * Return value is 0 for success, and anything else will
581  * terminate the reader thread (drop the connection).
582  */
583 int
584 smb2_newrq_negotiate(smb_request_t *sr)
585 {
586 	smb_session_t *s = sr->session;
587 	smb2_arg_negotiate_t *nego;
588 	int rc;
589 	uint32_t nctx_status = 0;
590 	uint32_t status = 0;
591 	uint32_t neg_ctx_off;
592 	uint16_t neg_ctx_cnt;
593 	uint16_t struct_size;
594 	uint16_t dialect_cnt;
595 	uint16_t best_version;
596 
597 	nego = smb_srm_zalloc(sr, sizeof (smb2_arg_negotiate_t));
598 	sr->arg.other = nego;	// for dtrace
599 
600 	sr->smb2_cmd_hdr = sr->command.chain_offset;
601 	rc = smb2_decode_header(sr);
602 	if (rc != 0)
603 		return (rc);
604 
605 	if (sr->smb2_hdr_flags & SMB2_FLAGS_SERVER_TO_REDIR)
606 		return (-1);
607 
608 	if ((sr->smb2_cmd_code != SMB2_NEGOTIATE) ||
609 	    (sr->smb2_next_command != 0))
610 		return (-1);
611 
612 	/*
613 	 * Decode SMB2 Negotiate (fixed-size part)
614 	 */
615 	rc = smb_mbc_decodef(
616 	    &sr->command, "www..l16clw..",
617 	    &struct_size,	/* w */
618 	    &dialect_cnt,	/* w */
619 	    &s->cli_secmode,	/* w */
620 	    /* reserved		(..) */
621 	    &s->capabilities,	/* l */
622 	    s->clnt_uuid,	/* 16c */
623 	    &neg_ctx_off,	/* l */
624 	    &neg_ctx_cnt);	/* w */
625 	    /* reserverd	(..) */
626 	if (rc != 0)
627 		return (rc);
628 	if (struct_size != 36)
629 		return (-1);
630 
631 	/*
632 	 * Decode SMB2 Negotiate (variable part)
633 	 *
634 	 * Be somewhat tolerant while decoding the variable part
635 	 * so we can return errors instead of dropping the client.
636 	 * Will limit decoding to the size of cli_dialects here,
637 	 * and do error checks on the decoded dialect_cnt after the
638 	 * dtrace start probe.
639 	 */
640 	if (dialect_cnt > SMB2_NEGOTIATE_MAX_DIALECTS)
641 		nego->neg_dialect_cnt = SMB2_NEGOTIATE_MAX_DIALECTS;
642 	else
643 		nego->neg_dialect_cnt = dialect_cnt;
644 	if (nego->neg_dialect_cnt > 0) {
645 		rc = smb_mbc_decodef(&sr->command, "#w",
646 		    nego->neg_dialect_cnt,
647 		    nego->neg_dialects);
648 		if (rc != 0)
649 			return (rc);	// short msg
650 	}
651 
652 	best_version = smb2_find_best_dialect(s, nego->neg_dialects,
653 	    nego->neg_dialect_cnt);
654 
655 	if (best_version >= SMB_VERS_3_11) {
656 		nego->neg_in_ctxs.offset = neg_ctx_off;
657 		nego->neg_in_ctxs.count  = neg_ctx_cnt;
658 		nctx_status = smb31_decode_neg_ctxs(sr);
659 		/* check nctx_status below */
660 	}
661 
662 	DTRACE_SMB2_START(op__Negotiate, smb_request_t *, sr);
663 
664 	sr->smb2_credit_response = 1;
665 	sr->smb2_hdr_flags |= SMB2_FLAGS_SERVER_TO_REDIR;
666 	(void) smb2_encode_header(sr, B_FALSE);
667 
668 	/*
669 	 * NOW start validating things (NOT before here)
670 	 */
671 
672 	/*
673 	 * [MS-SMB2] 3.3.5.2.4 Verifying the Signature
674 	 * "If the SMB2 header of the SMB2 NEGOTIATE request has the
675 	 * SMB2_FLAGS_SIGNED bit set in the Flags field, the server
676 	 * MUST fail the request with STATUS_INVALID_PARAMETER."
677 	 */
678 	if ((sr->smb2_hdr_flags & SMB2_FLAGS_SIGNED) != 0) {
679 		sr->smb2_hdr_flags &= ~SMB2_FLAGS_SIGNED;
680 		status = NT_STATUS_INVALID_PARAMETER;
681 		goto errout;
682 	}
683 
684 	/*
685 	 * [MS-SMB2] 3.3.5.4 Receiving an SMB2 NEGOTIATE Request
686 	 * "If the DialectCount of the SMB2 NEGOTIATE Request is 0, the
687 	 * server MUST fail the request with STATUS_INVALID_PARAMETER."
688 	 * Checking the decoded value here, not the constrained one.
689 	 */
690 	if (dialect_cnt == 0 ||
691 	    dialect_cnt > SMB2_NEGOTIATE_MAX_DIALECTS) {
692 		status = NT_STATUS_INVALID_PARAMETER;
693 		goto errout;
694 	}
695 
696 	/*
697 	 * We decoded the offered dialects above, and
698 	 * determined which was the highest we support.
699 	 *
700 	 * [MS-SMB2] 3.3.5.4 Receiving an SMB2 NEGOTIATE Request
701 	 * "If a common dialect is not found, the server MUST fail
702 	 * the request with STATUS_NOT_SUPPORTED."
703 	 */
704 	if (best_version == 0) {
705 		status = NT_STATUS_NOT_SUPPORTED;
706 		goto errout;
707 	}
708 
709 	/*
710 	 * Check for problems with the negotiate contexts.
711 	 */
712 	if (nctx_status != 0) {
713 		status = nctx_status;
714 		goto errout;
715 	}
716 
717 	/* Allow normal SMB2 requests now. */
718 	s->dialect = best_version;
719 	s->s_state = SMB_SESSION_STATE_NEGOTIATED;
720 	s->newrq_func = smb2sr_newrq;
721 
722 	if (smb2_negotiate_common(sr, best_version) != 0)
723 		status = NT_STATUS_INTERNAL_ERROR;
724 
725 	if (s->dialect >= SMB_VERS_3_11 && status == 0) {
726 		if (smb31_encode_neg_ctxs(sr) != 0)
727 			status = NT_STATUS_INTERNAL_ERROR;
728 	}
729 
730 errout:
731 	sr->smb2_status = status;
732 	DTRACE_SMB2_DONE(op__Negotiate, smb_request_t *, sr);
733 
734 	if (sr->smb2_status != 0)
735 		smb2sr_put_error(sr, sr->smb2_status);
736 	(void) smb2_encode_header(sr, B_TRUE);
737 
738 	if (s->dialect >= SMB_VERS_3_11 && sr->smb2_status == 0) {
739 		ASSERT3U(s->smb31_preauth_hashid, !=, 0);
740 		if (smb31_preauth_sha512_calc(sr, &sr->reply,
741 		    s->smb31_preauth_hashval,
742 		    s->smb31_preauth_hashval) != 0)
743 			cmn_err(CE_WARN, "(1) Preauth hash calculation "
744 			    "failed");
745 	}
746 
747 	smb2_send_reply(sr);
748 
749 	return (rc);
750 }
751 
752 /*
753  * Common parts of SMB2 Negotiate, used for both the
754  * SMB1-to-SMB2 style, and straight SMB2 style.
755  * Do negotiation decisions and encode the reply.
756  * The caller does the network send.
757  *
758  * Return value is 0 for success, else error.
759  */
760 static int
761 smb2_negotiate_common(smb_request_t *sr, uint16_t version)
762 {
763 	timestruc_t boot_tv, now_tv;
764 	smb_session_t *s = sr->session;
765 	int rc;
766 	uint32_t max_rwsize;
767 	uint16_t secmode;
768 	uint16_t neg_ctx_cnt = 0;
769 	uint32_t neg_ctx_off = 0;
770 
771 	/*
772 	 * Negotiation itself.  First the Security Mode.
773 	 */
774 	secmode = SMB2_NEGOTIATE_SIGNING_ENABLED;
775 	if (sr->sr_cfg->skc_signing_required)
776 		secmode |= SMB2_NEGOTIATE_SIGNING_REQUIRED;
777 	s->srv_secmode = secmode;
778 
779 	s->cmd_max_bytes = smb2_tcp_bufsize;
780 	s->reply_max_bytes = smb2_tcp_bufsize;
781 
782 	/*
783 	 * "The number of credits held by the client MUST be considered
784 	 * as 1 when the connection is established." [MS-SMB2]
785 	 * We leave credits at 1 until the first successful
786 	 * session setup is completed.
787 	 */
788 	s->s_cur_credits = s->s_max_credits = 1;
789 	sr->smb2_credit_response = 1;
790 
791 	boot_tv.tv_sec = smb_get_boottime();
792 	boot_tv.tv_nsec = 0;
793 	now_tv.tv_sec = gethrestime_sec();
794 	now_tv.tv_nsec = 0;
795 
796 	/*
797 	 * If the version is 0x2FF, we haven't completed negotiate.
798 	 * Don't initialize until we have our final request.
799 	 */
800 	if (version != 0x2FF)
801 		smb2_sign_init_mech(s);
802 	if (version >= 0x311)
803 		smb31_preauth_init_mech(s);
804 
805 	/*
806 	 * [MS-SMB2] 3.3.5.4 Receiving an SMB2 NEGOTIATE Request
807 	 *
808 	 * The SMB2.x capabilities are returned without regard for
809 	 * what capabilities the client provided in the request.
810 	 * The SMB3.x capabilities returned are the traditional
811 	 * logical AND of server and client capabilities.
812 	 *
813 	 * One additional check: If KCF is missing something we
814 	 * require for encryption, turn off that capability.
815 	 */
816 	if (s->dialect < SMB_VERS_2_1) {
817 		/* SMB 2.002 */
818 		s->srv_cap = smb2srv_capabilities & SMB2_CAP_DFS;
819 	} else if (s->dialect < SMB_VERS_3_0) {
820 		/* SMB 2.x */
821 		s->srv_cap = smb2srv_capabilities & SMB_2X_CAPS;
822 	} else {
823 		/* SMB 3.0 or later */
824 		s->srv_cap = smb2srv_capabilities &
825 		    (SMB_2X_CAPS | s->capabilities);
826 		if ((s->srv_cap & SMB2_CAP_ENCRYPTION) != 0 &&
827 		    smb3_encrypt_init_mech(s) != 0) {
828 			s->srv_cap &= ~SMB2_CAP_ENCRYPTION;
829 			s->smb31_enc_cipherid = 0;
830 		}
831 
832 		if (s->dialect >= SMB_VERS_3_11) {
833 			neg_ctx_cnt = s->smb31_enc_cipherid == 0 ? 1 : 2;
834 			neg_ctx_off = NEG_CTX_OFFSET_OFFSET +
835 			    P2ROUNDUP(sr->sr_cfg->skc_negtok_len, 8);
836 
837 			ASSERT3U(s->smb31_preauth_hashid, !=, 0);
838 
839 			if (smb31_preauth_sha512_calc(sr, &sr->command,
840 			    s->smb31_preauth_hashval,
841 			    s->smb31_preauth_hashval) != 0)
842 				cmn_err(CE_WARN, "(0) Preauth hash calculation "
843 				    "failed");
844 		}
845 	}
846 
847 	/*
848 	 * See notes above smb2_max_rwsize, smb2_old_rwsize
849 	 */
850 	if (s->capabilities & SMB2_CAP_LARGE_MTU)
851 		max_rwsize = smb2_max_rwsize;
852 	else
853 		max_rwsize = smb2_old_rwsize;
854 
855 	rc = smb_mbc_encodef(
856 	    &sr->reply,
857 	    "wwww#cllllTTwwl#c",
858 	    65,	/* StructSize */	/* w */
859 	    s->srv_secmode,		/* w */
860 	    version,			/* w */
861 	    neg_ctx_cnt,		/* w */
862 	    UUID_LEN,			/* # */
863 	    &s->s_cfg.skc_machine_uuid, /* c */
864 	    s->srv_cap,			/* l */
865 	    smb2_max_trans,		/* l */
866 	    max_rwsize,			/* l */
867 	    max_rwsize,			/* l */
868 	    &now_tv,			/* T */
869 	    &boot_tv,			/* T */
870 	    128, /* SecBufOff */	/* w */
871 	    sr->sr_cfg->skc_negtok_len,	/* w */
872 	    neg_ctx_off,		/* l */
873 	    sr->sr_cfg->skc_negtok_len,	/* # */
874 	    sr->sr_cfg->skc_negtok);	/* c */
875 
876 
877 
878 	/* smb2_send_reply(sr); in caller */
879 
880 	(void) ksocket_setsockopt(s->sock, SOL_SOCKET,
881 	    SO_SNDBUF, (const void *)&smb2_tcp_bufsize,
882 	    sizeof (smb2_tcp_bufsize), CRED());
883 	(void) ksocket_setsockopt(s->sock, SOL_SOCKET,
884 	    SO_RCVBUF, (const void *)&smb2_tcp_bufsize,
885 	    sizeof (smb2_tcp_bufsize), CRED());
886 
887 	return (rc);
888 }
889 
890 /*
891  * SMB2 Dispatch table handler, which will run if we see an
892  * SMB2_NEGOTIATE after the initial negotiation is done.
893  * That would be a protocol error.
894  */
895 smb_sdrc_t
896 smb2_negotiate(smb_request_t *sr)
897 {
898 	sr->smb2_status = NT_STATUS_INVALID_PARAMETER;
899 	return (SDRC_ERROR);
900 }
901 
902 /*
903  * VALIDATE_NEGOTIATE_INFO [MS-SMB2] 2.2.32.6
904  */
905 uint32_t
906 smb2_nego_validate(smb_request_t *sr, smb_fsctl_t *fsctl)
907 {
908 	smb_session_t *s = sr->session;
909 	int rc;
910 
911 	/*
912 	 * The spec. says to parse the VALIDATE_NEGOTIATE_INFO here
913 	 * and verify that the original negotiate was not modified.
914 	 *
915 	 * One interesting requirement here is that we MUST reply
916 	 * with exactly the same information as we returned in our
917 	 * original reply to the SMB2 negotiate on this session.
918 	 * If we don't the client closes the connection.
919 	 */
920 
921 	uint32_t capabilities;
922 	uint16_t secmode;
923 	uint16_t num_dialects;
924 	uint16_t dialects[SMB2_NEGOTIATE_MAX_DIALECTS];
925 	uint8_t clnt_guid[16];
926 
927 	if (s->dialect >= SMB_VERS_3_11)
928 		goto drop;
929 
930 	/*
931 	 * [MS-SMB2] 3.3.5.2.4 Verifying the Signature
932 	 *
933 	 * If the dialect is SMB3 and the message was successfully
934 	 * decrypted we MUST skip processing of the signature.
935 	 */
936 	if (!sr->encrypted && (sr->smb2_hdr_flags & SMB2_FLAGS_SIGNED) == 0)
937 		goto drop;
938 
939 	if (fsctl->InputCount < 24)
940 		goto drop;
941 
942 	(void) smb_mbc_decodef(fsctl->in_mbc, "l16cww",
943 	    &capabilities, /* l */
944 	    &clnt_guid, /* 16c */
945 	    &secmode, /* w */
946 	    &num_dialects); /* w */
947 
948 	if (num_dialects == 0 || num_dialects > SMB2_NEGOTIATE_MAX_DIALECTS)
949 		goto drop;
950 	if (secmode != s->cli_secmode)
951 		goto drop;
952 	if (capabilities != s->capabilities)
953 		goto drop;
954 	if (memcmp(clnt_guid, s->clnt_uuid, sizeof (clnt_guid)) != 0)
955 		goto drop;
956 
957 	rc = smb_mbc_decodef(fsctl->in_mbc, "#w", num_dialects, dialects);
958 	if (rc != 0)
959 		goto drop;
960 
961 	/*
962 	 * MS-SMB2 says we should compare the dialects array with the
963 	 * one sent previously, but that appears to be unnecessary
964 	 * as long as we end up with the same dialect.
965 	 */
966 	if (smb2_find_best_dialect(s, dialects, num_dialects) != s->dialect)
967 		goto drop;
968 
969 	rc = smb_mbc_encodef(
970 	    fsctl->out_mbc, "l#cww",
971 	    s->srv_cap,			/* l */
972 	    UUID_LEN,			/* # */
973 	    &s->s_cfg.skc_machine_uuid, /* c */
974 	    s->srv_secmode,		/* w */
975 	    s->dialect);		/* w */
976 	if (rc == 0)
977 		return (rc);
978 
979 drop:
980 	smb_session_disconnect(s);
981 	return (NT_STATUS_ACCESS_DENIED);
982 }
983