Debian Codegraph @

source4/torture/smb2/credits.c

raw
1 /*
2 Unix SMB/CIFS implementation.
3
4 test suite for SMB2 credits
5
6 Copyright (C) Ralph Boehme 2017
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "torture/torture.h"
24
25 #include "lib/cmdline/cmdline.h"
26 #include "lib/param/param.h"
27 #include "libcli/resolve/resolve.h"
28 #include "libcli/smb/smbXcli_base.h"
29 #include "libcli/smb2/smb2.h"
30 #include "libcli/smb2/smb2_calls.h"
31 #include "librpc/gen_ndr/ndr_lsa.h"
32 #include "torture/smb2/proto.h"
33 #include "util/tevent_ntstatus.h"
34
35 /**
36 * Request 64k credits in negprot/sessionsetup and require at least 8k
37 *
38 * This passes against Windows 2016
39 **/
40 static bool test_session_setup_credits_granted(struct torture_context *tctx,
41 struct smb2_tree *_tree)
42 {
43 struct smbcli_options options;
44 struct smb2_transport *transport = NULL;
45 struct smb2_tree *tree = NULL;
46 uint16_t cur_credits;
47 NTSTATUS status;
48 bool ret = true;
49
50 transport = _tree->session->transport;
51 options = transport->options;
52
53 status = smb2_logoff(_tree->session);
54 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
55 "smb2_logoff failed\n");
56 TALLOC_FREE(_tree);
57
58 options.max_credits = 65535;
59
60 ret = torture_smb2_connection_ext(tctx, 0, &options, &tree);
61 torture_assert_goto(tctx, ret == true, ret, done,
62 "torture_smb2_connection_ext failed\n");
63
64 transport = tree->session->transport;
65
66 cur_credits = smb2cli_conn_get_cur_credits(transport->conn);
67 if (cur_credits < 8192) {
68 torture_result(tctx, TORTURE_FAIL,
69 "Server only granted %" PRIu16" credits\n",
70 cur_credits);
71 ret = false;
72 goto done;
73 }
74
75 done:
76 TALLOC_FREE(tree);
77 return ret;
78 }
79
80 /**
81 * Request 64K credits in a single SMB2 request and requite at least 8192
82 *
83 * This passes against Windows 2016
84 **/
85 static bool test_single_req_credits_granted(struct torture_context *tctx,
86 struct smb2_tree *_tree)
87 {
88 struct smbcli_options options;
89 struct smb2_transport *transport = NULL;
90 struct smb2_tree *tree = NULL;
91 struct smb2_handle h = {{0}};
92 struct smb2_create create;
93 const char *fname = "single_req_credits_granted.dat";
94 uint16_t cur_credits;
95 NTSTATUS status;
96 bool ret = true;
97
98 smb2_util_unlink(_tree, fname);
99
100 transport = _tree->session->transport;
101 options = transport->options;
102
103 status = smb2_logoff(_tree->session);
104 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
105 "smb2_logoff failed\n");
106 TALLOC_FREE(_tree);
107
108 options.max_credits = 1;
109
110 ret = torture_smb2_connection_ext(tctx, 0, &options, &tree);
111 torture_assert_goto(tctx, ret == true, ret, done,
112 "torture_smb2_connection_ext failed\n");
113
114 transport = tree->session->transport;
115
116 cur_credits = smb2cli_conn_get_cur_credits(transport->conn);
117 if (cur_credits != 1) {
118 torture_result(tctx, TORTURE_FAIL,
119 "Only wanted 1 credit but server granted %" PRIu16"\n",
120 cur_credits);
121 ret = false;
122 goto done;
123 }
124
125 smb2cli_conn_set_max_credits(transport->conn, 65535);
126
127 ZERO_STRUCT(create);
128 create.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
129 create.in.desired_access = SEC_RIGHTS_FILE_ALL;
130 create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
131 create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
132 create.in.fname = fname;
133
134 status = smb2_create(tree, tctx, &create);
135 torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
136 "smb2_create failed\n");
137 h = create.out.file.handle;
138
139 cur_credits = smb2cli_conn_get_cur_credits(transport->conn);
140 if (cur_credits < 8192) {
141 torture_result(tctx, TORTURE_FAIL,
142 "Server only granted %" PRIu16" credits\n",
143 cur_credits);
144 ret = false;
145 goto done;
146 }
147
148 done:
149 if (!smb2_util_handle_empty(h)) {
150 smb2_util_close(tree, h);
151 }
152 smb2_util_unlink(tree, fname);
153 TALLOC_FREE(tree);
154 return ret;
155 }
156
157 static bool test_crediting_skipped_mid(struct torture_context *tctx,
158 struct smb2_tree *_tree)
159 {
160 struct smbcli_options options;
161 struct smb2_transport *transport = NULL;
162 struct smb2_tree *tree = NULL;
163 struct smb2_handle h = {{0}};
164 struct smb2_create create;
165 const char *fname = "skipped_mid.dat";
166 uint64_t mid;
167 uint16_t cur_credits;
168 NTSTATUS status;
169 bool ret = true;
170 int i;
171
172 smb2_util_unlink(_tree, fname);
173
174 transport = _tree->session->transport;
175 options = transport->options;
176
177 status = smb2_logoff(_tree->session);
178 torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "smb2_logoff failed\n");
179 TALLOC_FREE(_tree);
180
181 options.max_credits = 8192;
182
183 ret = torture_smb2_connection_ext(tctx, 0, &options, &tree);
184 torture_assert_goto(tctx, ret == true, ret, done, "torture_smb2_connection_ext failed\n");
185
186 transport = tree->session->transport;
187
188 cur_credits = smb2cli_conn_get_cur_credits(transport->conn);
189 if (cur_credits != 8192) {
190 torture_result(tctx, TORTURE_FAIL, "Server only granted %" PRIu16" credits\n", cur_credits);
191 ret = false;
192 goto done;
193 }
194
195 ZERO_STRUCT(create);
196 create.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
197 create.in.desired_access = SEC_RIGHTS_FILE_ALL;
198 create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
199 create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
200 create.in.fname = fname;
201
202 status = smb2_create(tree, tctx, &create);
203 torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "smb2_create failed\n");
204 h = create.out.file.handle;
205
206 /*
207 * See what happens if we skip a mid. As we want to avoid triggering our
208 * client side mid window check we keep conn->smb2.cur_credits
209 * unchanged so the server keeps granting credits until it's max mid
210 * windows size is reached at which point it will disconnect us:
211 *
212 * o Windows 2016 currently has a maximum mid window size of 8192 by
213 * default
214 *
215 * o Samba's limit is 512
216 *
217 * o Windows 2008r2 uses some special algorithm (MS-SMB2 3.3.1.1
218 * footnote <167>) that kicks in once a mid is skipped, resulting in a
219 * maximum window size of 100-300 depending on the number of granted
220 * credits at the moment of skipping a mid.
221 */
222
223 mid = smb2cli_conn_get_mid(tree->session->transport->conn);
224 smb2cli_conn_set_mid(tree->session->transport->conn, mid + 1);
225
226 for (i = 0; i < 8191; i++) {
227 status = smb2_util_write(tree, h, "\0", 0, 1);
228 if (!NT_STATUS_IS_OK(status)) {
229 torture_result(tctx, TORTURE_FAIL, "Server only allowed %d writes\n", i);
230 ret = false;
231 goto done;
232 }
233 }
234
235 /*
236 * Now use the skipped mid (the smb2_util_close...), we should
237 * immediately get a full mid window of size 8192.
238 */
239 smb2cli_conn_set_mid(tree->session->transport->conn, mid);
240 status = smb2_util_close(tree, h);
241 torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "smb2_close failed\n");
242 ZERO_STRUCT(h);
243
244 cur_credits = smb2cli_conn_get_cur_credits(transport->conn);
245 if (cur_credits != 8192) {
246 torture_result(tctx, TORTURE_FAIL, "Server only granted %" PRIu16" credits\n", cur_credits);
247 ret = false;
248 goto done;
249 }
250
251 smb2cli_conn_set_mid(tree->session->transport->conn, mid + 8192);
252
253 done:
254 if (!smb2_util_handle_empty(h)) {
255 smb2_util_close(tree, h);
256 }
257 smb2_util_unlink(tree, fname);
258 TALLOC_FREE(tree);
259 return ret;
260 }
261
262 #define SMBXCLI_NP_DESIRED_ACCESS \
263 (SEC_STD_READ_CONTROL | SEC_FILE_READ_DATA | SEC_FILE_WRITE_DATA | \
264 SEC_FILE_APPEND_DATA | SEC_FILE_READ_EA | SEC_FILE_WRITE_EA | \
265 SEC_FILE_READ_ATTRIBUTE | SEC_FILE_WRITE_ATTRIBUTE | 0)
266
267 struct test_ipc_async_credits_loop;
268
269 struct test_ipc_async_credits_state {
270 struct torture_context *tctx;
271 struct smbXcli_conn *conn;
272 struct smbXcli_session *session;
273 struct smbXcli_tcon *tcon;
274 uint32_t timeout_msec;
275 uint16_t pid;
276 const char *pipe_name;
277
278 size_t num_loops;
279 struct test_ipc_async_credits_loop *loops;
280 size_t num_status_received;
281
282 size_t num_status_pending;
283 size_t num_status_insufficient;
284
285 bool stop;
286 };
287
288 struct test_ipc_async_credits_loop {
289 size_t idx;
290 struct test_ipc_async_credits_state *state;
291 struct tevent_req *req;
292 size_t num_started;
293
294 uint32_t max_data;
295 uint64_t fid_persistent;
296 uint64_t fid_volatile;
297
298 struct tevent_immediate *im;
299
300 NTSTATUS status;
301 };
302
303 static void test_ipc_async_credits_read_loop_do(
304 struct test_ipc_async_credits_loop *loop);
305
306 static void test_ipc_async_credits_read_loop_start(struct tevent_context *ctx,
307 struct tevent_immediate *im,
308 void *private_data)
309 {
310 struct test_ipc_async_credits_loop *loop = private_data;
311
312 test_ipc_async_credits_read_loop_do(loop);
313 }
314
315 static void test_ipc_async_credits_read_loop_done(struct tevent_req *req);
316
317 static void test_ipc_async_credits_read_loop_do(
318 struct test_ipc_async_credits_loop *loop)
319 {
320 struct test_ipc_async_credits_state *state = loop->state;
321 bool ok;
322
323 loop->num_started += 1;
324 loop->req = smb2cli_read_send(state->loops,
325 state->tctx->ev,
326 state->conn,
327 state->timeout_msec,
328 state->session,
329 state->tcon,
330 loop->max_data, /* length */
331 0, /* offset */
332 loop->fid_persistent,
333 loop->fid_volatile,
334 0, /* minimum_count */
335 0); /* remaining_bytes */
336 torture_assert_not_null_goto(
337 state->tctx, loop->req, ok, asserted, "smb2cli_read_send");
338 smb2cli_read_set_notify_async(loop->req);
339 tevent_req_set_callback(loop->req,
340 test_ipc_async_credits_read_loop_done,
341 loop);
342
343 return;
344 asserted:
345 (void)ok;
346 state->stop = true;
347 }
348
349 static void test_ipc_async_credits_read_loop_done(struct tevent_req *req)
350 {
351 struct test_ipc_async_credits_loop *loop = _tevent_req_callback_data(
352 req);
353 struct test_ipc_async_credits_state *state = loop->state;
354 TALLOC_CTX *frame = talloc_stackframe();
355 uint8_t *data = NULL;
356 uint32_t data_length = 0;
357 bool ok;
358 bool was_pending = false;
359 bool in_progress = false;
360
361 torture_assert_goto(state->tctx,
362 loop->req == req,
363 ok,
364 asserted,
365 __location__);
366
367 if (NT_STATUS_EQUAL(loop->status, NT_STATUS_PENDING)) {
368 was_pending = true;
369 }
370
371 loop->status = smb2cli_read_recv(req, frame, &data, &data_length);
372 #if 0
373 torture_comment(state->tctx,
374 "loop->status: %s\n",
375 nt_errstr(loop->status));
376 #endif
377 in_progress = tevent_req_is_in_progress(req);
378 state->num_status_received += 1;
379
380 if (NT_STATUS_EQUAL(loop->status, NT_STATUS_PENDING)) {
381 state->num_status_pending += 1;
382 torture_assert_goto(state->tctx,
383 in_progress,
384 ok,
385 asserted,
386 __location__);
387 goto done;
388 }
389 loop->req = NULL;
390 /* We will cancel the request, don't free it. */
391 torture_assert_goto(state->tctx,
392 !in_progress,
393 ok,
394 asserted,
395 __location__);
396 if (was_pending &&
397 NT_STATUS_EQUAL(loop->status, NT_STATUS_CANCELLED))
398 {
399 goto done;
400 }
401 torture_assert_ntstatus_equal_goto(state->tctx,
402 loop->status,
403 NT_STATUS_INSUFFICIENT_RESOURCES,
404 ok,
405 asserted,
406 __location__);
407 state->num_status_insufficient += 1;
408
409 done:
410 TALLOC_FREE(frame);
411 return;
412 asserted:
413 (void)ok;
414 state->stop = true;
415 TALLOC_FREE(frame);
416 }
417
418 static const uint8_t dcerpc_bind_lsa_bytes[] = {
419 0x05, 0x00, 0x0b, 0x03, 0x10, 0x00, 0x00, 0x00,
420 0xa0, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
421 0xb8, 0x10, 0xb8, 0x10, 0x00, 0x00, 0x00, 0x00,
422 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
423 0x78, 0x57, 0x34, 0x12, 0x34, 0x12, 0xcd, 0xab,
424 0xef, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
425 0x00, 0x00, 0x00, 0x00, 0x04, 0x5d, 0x88, 0x8a,
426 0xeb, 0x1c, 0xc9, 0x11, 0x9f, 0xe8, 0x08, 0x00,
427 0x2b, 0x10, 0x48, 0x60, 0x02, 0x00, 0x00, 0x00,
428 0x01, 0x00, 0x01, 0x00, 0x78, 0x57, 0x34, 0x12,
429 0x34, 0x12, 0xcd, 0xab, 0xef, 0x00, 0x01, 0x23,
430 0x45, 0x67, 0x89, 0xab, 0x00, 0x00, 0x00, 0x00,
431 0x33, 0x05, 0x71, 0x71, 0xba, 0xbe, 0x37, 0x49,
432 0x83, 0x19, 0xb5, 0xdb, 0xef, 0x9c, 0xcc, 0x36,
433 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,
434 0x78, 0x57, 0x34, 0x12, 0x34, 0x12, 0xcd, 0xab,
435 0xef, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
436 0x00, 0x00, 0x00, 0x00, 0x2c, 0x1c, 0xb7, 0x6c,
437 0x12, 0x98, 0x40, 0x45, 0x03, 0x00, 0x00, 0x00,
438 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00
439 };
440
441 static bool test_ipc_max_async_credits(struct torture_context *tctx,
442 struct smb2_tree *trees[],
443 size_t num_trees,
444 size_t num_loops,
445 size_t max_data)
446 {
447 struct test_ipc_async_credits_state *states[num_trees];
448 struct timeval timeout_tv = { .tv_sec = 0, };
449 struct tevent_req *timeout_req = NULL;
450 bool stop_loop = false;
451 NTSTATUS status;
452 size_t i, t;
453 bool ok;
454
455 for (t = 0; t < num_trees; t++) {
456 uint16_t cur_credits;
457
458 states[t] = NULL;
459
460 cur_credits = smb2cli_conn_get_cur_credits(
461 trees[t]->session->transport->conn);
462 torture_assert_int_equal_goto(
463 tctx,
464 cur_credits,
465 num_loops,
466 ok,
467 out,
468 "Invalid number of granted credits");
469 }
470
471 torture_assert_int_not_equal_goto(
472 tctx, num_trees, 0, ok, out, "Invalid number of trees");
473
474 for (i = 0; i < num_trees; i++) {
475 struct smb2_tree *tree = trees[i];
476 struct test_ipc_async_credits_state *state = NULL;
477
478 state = talloc_zero(tctx,
479 struct test_ipc_async_credits_state);
480 torture_assert_not_null_goto(
481 tctx, state, ok, out, "tevent_zero failed");
482
483 state->tctx = tctx;
484 state->conn = tree->session->transport->conn;
485 state->session = tree->session->smbXcli;
486 state->tcon = tree->smbXcli;
487 state->timeout_msec = 0; /* 10 * 1000; */
488 state->pid = 0;
489 state->pipe_name = NDR_LSARPC_NAME;
490
491 state->num_loops = num_loops;
492 state->loops = talloc_zero_array(
493 state,
494 struct test_ipc_async_credits_loop,
495 state->num_loops);
496 torture_assert_not_null_goto(tctx,
497 state->loops,
498 ok,
499 out,
500 "tevent_zero_array failed");
501
502 states[i] = state;
503 }
504
505 for (i = 0; i < num_loops; i++) {
506 for (t = 0; t < num_trees; t++) {
507 struct test_ipc_async_credits_state
508 *state = states[t];
509 struct test_ipc_async_credits_loop
510 *loop = &state->loops[i];
511 DATA_BLOB in_input_buffer = data_blob_const(
512 dcerpc_bind_lsa_bytes,
513 ARRAY_SIZE(dcerpc_bind_lsa_bytes));
514 DATA_BLOB in_output_buffer = data_blob_null;
515 DATA_BLOB out_input_buffer = data_blob_null;
516 DATA_BLOB out_output_buffer = data_blob_null;
517
518 loop->idx = i;
519 loop->state = state;
520 loop->status = NT_STATUS_UNSUCCESSFUL;
521 loop->max_data = max_data;
522
523 loop->im = tevent_create_immediate(state->loops);
524 torture_assert_not_null_goto(
525 tctx,
526 loop->im,
527 ok,
528 out,
529 "tevent_create_immediate failed");
530
531 status = smb2cli_create(
532 state->conn,
533 state->timeout_msec,
534 state->session,
535 state->tcon,
536 state->pipe_name,
537 SMB2_OPLOCK_LEVEL_NONE,
538 SMB2_IMPERSONATION_IMPERSONATION,
539 SMBXCLI_NP_DESIRED_ACCESS,
540 0, /* file_attributes */
541 FILE_SHARE_READ | FILE_SHARE_WRITE,
542 FILE_OPEN,
543 0, /* create_options */
544 NULL, /* blobs */
545 &loop->fid_persistent,
546 &loop->fid_volatile,
547 NULL, /* cr */
548 state, /* mem_ctx */
549 NULL, /* ret_blobs */
550 NULL); /* psymlink */
551 torture_assert_ntstatus_ok_goto(
552 tctx, status, ok, out, "smb2cli_create failed");
553
554 status = smb2cli_ioctl(
555 state->conn,
556 state->timeout_msec,
557 state->session,
558 state->tcon,
559 loop->fid_persistent,
560 loop->fid_volatile,
561 FSCTL_NAMED_PIPE_READ_WRITE,
562 0, /* in_max_input_length */
563 &in_input_buffer,
564 1024, /* in_max_output_length */
565 &in_output_buffer,
566 SMB2_IOCTL_FLAG_IS_FSCTL,
567 state, /* mem_ctx */
568 &out_input_buffer,
569 &out_output_buffer);
570 torture_assert_ntstatus_ok_goto(
571 tctx,
572 status,
573 ok,
574 out,
575 "FSCTL_NAMED_PIPE_READ_WRITE failed");
576 }
577 }
578
579 for (i = 0; i < num_loops; i++) {
580 for (t = 0; t < num_trees; t++) {
581 struct test_ipc_async_credits_state
582 *state = states[t];
583 struct test_ipc_async_credits_loop
584 *loop = &state->loops[i];
585
586 tevent_schedule_immediate(
587 loop->im,
588 tctx->ev,
589 test_ipc_async_credits_read_loop_start,
590 loop);
591 }
592 }
593
594 timeout_tv = timeval_current_ofs(10, 0);
595 timeout_req = tevent_wakeup_send(tctx, tctx->ev, timeout_tv);
596 torture_assert_goto(tctx, timeout_req, ok, out, "tevent_wakeup_send");
597
598 /* Loop to send and receive packets */
599 while (!stop_loop) {
600 size_t loops_ready = 0;
601 int rc;
602
603 rc = tevent_loop_once(tctx->ev);
604 torture_assert_int_equal_goto(
605 tctx, rc, 0, ok, out, "tevent_loop_once");
606
607 ok = tevent_req_is_in_progress(timeout_req);
608 torture_assert_goto(tctx, ok, ok, out, "timeout_req");
609
610 for (i = 0; i < num_trees; i++) {
611 struct test_ipc_async_credits_state
612 *state = states[i];
613 if (state->stop) {
614 stop_loop = true;
615 }
616
617 if (state->num_status_received >= state->num_loops) {
618 loops_ready += 1;
619 }
620 }
621
622 if (loops_ready >= num_trees) {
623 stop_loop = true;
624 }
625 }
626
627 for (t = 0; t < num_trees; t++) {
628 struct test_ipc_async_credits_state *state = states[t];
629 size_t max_async_credits = 512;
630 size_t max_credits = max_async_credits + 2;
631
632 torture_assert_goto(state->tctx, !state->stop, ok, out, "");
633 torture_assert_int_equal_goto(state->tctx,
634 state->num_status_received,
635 state->num_loops,
636 ok,
637 out,
638 "");
639 torture_assert_int_equal_goto(state->tctx,
640 state->num_status_pending,
641 max_async_credits - 1,
642 ok,
643 out,
644 "");
645 torture_assert_int_equal_goto(state->tctx,
646 state->num_status_insufficient,
647 max_credits - max_async_credits + 1,
648 ok,
649 out,
650 "");
651 }
652
653 for (t = 0; t < num_trees; t++) {
654 struct test_ipc_async_credits_state *state = states[t];
655
656 for (i = 0; i < num_loops; i++) {
657 struct test_ipc_async_credits_loop
658 *loop = &state->loops[i];
659 if (loop->req != NULL) {
660 ok = tevent_req_cancel(loop->req);
661 torture_assert_goto(tctx,
662 ok,
663 ok,
664 out,
665 "tevent_req_cancel failed");
666 }
667 }
668 }
669
670 for (t = 0; t < num_trees; t++) {
671 struct test_ipc_async_credits_state *state = states[t];
672
673 for (i = 0; i < num_loops; i++) {
674 struct test_ipc_async_credits_loop
675 *loop = &state->loops[i];
676
677 if (NT_STATUS_EQUAL(loop->status,
678 NT_STATUS_INSUFFICIENT_RESOURCES))
679 {
680 continue;
681 }
682
683 if (loop->req != NULL) {
684 ok = tevent_req_poll(loop->req, tctx->ev);
685 torture_assert_goto(state->tctx,
686 ok,
687 ok,
688 out,
689 "tevent_req_poll failed");
690 }
691 torture_assert_ntstatus_equal_goto(
692 tctx,
693 loop->status,
694 NT_STATUS_CANCELLED,
695 ok,
696 out,
697 __location__);
698 }
699 }
700
701 ok = true;
702 out:
703 for (t = 0; t < num_trees; t++) {
704 TALLOC_FREE(states[t]);
705 }
706
707 return ok;
708 }
709
710 static bool test_ipc_max_data_zero(struct torture_context *tctx,
711 struct smb2_tree *tree0)
712 {
713 struct smb2_transport *transport0 = tree0->session->transport;
714 struct smbcli_options options = transport0->options;
715 struct smb2_tree *tree1 = NULL;
716 struct smb2_tree *trees[1] = {};
717 const char *host = torture_setting_string(tctx, "host", NULL);
718 const char *share = "IPC$";
719 /* We want to check if we IPC connections add to "max async credits" */
720 uint16_t max_async_credits = torture_setting_int(
721 tctx,
722 "maxasynccredits",
723 512 /* lpcfg_smb2_max_async_credits(tctx->lp_ctx) */);
724 bool ok = false;
725 NTSTATUS status;
726 uint16_t max_credits = max_async_credits + 2;
727
728 options.client_guid = GUID_random();
729 options.max_credits = max_credits;
730
731 /* Create connection to IPC$ */
732 status = smb2_connect(tctx,
733 host,
734 share,
735 tctx->lp_ctx,
736 lpcfg_resolve_context(tctx->lp_ctx),
737 samba_cmdline_get_creds(),
738 &tree1,
739 tctx->ev,
740 &options,
741 lpcfg_socket_options(tctx->lp_ctx),
742 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
743 torture_assert_ntstatus_ok_goto(
744 tctx, status, ok, out, "smb2_connect failed");
745
746 trees[0] = tree1;
747 ok = test_ipc_max_async_credits(tctx,
748 trees,
749 ARRAY_SIZE(trees),
750 max_credits,
751 0);
752 out:
753 if (tree1 != NULL) {
754 smbXcli_conn_disconnect(tree1->session->transport->conn,
755 NT_STATUS_LOCAL_DISCONNECT);
756 }
757 smbXcli_conn_disconnect(tree0->session->transport->conn,
758 NT_STATUS_LOCAL_DISCONNECT);
759
760 return ok;
761 }
762
763 static bool test_1conn_ipc_max_async_credits(struct torture_context *tctx,
764 struct smb2_tree *tree0)
765 {
766 struct smb2_transport *transport0 = tree0->session->transport;
767 struct smbcli_options options = transport0->options;
768 struct smb2_tree *tree1 = NULL;
769 struct smb2_tree *trees[1] = {};
770 const char *host = torture_setting_string(tctx, "host", NULL);
771 const char *share = "IPC$";
772 /* We want to check if we IPC connections add to "max async credits" */
773 uint16_t max_async_credits = torture_setting_int(
774 tctx,
775 "maxasynccredits",
776 512 /* lpcfg_smb2_max_async_credits(tctx->lp_ctx) */);
777 bool ok = false;
778 NTSTATUS status;
779 uint16_t max_credits = max_async_credits + 2;
780
781 options.client_guid = GUID_random();
782 options.max_credits = max_credits;
783
784 /* Create connection to IPC$ */
785 status = smb2_connect(tctx,
786 host,
787 share,
788 tctx->lp_ctx,
789 lpcfg_resolve_context(tctx->lp_ctx),
790 samba_cmdline_get_creds(),
791 &tree1,
792 tctx->ev,
793 &options,
794 lpcfg_socket_options(tctx->lp_ctx),
795 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
796 torture_assert_ntstatus_ok_goto(
797 tctx, status, ok, out, "smb2_connect failed");
798
799 trees[0] = tree1;
800 ok = test_ipc_max_async_credits(tctx,
801 trees,
802 ARRAY_SIZE(trees),
803 max_credits,
804 1024); /* max_data */
805 out:
806 if (tree1 != NULL) {
807 smbXcli_conn_disconnect(tree1->session->transport->conn,
808 NT_STATUS_LOCAL_DISCONNECT);
809 }
810 smbXcli_conn_disconnect(transport0->conn,
811 NT_STATUS_LOCAL_DISCONNECT);
812
813 return ok;
814 }
815
816 static bool test_2conn_ipc_max_async_credits(struct torture_context *tctx,
817 struct smb2_tree *tree0)
818 {
819 struct smb2_transport *transport0 = tree0->session->transport;
820 struct smbcli_options options1 = transport0->options;
821 struct smbcli_options options2 = transport0->options;
822 struct smb2_tree *tree1 = NULL;
823 struct smb2_tree *tree2 = NULL;
824 struct smb2_tree *trees[2] = {};
825 const char *host = torture_setting_string(tctx, "host", NULL);
826 const char *share = "IPC$";
827 /* We want to check if we IPC connections add to "max async credits" */
828 uint16_t max_async_credits = torture_setting_int(
829 tctx,
830 "maxasynccredits",
831 512 /* lpcfg_smb2_max_async_credits(tctx->lp_ctx) */);
832 bool ok = false;
833 NTSTATUS status;
834 uint16_t max_credits = max_async_credits + 2;
835
836 /*
837 * If we run the test against the source4 rpc_server, then we get a lot
838 * of:
839 * single_accept_connection: accept: NT_STATUS_TOO_MANY_OPENED_FILES
840 */
841 if (torture_setting_bool(tctx, "samba4", false)) {
842 torture_skip(tctx, "Skip test against Samba4");
843 }
844
845 options1.client_guid = GUID_random();
846 options1.max_credits = max_credits;
847
848 /* Create connection to IPC$ */
849 status = smb2_connect(tctx,
850 host,
851 share,
852 tctx->lp_ctx,
853 lpcfg_resolve_context(tctx->lp_ctx),
854 samba_cmdline_get_creds(),
855 &tree1,
856 tctx->ev,
857 &options1,
858 lpcfg_socket_options(tctx->lp_ctx),
859 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
860 torture_assert_ntstatus_ok_goto(
861 tctx, status, ok, out, "smb2_connect failed");
862
863 options2.client_guid = GUID_random();
864 options2.max_credits = max_credits;
865
866 /* Create connection to IPC$ */
867 status = smb2_connect(tctx,
868 host,
869 share,
870 tctx->lp_ctx,
871 lpcfg_resolve_context(tctx->lp_ctx),
872 samba_cmdline_get_creds(),
873 &tree2,
874 tctx->ev,
875 &options2,
876 lpcfg_socket_options(tctx->lp_ctx),
877 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
878 torture_assert_ntstatus_ok_goto(
879 tctx, status, ok, out, "smb2_connect failed");
880
881 trees[0] = tree1;
882 trees[1] = tree2;
883
884 ok = test_ipc_max_async_credits(tctx,
885 trees,
886 ARRAY_SIZE(trees),
887 max_credits,
888 1024); /* max_data */
889 out:
890 if (tree2 != NULL) {
891 smbXcli_conn_disconnect(tree2->session->transport->conn,
892 NT_STATUS_LOCAL_DISCONNECT);
893 }
894 if (tree1 != NULL) {
895 smbXcli_conn_disconnect(tree1->session->transport->conn,
896 NT_STATUS_LOCAL_DISCONNECT);
897 }
898 smbXcli_conn_disconnect(transport0->conn,
899 NT_STATUS_LOCAL_DISCONNECT);
900
901 return ok;
902 }
903
904 static bool test_multichannel_ipc_max_async_credits(
905 struct torture_context *tctx,
906 struct smb2_tree *tree0)
907 {
908 uint16_t max_async_credits = torture_setting_int(
909 tctx,
910 "maxasynccredits",
911 512 /* lpcfg_smb2_max_async_credits(tctx->lp_ctx) */);
912 const char *host = torture_setting_string(tctx, "host", NULL);
913 const char *share = "IPC$";
914 struct smbcli_options options = tree0->session->transport->options;
915 struct smb2_session *session1a = NULL;
916 struct smb2_session *session1b = NULL;
917 struct smb2_tree *tree1a = NULL;
918 struct smb2_tree *tree1b_base = NULL;
919 struct smb2_transport *transport1b = NULL;
920 struct smb2_tree *tree1b = NULL;
921 struct smb2_tree *trees[2] = {};
922 bool ok = false;
923 NTSTATUS status;
924 uint16_t max_credits = max_async_credits + 2;
925
926 /*
927 * If we run the test against the source4 rpc_server, then we get a lot
928 * of:
929 * single_accept_connection: accept: NT_STATUS_TOO_MANY_OPENED_FILES
930 */
931 if (torture_setting_bool(tctx, "samba4", false)) {
932 torture_skip(tctx, "Skip test against Samba4");
933 }
934
935 options.client_guid = GUID_random();
936 options.max_credits = max_credits;
937
938 /* Create connection to IPC$ */
939 status = smb2_connect(tctx,
940 host,
941 share,
942 tctx->lp_ctx,
943 lpcfg_resolve_context(tctx->lp_ctx),
944 samba_cmdline_get_creds(),
945 &tree1a,
946 tctx->ev,
947 &options,
948 lpcfg_socket_options(tctx->lp_ctx),
949 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
950 torture_assert_ntstatus_ok_goto(
951 tctx, status, ok, out, "smb2_connect failed");
952 session1a = tree1a->session;
953
954 /* Connect multichannel */
955 options.only_negprot = true;
956
957 status = smb2_connect(tree1a,
958 host,
959 share,
960 tctx->lp_ctx,
961 lpcfg_resolve_context(tctx->lp_ctx),
962 samba_cmdline_get_creds(),
963 &tree1b_base,
964 tctx->ev,
965 &options,
966 lpcfg_socket_options(tctx->lp_ctx),
967 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
968 torture_assert_ntstatus_ok_goto(
969 tctx, status, ok, out, "smb2_connect failed");
970 transport1b = tree1b_base->session->transport;
971
972 session1b = smb2_session_channel(transport1b,
973 lpcfg_gensec_settings(tctx,
974 tctx->lp_ctx),
975 tree1b_base,
976 session1a);
977 torture_assert_not_null_goto(
978 tctx, session1b, ok, out, "smb2_session_channel failed");
979
980 status = smb2_session_setup_spnego(session1b,
981 samba_cmdline_get_creds(),
982 0);
983 torture_assert_ntstatus_ok_goto(
984 tctx, status, ok, out, "smb2_session_setup_spnego failed");
985
986 tree1b = smb2_tree_channel(tree1a, tree1b_base, false, session1b);
987 torture_assert_not_null_goto(
988 tctx, tree1b, ok, out, "smb2_tree_channel failed");
989
990 trees[0] = tree1a;
991 trees[1] = tree1b;
992
993 ok = test_ipc_max_async_credits(tctx,
994 trees,
995 ARRAY_SIZE(trees),
996 max_credits,
997 1024); /* max_data */
998 out:
999 if (tree1a != NULL) {
1000 smbXcli_conn_disconnect(tree1a->session->transport->conn,
1001 NT_STATUS_LOCAL_DISCONNECT);
1002 }
1003 if (transport1b != NULL) {
1004 smbXcli_conn_disconnect(transport1b->conn,
1005 NT_STATUS_LOCAL_DISCONNECT);
1006 }
1007 smbXcli_conn_disconnect(tree0->session->transport->conn,
1008 NT_STATUS_LOCAL_DISCONNECT);
1009
1010 return ok;
1011 }
1012
1013
1014 #define TESTDIR "test_max_async_credits"
1015
1016 #define SMBXCLI_NOTIFY_DIR_DESIRED_ACCESS \
1017 (SEC_STD_READ_CONTROL | SEC_FILE_READ_DATA | SEC_FILE_WRITE_DATA | \
1018 SEC_FILE_APPEND_DATA | SEC_FILE_READ_EA | SEC_FILE_WRITE_EA | \
1019 SEC_FILE_READ_ATTRIBUTE | SEC_FILE_WRITE_ATTRIBUTE | 0)
1020
1021 struct test_notify_async_credit_loop;
1022
1023 struct test_notify_async_credit_state {
1024 struct torture_context *tctx;
1025 struct smbXcli_conn *conn;
1026 struct smbXcli_session *session;
1027 struct smbXcli_tcon *tcon;
1028 uint32_t timeout_msec;
1029 uint16_t pid;
1030 const char *fname;
1031
1032 size_t num_loops;
1033 struct test_notify_async_credit_loop *loops;
1034 size_t num_status_received;
1035 size_t num_started;
1036
1037 size_t num_status_pending;
1038 size_t num_status_insufficient;
1039
1040 bool stop;
1041 };
1042
1043 struct test_notify_async_credit_loop {
1044 size_t idx;
1045 struct test_notify_async_credit_state *state;
1046 struct tevent_req *req;
1047
1048 uint64_t fid_persistent;
1049 uint64_t fid_volatile;
1050
1051 struct tevent_immediate *im;
1052
1053 NTSTATUS status;
1054 };
1055
1056 static void test_async_notify_loop_do(
1057 struct test_notify_async_credit_loop *loop);
1058
1059 static void test_async_notify_loop_start(struct tevent_context *ctx,
1060 struct tevent_immediate *im,
1061 void *private_data)
1062 {
1063 struct test_notify_async_credit_loop *loop = private_data;
1064
1065 test_async_notify_loop_do(loop);
1066 }
1067
1068 static void test_async_notify_loop_done(struct tevent_req *req);
1069
1070 static void test_async_notify_loop_do(struct test_notify_async_credit_loop *loop)
1071 {
1072 struct test_notify_async_credit_state *state = loop->state;
1073 bool ok;
1074
1075 state->num_started += 1;
1076 loop->req = smb2cli_notify_send(state->loops,
1077 state->tctx->ev,
1078 state->conn,
1079 state->timeout_msec,
1080 state->session,
1081 state->tcon,
1082 10000, /* out_buffer_length */
1083 loop->fid_persistent,
1084 loop->fid_volatile,
1085 FILE_NOTIFY_CHANGE_NAME |
1086 FILE_NOTIFY_CHANGE_ATTRIBUTES |
1087 FILE_NOTIFY_CHANGE_CREATION,
1088 false /* recursive */
1089 );
1090 torture_assert_not_null_goto(state->tctx,
1091 loop->req,
1092 ok,
1093 asserted,
1094 "smb2cli_notify_send failed");
1095 smb2cli_notify_set_notify_async(loop->req);
1096 tevent_req_set_callback(loop->req,
1097 test_async_notify_loop_done,
1098 loop);
1099
1100
1101 return;
1102 asserted:
1103 torture_comment(state->tctx, "Assserted for idx=%zu\n", loop->idx);
1104 (void)ok;
1105 state->stop = true;
1106 }
1107
1108 static void test_async_notify_loop_done(struct tevent_req *req)
1109 {
1110 struct test_notify_async_credit_loop *loop = _tevent_req_callback_data(
1111 req);
1112 struct test_notify_async_credit_state *state = loop->state;
1113 TALLOC_CTX *frame = talloc_stackframe();
1114 uint8_t *data = NULL;
1115 uint32_t data_length = 0;
1116 bool ok;
1117 bool was_pending = false;
1118 bool in_progress = false;
1119
1120 torture_assert_goto(state->tctx,
1121 loop->req == req,
1122 ok,
1123 asserted,
1124 __location__);
1125
1126 if (NT_STATUS_EQUAL(loop->status, NT_STATUS_PENDING)) {
1127 was_pending = true;
1128 }
1129
1130 loop->status = smb2cli_notify_recv(req, frame, &data, &data_length);
1131 #if 0
1132 torture_comment(state->tctx,
1133 "loop->status: %s\n",
1134 nt_errstr(loop->status));
1135 #endif
1136 in_progress = tevent_req_is_in_progress(req);
1137 state->num_status_received += 1;
1138
1139 if (NT_STATUS_EQUAL(loop->status, NT_STATUS_PENDING)) {
1140 state->num_status_pending += 1;
1141 torture_assert_goto(state->tctx,
1142 in_progress,
1143 ok,
1144 asserted,
1145 __location__);
1146 goto done;
1147 }
1148 loop->req = NULL;
1149 TALLOC_FREE(req);
1150 torture_assert_goto(state->tctx,
1151 !in_progress,
1152 ok,
1153 asserted,
1154 __location__);
1155 if (was_pending &&
1156 NT_STATUS_EQUAL(loop->status, NT_STATUS_NOTIFY_CLEANUP))
1157 {
1158 goto done;
1159 }
1160 torture_assert_ntstatus_equal_goto(state->tctx,
1161 loop->status,
1162 NT_STATUS_INSUFFICIENT_RESOURCES,
1163 ok,
1164 asserted,
1165 __location__);
1166 state->num_status_insufficient += 1;
1167
1168 done:
1169 TALLOC_FREE(frame);
1170 return;
1171 asserted:
1172 (void)ok;
1173 state->stop = true;
1174 TALLOC_FREE(frame);
1175 }
1176
1177 static bool test_notify_max_async_credits(struct torture_context *tctx,
1178 struct smb2_tree *trees[],
1179 size_t num_trees,
1180 size_t num_loops)
1181 {
1182 struct test_notify_async_credit_state *states[num_trees];
1183 struct timeval timeout_tv = { .tv_sec = 0, };
1184 struct tevent_req *timeout_req = NULL;
1185 bool stop_loop = false;
1186 NTSTATUS status;
1187 size_t i, t;
1188 bool ok;
1189
1190 for (t = 0; t < num_trees; t++) {
1191 uint16_t cur_credits;
1192
1193 states[t] = NULL;
1194
1195 cur_credits = smb2cli_conn_get_cur_credits(
1196 trees[t]->session->transport->conn);
1197 torture_assert_int_equal_goto(
1198 tctx,
1199 cur_credits,
1200 num_loops,
1201 ok,
1202 out,
1203 "Invalid number of granted credits");
1204 }
1205
1206 torture_assert_int_not_equal_goto(
1207 tctx, num_trees, 0, ok, out, "Invalid number of trees");
1208
1209 for (i = 0; i < num_trees; i++) {
1210 struct smb2_tree *tree = trees[i];
1211 struct test_notify_async_credit_state *state = NULL;
1212
1213 state = talloc_zero(tctx,
1214 struct test_notify_async_credit_state);
1215 torture_assert_not_null_goto(
1216 tctx, state, ok, out, "tevent_zero failed");
1217
1218 state->tctx = tctx;
1219 state->conn = tree->session->transport->conn;
1220 state->session = tree->session->smbXcli;
1221 state->tcon = tree->smbXcli;
1222 state->timeout_msec = tree->session->transport->options
1223 .request_timeout * 1000;
1224 state->pid = 0;
1225 state->fname = TESTDIR;
1226
1227 state->num_loops = num_loops;
1228 state->loops = talloc_zero_array(
1229 state,
1230 struct test_notify_async_credit_loop,
1231 state->num_loops);
1232 torture_assert_not_null_goto(tctx,
1233 state->loops,
1234 ok,
1235 out,
1236 "tevent_zero_array failed");
1237
1238 states[i] = state;
1239 }
1240
1241 for (i = 0; i < num_loops; i++) {
1242 for (t = 0; t < num_trees; t++) {
1243 struct test_notify_async_credit_state
1244 *state = states[t];
1245 struct test_notify_async_credit_loop
1246 *loop = &state->loops[i];
1247
1248 loop->idx = i;
1249 loop->state = state;
1250 loop->status = NT_STATUS_UNSUCCESSFUL;
1251
1252 loop->im = tevent_create_immediate(state->loops);
1253 torture_assert_not_null_goto(
1254 tctx,
1255 loop->im,
1256 ok,
1257 out,
1258 "tevent_create_immediate failed");
1259
1260 /* Open directory */
1261 status = smb2cli_create(
1262 state->conn,
1263 state->timeout_msec,
1264 state->session,
1265 state->tcon,
1266 state->fname,
1267 SMB2_OPLOCK_LEVEL_NONE,
1268 SMB2_IMPERSONATION_IMPERSONATION,
1269 SMBXCLI_NOTIFY_DIR_DESIRED_ACCESS,
1270 0, /* file_attributes */
1271 FILE_SHARE_READ | FILE_SHARE_WRITE,
1272 FILE_OPEN,
1273 FILE_DIRECTORY_FILE,
1274 NULL, /* blobs */
1275 &loop->fid_persistent,
1276 &loop->fid_volatile,
1277 NULL, /* cr */
1278 state, /* mem_ctx */
1279 NULL, /* ret_blobs */
1280 NULL); /* psymlink */
1281 torture_assert_ntstatus_ok_goto(
1282 tctx, status, ok, out, "smb2cli_create failed");
1283 }
1284 }
1285
1286 for (i = 0; i < num_loops; i++) {
1287 for (t = 0; t < num_trees; t++) {
1288 struct test_notify_async_credit_state
1289 *state = states[t];
1290 struct test_notify_async_credit_loop
1291 *loop = &state->loops[i];
1292
1293 tevent_schedule_immediate(loop->im,
1294 tctx->ev,
1295 test_async_notify_loop_start,
1296 loop);
1297 }
1298 }
1299
1300 timeout_tv = timeval_current_ofs(10, 0);
1301 timeout_req = tevent_wakeup_send(tctx, tctx->ev, timeout_tv);
1302 torture_assert_goto(tctx, timeout_req, ok, out, "tevent_wakeup_send");
1303
1304 /* Loop to send and receive packets */
1305 while (!stop_loop) {
1306 size_t loops_ready = 0;
1307 int rc;
1308
1309 rc = tevent_loop_once(tctx->ev);
1310 torture_assert_int_equal_goto(
1311 tctx, rc, 0, ok, out, "tevent_loop_once");
1312
1313 ok = tevent_req_is_in_progress(timeout_req);
1314 torture_assert_goto(tctx, ok, ok, out, "timeout_req");
1315
1316 for (i = 0; i < num_trees; i++) {
1317 struct test_notify_async_credit_state
1318 *state = states[i];
1319 if (state->stop) {
1320 stop_loop = true;
1321 }
1322
1323 if (state->num_status_received >= state->num_loops) {
1324 loops_ready += 1;
1325 }
1326 }
1327
1328 if (loops_ready >= num_trees) {
1329 stop_loop = true;
1330 }
1331 }
1332
1333 for (t = 0; t < num_trees; t++) {
1334 struct test_notify_async_credit_state *state = states[t];
1335 size_t max_async_credits = 512;
1336 size_t max_credits = max_async_credits + 2;
1337
1338 torture_assert_goto(state->tctx, !state->stop, ok, out, "");
1339 torture_assert_int_equal_goto(state->tctx,
1340 state->num_status_received,
1341 state->num_loops,
1342 ok,
1343 out,
1344 "");
1345 torture_assert_int_equal_goto(state->tctx,
1346 state->num_status_pending,
1347 max_async_credits - 1,
1348 ok,
1349 out,
1350 "");
1351 torture_assert_int_equal_goto(state->tctx,
1352 state->num_status_insufficient,
1353 max_credits - max_async_credits + 1,
1354 ok,
1355 out,
1356 "");
1357 }
1358
1359 for (t = 0; t < num_trees; t++) {
1360 struct test_notify_async_credit_state *state = states[t];
1361
1362 for (i = 0; i < num_loops; i++) {
1363 struct test_notify_async_credit_loop
1364 *loop = &state->loops[i];
1365 struct smb2_handle h = {{
1366 loop->fid_persistent,
1367 loop->fid_volatile,
1368 }};
1369
1370 /* the close cancels any pending notify request */
1371 status = smb2_util_close(trees[t], h);
1372 torture_assert_ntstatus_ok_goto(
1373 tctx,
1374 status,
1375 ok,
1376 out,
1377 "smb2_util_close failed");
1378 }
1379 }
1380
1381 for (t = 0; t < num_trees; t++) {
1382 struct test_notify_async_credit_state *state = states[t];
1383
1384 for (i = 0; i < num_loops; i++) {
1385 struct test_notify_async_credit_loop
1386 *loop = &state->loops[i];
1387
1388 if (NT_STATUS_EQUAL(loop->status,
1389 NT_STATUS_INSUFFICIENT_RESOURCES))
1390 {
1391 continue;
1392 }
1393
1394 if (loop->req != NULL) {
1395 ok = tevent_req_poll(loop->req, state->tctx->ev);
1396 torture_assert_goto(state->tctx,
1397 ok,
1398 ok,
1399 out,
1400 "tevent_req_poll failed");
1401 }
1402 torture_assert_ntstatus_equal_goto(
1403 tctx,
1404 loop->status,
1405 NT_STATUS_NOTIFY_CLEANUP,
1406 ok,
1407 out,
1408 __location__);
1409 }
1410 }
1411
1412 ok = true;
1413 out:
1414 for (t = 0; t < num_trees; t++) {
1415 TALLOC_FREE(states[t]);
1416 }
1417
1418 return ok;
1419 }
1420
1421 static bool test_1conn_notify_max_async_credits(struct torture_context *tctx,
1422 struct smb2_tree *tree0)
1423 {
1424 struct smb2_tree *trees[1] = { tree0 };
1425 struct smb2_handle dh = {{}};
1426 uint16_t max_async_credits = torture_setting_int(
1427 tctx,
1428 "maxasynccredits",
1429 512 /* lpcfg_smb2_max_async_credits(tctx->lp_ctx) */);
1430 bool ok = false;
1431 NTSTATUS status;
1432 uint16_t max_credits = max_async_credits + 2;
1433
1434 smb2_transport_credits_ask_num(tree0->session->transport, max_credits);
1435
1436 /* Cleanup TESTDIR */
1437 smb2_deltree(tree0, TESTDIR);
1438
1439 /* Create TESTDIR */
1440 status = torture_smb2_testdir(tree0, TESTDIR, &dh);
1441 torture_assert_ntstatus_ok_goto(
1442 tctx, status, ok, out, "smb2_create failed");
1443 status = smb2_util_close(tree0, dh);
1444 torture_assert_ntstatus_ok_goto(
1445 tctx, status, ok, out, "smb2_util_close failed");
1446
1447 ok = test_notify_max_async_credits(tctx,
1448 trees,
1449 ARRAY_SIZE(trees),
1450 max_credits);
1451 out:
1452 /* Cleanup TESTDIR */
1453 smb2_deltree(tree0, TESTDIR);
1454
1455 smbXcli_conn_disconnect(tree0->session->transport->conn,
1456 NT_STATUS_LOCAL_DISCONNECT);
1457 return ok;
1458 }
1459
1460 static bool test_2conn_notify_max_async_credits(struct torture_context *tctx,
1461 struct smb2_tree *tree1,
1462 struct smb2_tree *tree2)
1463 {
1464 struct smb2_tree *trees[2] = { tree1, tree2 };
1465 uint16_t max_async_credits = torture_setting_int(
1466 tctx,
1467 "maxasynccredits",
1468 512 /* lpcfg_smb2_max_async_credits(tctx->lp_ctx) */);
1469 struct smb2_handle dh = {{}};
1470 bool ok = false;
1471 NTSTATUS status;
1472 uint16_t max_credits = max_async_credits + 2;
1473
1474 smb2_transport_credits_ask_num(tree1->session->transport, max_credits);
1475 smb2_transport_credits_ask_num(tree2->session->transport, max_credits);
1476
1477 /* Cleanup TESTDIR */
1478 smb2_deltree(tree1, TESTDIR);
1479 smb2_deltree(tree2, TESTDIR);
1480
1481 /* Create TESTDIR */
1482 status = torture_smb2_testdir(tree1, TESTDIR, &dh);
1483 torture_assert_ntstatus_ok_goto(
1484 tctx, status, ok, out, "smb2_create failed");
1485 status = smb2_util_close(tree1, dh);
1486 torture_assert_ntstatus_ok_goto(
1487 tctx, status, ok, out, "smb2_util_close failed");
1488
1489 ok = test_notify_max_async_credits(tctx,
1490 trees,
1491 ARRAY_SIZE(trees),
1492 max_credits);
1493 out:
1494 /* Cleanup TESTDIR */
1495 smb2_deltree(tree1, TESTDIR);
1496
1497 smbXcli_conn_disconnect(tree1->session->transport->conn,
1498 NT_STATUS_LOCAL_DISCONNECT);
1499 smbXcli_conn_disconnect(tree2->session->transport->conn,
1500 NT_STATUS_LOCAL_DISCONNECT);
1501 return ok;
1502 }
1503
1504 static bool test_multichannel_notify_max_async_credits(
1505 struct torture_context *tctx,
1506 struct smb2_tree *tree1a)
1507 {
1508 uint16_t max_async_credits = torture_setting_int(
1509 tctx,
1510 "maxasynccredits",
1511 512 /* lpcfg_smb2_max_async_credits(tctx->lp_ctx) */);
1512 const char *host = torture_setting_string(tctx, "host", NULL);
1513 const char *share = torture_setting_string(tctx, "share", NULL);
1514 struct smb2_transport *transport1a = tree1a->session->transport;
1515 struct smbcli_options options = transport1a->options;
1516 struct smb2_session *session1a = tree1a->session;
1517 struct smb2_session *session1b = NULL;
1518 struct smb2_tree *tree1b_base = NULL;
1519 struct smb2_transport *transport1b = NULL;
1520 struct smb2_tree *tree1b = NULL;
1521 struct smb2_tree *trees[2] = {};
1522 struct smb2_handle dh = {{}};
1523 bool ok = false;
1524 NTSTATUS status;
1525 uint16_t max_credits = max_async_credits + 2;
1526
1527 smb2_transport_credits_ask_num(transport1a, max_credits);
1528
1529 /* Cleanup TESTDIR */
1530 smb2_deltree(tree1a, TESTDIR);
1531
1532 /* Create TESTDIR */
1533 status = torture_smb2_testdir(tree1a, TESTDIR, &dh);
1534 torture_assert_ntstatus_ok_goto(
1535 tctx, status, ok, out, "smb2_create failed");
1536 status = smb2_util_close(tree1a, dh);
1537 torture_assert_ntstatus_ok_goto(
1538 tctx, status, ok, out, "smb2_util_close failed");
1539
1540 /* Create a new connection with bigger max_credits value */
1541 options.max_credits = max_credits;
1542 /* Connect multichannel */
1543 options.only_negprot = true;
1544
1545 status = smb2_connect(tree1a,
1546 host,
1547 share,
1548 tctx->lp_ctx,
1549 lpcfg_resolve_context(tctx->lp_ctx),
1550 samba_cmdline_get_creds(),
1551 &tree1b_base,
1552 tctx->ev,
1553 &options,
1554 lpcfg_socket_options(tctx->lp_ctx),
1555 lpcfg_gensec_settings(tctx, tctx->lp_ctx));
1556 torture_assert_ntstatus_ok_goto(
1557 tctx, status, ok, out, "smb2_connect failed");
1558 transport1b = tree1b_base->session->transport;
1559
1560 session1b = smb2_session_channel(transport1b,
1561 lpcfg_gensec_settings(tctx,
1562 tctx->lp_ctx),
1563 tree1b_base,
1564 session1a);
1565 torture_assert_not_null_goto(
1566 tctx, session1b, ok, out, "smb2_session_channel failed");
1567
1568 status = smb2_session_setup_spnego(session1b,
1569 samba_cmdline_get_creds(),
1570 0);
1571 torture_assert_ntstatus_ok_goto(
1572 tctx, status, ok, out, "smb2_session_setup_spnego failed");
1573
1574 tree1b = smb2_tree_channel(tree1a, tree1b_base, false, session1b);
1575 torture_assert_not_null_goto(
1576 tctx, tree1b, ok, out, "smb2_tree_channel failed");
1577
1578 trees[0] = tree1a;
1579 trees[1] = tree1b;
1580
1581 ok = test_notify_max_async_credits(tctx,
1582 trees,
1583 ARRAY_SIZE(trees),
1584 max_credits);
1585 out:
1586 /* Cleanup TESTDIR */
1587 smb2_deltree(tree1a, TESTDIR);
1588
1589 smbXcli_conn_disconnect(transport1a->conn,
1590 NT_STATUS_LOCAL_DISCONNECT);
1591 if (transport1b != NULL) {
1592 smbXcli_conn_disconnect(transport1b->conn,
1593 NT_STATUS_LOCAL_DISCONNECT);
1594 }
1595
1596 return ok;
1597 }
1598
1599 struct torture_suite *torture_smb2_crediting_init(TALLOC_CTX *ctx)
1600 {
1601 struct torture_suite *suite = torture_suite_create(ctx, "credits");
1602
1603 torture_suite_add_1smb2_test(suite, "session_setup_credits_granted", test_session_setup_credits_granted);
1604 torture_suite_add_1smb2_test(suite, "single_req_credits_granted", test_single_req_credits_granted);
1605 torture_suite_add_1smb2_test(suite, "skipped_mid", test_crediting_skipped_mid);
1606
1607 torture_suite_add_1smb2_test(suite,
1608 "ipc_max_data_zero",
1609 test_ipc_max_data_zero);
1610 torture_suite_add_1smb2_test(suite,
1611 "1conn_ipc_max_async_credits",
1612 test_1conn_ipc_max_async_credits);
1613 torture_suite_add_1smb2_test(suite,
1614 "2conn_ipc_max_async_credits",
1615 test_2conn_ipc_max_async_credits);
1616 torture_suite_add_1smb2_test(suite,
1617 "multichannel_ipc_max_async_credits",
1618 test_multichannel_ipc_max_async_credits);
1619
1620 torture_suite_add_1smb2_test(suite,
1621 "1conn_notify_max_async_credits",
1622 test_1conn_notify_max_async_credits);
1623 torture_suite_add_2smb2_test(suite,
1624 "2conn_notify_max_async_credits",
1625 test_2conn_notify_max_async_credits);
1626 torture_suite_add_1smb2_test(suite,
1627 "multichannel_max_async_credits",
1628 test_multichannel_notify_max_async_credits);
1629
1630 suite->description = talloc_strdup(suite, "SMB2-CREDITS tests");
1631
1632 return suite;
1633 }