2 Unix SMB/CIFS implementation.
4 SMB2 client transport context management functions
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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/>.
23 #include "system/network.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "libcli/smb2/smb2.h"
27 #include "libcli/smb2/smb2_calls.h"
28 #include "lib/socket/socket.h"
29 #include "lib/events/events.h"
30 #include "../lib/util/dlinklist.h"
31 #include "../libcli/smb/smbXcli_base.h"
32 #include "librpc/ndr/libndr.h"
37 static int transport_destructor(struct smb2_transport *transport)
39 smb2_transport_dead(transport, NT_STATUS_LOCAL_DISCONNECT);
44 create a transport structure based on an established socket
46 struct smb2_transport *smb2_transport_init(struct smbcli_socket *sock,
47 TALLOC_CTX *parent_ctx,
48 struct smbcli_options *options)
50 struct smb2_transport *transport;
51 uint32_t smb2_capabilities = 0;
53 transport = talloc_zero(parent_ctx, struct smb2_transport);
54 if (!transport) return NULL;
56 transport->ev = sock->event.ctx;
57 transport->options = *options;
59 TALLOC_FREE(sock->event.fde);
60 TALLOC_FREE(sock->event.te);
62 transport->conn = smbXcli_conn_create(transport,
66 0, /* smb1_capabilities */
67 &options->client_guid,
68 options->smb2_capabilities);
69 if (transport->conn == NULL) {
70 talloc_free(transport);
76 talloc_set_destructor(transport, transport_destructor);
82 mark the transport as dead
84 void smb2_transport_dead(struct smb2_transport *transport, NTSTATUS status)
86 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
87 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
89 if (NT_STATUS_IS_OK(status)) {
90 status = NT_STATUS_LOCAL_DISCONNECT;
93 smbXcli_conn_disconnect(transport->conn, status);
96 static void smb2_request_done(struct tevent_req *subreq);
97 static void smb2_transport_break_handler(struct tevent_req *subreq);
100 put a request into the send queue
102 void smb2_transport_send(struct smb2_request *req)
105 struct smb2_transport *transport = req->transport;
106 struct tevent_req **reqs = transport->compound.reqs;
107 size_t num_reqs = talloc_array_length(reqs);
109 uint16_t cmd = SVAL(req->out.hdr, SMB2_HDR_OPCODE);
110 uint32_t additional_flags = IVAL(req->out.hdr, SMB2_HDR_FLAGS);
111 uint32_t clear_flags = 0;
112 struct smbXcli_tcon *tcon = NULL;
113 struct smbXcli_session *session = NULL;
114 bool need_pending_break = false;
117 DATA_BLOB body = data_blob_null;
118 DATA_BLOB dyn = data_blob_null;
119 uint32_t timeout_msec = transport->options.request_timeout * 1000;
121 if (transport->oplock.handler) {
122 need_pending_break = true;
125 if (transport->lease.handler) {
126 need_pending_break = true;
129 if (transport->break_subreq) {
130 need_pending_break = false;
133 if (need_pending_break) {
134 struct tevent_req *subreq;
136 subreq = smb2cli_req_create(transport,
140 0, /* additional_flags */
142 0, /* timeout_msec */
149 0); /* max_dyn_len */
150 if (subreq != NULL) {
151 smbXcli_req_set_pending(subreq);
152 tevent_req_set_callback(subreq,
153 smb2_transport_break_handler,
155 transport->break_subreq = subreq;
160 session = req->session->smbXcli;
164 tcon = req->tree->smbXcli;
167 if (transport->compound.related) {
168 additional_flags |= SMB2_HDR_FLAG_CHAINED;
171 hdr_ofs = PTR_DIFF(req->out.hdr, req->out.buffer);
172 pdu_len = req->out.size - hdr_ofs;
173 body.data = req->out.body;
174 body.length = req->out.body_fixed;
175 dyn.data = req->out.body + req->out.body_fixed;
176 dyn.length = pdu_len - (SMB2_HDR_BODY + req->out.body_fixed);
178 req->subreq = smb2cli_req_create(req,
187 body.data, body.length,
188 dyn.data, dyn.length,
189 0); /* max_dyn_len */
190 if (req->subreq == NULL) {
191 req->state = SMB2_REQUEST_ERROR;
192 req->status = NT_STATUS_NO_MEMORY;
196 if (!tevent_req_is_in_progress(req->subreq)) {
197 req->state = SMB2_REQUEST_ERROR;
198 req->status = NT_STATUS_INTERNAL_ERROR;/* TODO */
202 tevent_req_set_callback(req->subreq, smb2_request_done, req);
204 smb2cli_req_set_notify_async(req->subreq);
205 if (req->credit_charge) {
206 smb2cli_req_set_credit_charge(req->subreq, req->credit_charge);
209 ZERO_STRUCT(req->out);
210 req->state = SMB2_REQUEST_RECV;
213 for (i=0; i < num_reqs; i++) {
214 if (reqs[i] != NULL) {
218 reqs[i] = req->subreq;
230 status = smb2cli_req_compound_submit(reqs, num_reqs);
232 TALLOC_FREE(transport->compound.reqs);
233 transport->compound.related = false;
235 if (!NT_STATUS_IS_OK(status)) {
236 req->status = status;
237 req->state = SMB2_REQUEST_ERROR;
238 smbXcli_conn_disconnect(transport->conn, status);
242 static void smb2_request_done(struct tevent_req *subreq)
244 struct smb2_request *req =
245 tevent_req_callback_data(subreq,
246 struct smb2_request);
250 req->recv_iov = NULL;
252 req->status = smb2cli_req_recv(req->subreq, req, &req->recv_iov, NULL, 0);
253 if (NT_STATUS_EQUAL(req->status, STATUS_PENDING)) {
254 req->cancel.can_cancel = true;
257 TALLOC_FREE(req->subreq);
258 if (!NT_STATUS_IS_OK(req->status)) {
259 if (req->recv_iov == NULL) {
260 req->state = SMB2_REQUEST_ERROR;
268 len = req->recv_iov[0].iov_len;
269 for (i=1; i < 3; i++) {
270 uint8_t *p = req->recv_iov[i-1].iov_base;
271 uint8_t *c1 = req->recv_iov[i].iov_base;
272 uint8_t *c2 = p + req->recv_iov[i-1].iov_len;
274 len += req->recv_iov[i].iov_len;
276 if (req->recv_iov[i].iov_len == 0) {
281 req->status = NT_STATUS_INTERNAL_ERROR;
282 req->state = SMB2_REQUEST_ERROR;
290 req->in.buffer = req->recv_iov[0].iov_base;
292 req->in.allocated = req->in.size;
294 req->in.hdr = req->recv_iov[0].iov_base;
295 req->in.body = req->recv_iov[1].iov_base;
296 req->in.dynamic = req->recv_iov[2].iov_base;
297 req->in.body_fixed = req->recv_iov[1].iov_len;
298 req->in.body_size = req->in.body_fixed;
299 req->in.body_size += req->recv_iov[2].iov_len;
301 smb2_setup_bufinfo(req);
303 req->state = SMB2_REQUEST_DONE;
309 static void smb2_transport_break_handler(struct tevent_req *subreq)
311 struct smb2_transport *transport =
312 tevent_req_callback_data(subreq,
313 struct smb2_transport);
318 struct iovec *recv_iov = NULL;
320 transport->break_subreq = NULL;
322 status = smb2cli_req_recv(subreq, transport, &recv_iov, NULL, 0);
324 if (!NT_STATUS_IS_OK(status)) {
325 TALLOC_FREE(recv_iov);
326 smb2_transport_dead(transport, status);
331 * Setup the subreq to handle the
332 * next incoming SMB2 Break.
334 subreq = smb2cli_req_create(transport,
338 0, /* additional_flags */
340 0, /* timeout_msec */
347 0); /* max_dyn_len */
348 if (subreq != NULL) {
349 smbXcli_req_set_pending(subreq);
350 tevent_req_set_callback(subreq,
351 smb2_transport_break_handler,
353 transport->break_subreq = subreq;
356 body = recv_iov[1].iov_base;
358 len = recv_iov[1].iov_len;
359 if (recv_iov[1].iov_len >= 2) {
360 len = CVAL(body, 0x00);
361 if (len != recv_iov[1].iov_len) {
362 len = recv_iov[1].iov_len;
368 } else if (len == 44) {
371 DEBUG(1,("Discarding smb2 oplock reply of invalid size %u\n",
373 TALLOC_FREE(recv_iov);
374 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
375 smb2_transport_dead(transport, status);
379 if (!lease && transport->oplock.handler) {
380 struct smb2_handle h;
383 level = CVAL(body, 0x02);
384 smb2_pull_handle(body+0x08, &h);
386 TALLOC_FREE(recv_iov);
388 transport->oplock.handler(transport, &h, level,
389 transport->oplock.private_data);
390 } else if (lease && transport->lease.handler) {
391 struct smb2_lease_break lb;
394 lb.break_flags = SVAL(body, 0x4);
395 memcpy(&lb.current_lease.lease_key, body+0x8,
396 sizeof(struct smb2_lease_key));
397 lb.current_lease.lease_state = SVAL(body, 0x18);
398 lb.new_lease_state = SVAL(body, 0x1C);
399 lb.break_reason = SVAL(body, 0x20);
400 lb.access_mask_hint = SVAL(body, 0x24);
401 lb.share_mask_hint = SVAL(body, 0x28);
403 TALLOC_FREE(recv_iov);
405 transport->lease.handler(transport, &lb,
406 transport->lease.private_data);
408 DEBUG(5,("Got SMB2 %s break with no handler\n",
409 lease ? "lease" : "oplock"));
411 TALLOC_FREE(recv_iov);
414 NTSTATUS smb2_transport_compound_start(struct smb2_transport *transport,
417 TALLOC_FREE(transport->compound.reqs);
418 ZERO_STRUCT(transport->compound);
420 transport->compound.reqs = talloc_zero_array(transport,
423 if (transport->compound.reqs == NULL) {
424 return NT_STATUS_NO_MEMORY;
430 void smb2_transport_compound_set_related(struct smb2_transport *transport,
433 transport->compound.related = related;
436 void smb2_transport_credits_ask_num(struct smb2_transport *transport,
439 smb2cli_conn_set_max_credits(transport->conn, ask_num);
442 static void idle_handler(struct tevent_context *ev,
443 struct tevent_timer *te, struct timeval t, void *private_data)
445 struct smb2_transport *transport = talloc_get_type(private_data,
446 struct smb2_transport);
449 transport->idle.func(transport, transport->idle.private_data);
451 next = timeval_current_ofs_usec(transport->idle.period);
452 transport->idle.te = tevent_add_timer(transport->ev,
460 setup the idle handler for a transport
461 the period is in microseconds
463 void smb2_transport_idle_handler(struct smb2_transport *transport,
464 void (*idle_func)(struct smb2_transport *, void *),
468 TALLOC_FREE(transport->idle.te);
470 transport->idle.func = idle_func;
471 transport->idle.private_data = private_data;
472 transport->idle.period = period;
474 transport->idle.te = tevent_add_timer(transport->ev,
476 timeval_current_ofs_usec(period),