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
;
52 transport
= talloc_zero(parent_ctx
, struct smb2_transport
);
53 if (!transport
) return NULL
;
55 transport
->ev
= sock
->event
.ctx
;
56 transport
->options
= *options
;
58 if (transport
->options
.max_protocol
== PROTOCOL_DEFAULT
) {
59 transport
->options
.max_protocol
= PROTOCOL_LATEST
;
62 if (transport
->options
.max_protocol
< PROTOCOL_SMB2_02
) {
63 transport
->options
.max_protocol
= PROTOCOL_LATEST
;
66 TALLOC_FREE(sock
->event
.fde
);
67 TALLOC_FREE(sock
->event
.te
);
69 transport
->conn
= smbXcli_conn_create(transport
,
73 0, /* smb1_capabilities */
74 &options
->client_guid
,
75 options
->smb2_capabilities
);
76 if (transport
->conn
== NULL
) {
77 talloc_free(transport
);
83 talloc_set_destructor(transport
, transport_destructor
);
89 mark the transport as dead
91 void smb2_transport_dead(struct smb2_transport
*transport
, NTSTATUS status
)
93 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL
, status
)) {
94 status
= NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
96 if (NT_STATUS_IS_OK(status
)) {
97 status
= NT_STATUS_LOCAL_DISCONNECT
;
100 smbXcli_conn_disconnect(transport
->conn
, status
);
103 static void smb2_request_done(struct tevent_req
*subreq
);
104 static void smb2_transport_break_handler(struct tevent_req
*subreq
);
107 put a request into the send queue
109 void smb2_transport_send(struct smb2_request
*req
)
112 struct smb2_transport
*transport
= req
->transport
;
113 struct tevent_req
**reqs
= transport
->compound
.reqs
;
114 size_t num_reqs
= talloc_array_length(reqs
);
116 uint16_t cmd
= SVAL(req
->out
.hdr
, SMB2_HDR_OPCODE
);
117 uint32_t additional_flags
= IVAL(req
->out
.hdr
, SMB2_HDR_FLAGS
);
118 uint32_t clear_flags
= 0;
119 struct smbXcli_tcon
*tcon
= NULL
;
120 struct smbXcli_session
*session
= NULL
;
121 bool need_pending_break
= false;
124 DATA_BLOB body
= data_blob_null
;
125 DATA_BLOB dyn
= data_blob_null
;
126 uint32_t timeout_msec
= transport
->options
.request_timeout
* 1000;
128 if (transport
->oplock
.handler
) {
129 need_pending_break
= true;
132 if (transport
->lease
.handler
) {
133 need_pending_break
= true;
136 if (transport
->break_subreq
) {
137 need_pending_break
= false;
140 if (need_pending_break
) {
141 struct tevent_req
*subreq
;
143 subreq
= smb2cli_req_create(transport
,
147 0, /* additional_flags */
149 0, /* timeout_msec */
156 0); /* max_dyn_len */
157 if (subreq
!= NULL
) {
158 smbXcli_req_set_pending(subreq
);
159 tevent_req_set_callback(subreq
,
160 smb2_transport_break_handler
,
162 transport
->break_subreq
= subreq
;
167 session
= req
->session
->smbXcli
;
171 tcon
= req
->tree
->smbXcli
;
174 if (transport
->compound
.related
) {
175 additional_flags
|= SMB2_HDR_FLAG_CHAINED
;
178 hdr_ofs
= PTR_DIFF(req
->out
.hdr
, req
->out
.buffer
);
179 pdu_len
= req
->out
.size
- hdr_ofs
;
180 body
.data
= req
->out
.body
;
181 body
.length
= req
->out
.body_fixed
;
182 dyn
.data
= req
->out
.body
+ req
->out
.body_fixed
;
183 dyn
.length
= pdu_len
- (SMB2_HDR_BODY
+ req
->out
.body_fixed
);
185 req
->subreq
= smb2cli_req_create(req
,
194 body
.data
, body
.length
,
195 dyn
.data
, dyn
.length
,
196 0); /* max_dyn_len */
197 if (req
->subreq
== NULL
) {
198 req
->state
= SMB2_REQUEST_ERROR
;
199 req
->status
= NT_STATUS_NO_MEMORY
;
203 if (!tevent_req_is_in_progress(req
->subreq
)) {
204 req
->state
= SMB2_REQUEST_ERROR
;
205 req
->status
= NT_STATUS_INTERNAL_ERROR
;/* TODO */
209 tevent_req_set_callback(req
->subreq
, smb2_request_done
, req
);
211 smb2cli_req_set_notify_async(req
->subreq
);
212 if (req
->credit_charge
) {
213 smb2cli_req_set_credit_charge(req
->subreq
, req
->credit_charge
);
216 ZERO_STRUCT(req
->out
);
217 req
->state
= SMB2_REQUEST_RECV
;
220 for (i
=0; i
< num_reqs
; i
++) {
221 if (reqs
[i
] != NULL
) {
225 reqs
[i
] = req
->subreq
;
237 status
= smb2cli_req_compound_submit(reqs
, num_reqs
);
239 TALLOC_FREE(transport
->compound
.reqs
);
240 transport
->compound
.related
= false;
242 if (!NT_STATUS_IS_OK(status
)) {
243 req
->status
= status
;
244 req
->state
= SMB2_REQUEST_ERROR
;
245 smbXcli_conn_disconnect(transport
->conn
, status
);
249 static void smb2_request_done(struct tevent_req
*subreq
)
251 struct smb2_request
*req
=
252 tevent_req_callback_data(subreq
,
253 struct smb2_request
);
257 req
->recv_iov
= NULL
;
259 req
->status
= smb2cli_req_recv(req
->subreq
, req
, &req
->recv_iov
, NULL
, 0);
260 if (NT_STATUS_EQUAL(req
->status
, STATUS_PENDING
)) {
261 req
->cancel
.can_cancel
= true;
264 TALLOC_FREE(req
->subreq
);
265 if (!NT_STATUS_IS_OK(req
->status
)) {
266 if (req
->recv_iov
== NULL
) {
267 req
->state
= SMB2_REQUEST_ERROR
;
275 len
= req
->recv_iov
[0].iov_len
;
276 for (i
=1; i
< 3; i
++) {
277 uint8_t *p
= req
->recv_iov
[i
-1].iov_base
;
278 uint8_t *c1
= req
->recv_iov
[i
].iov_base
;
279 uint8_t *c2
= p
+ req
->recv_iov
[i
-1].iov_len
;
281 len
+= req
->recv_iov
[i
].iov_len
;
283 if (req
->recv_iov
[i
].iov_len
== 0) {
288 req
->status
= NT_STATUS_INTERNAL_ERROR
;
289 req
->state
= SMB2_REQUEST_ERROR
;
297 req
->in
.buffer
= req
->recv_iov
[0].iov_base
;
299 req
->in
.allocated
= req
->in
.size
;
301 req
->in
.hdr
= req
->recv_iov
[0].iov_base
;
302 req
->in
.body
= req
->recv_iov
[1].iov_base
;
303 req
->in
.dynamic
= req
->recv_iov
[2].iov_base
;
304 req
->in
.body_fixed
= req
->recv_iov
[1].iov_len
;
305 req
->in
.body_size
= req
->in
.body_fixed
;
306 req
->in
.body_size
+= req
->recv_iov
[2].iov_len
;
308 smb2_setup_bufinfo(req
);
310 req
->state
= SMB2_REQUEST_DONE
;
316 static void smb2_transport_break_handler(struct tevent_req
*subreq
)
318 struct smb2_transport
*transport
=
319 tevent_req_callback_data(subreq
,
320 struct smb2_transport
);
325 struct iovec
*recv_iov
= NULL
;
327 transport
->break_subreq
= NULL
;
329 status
= smb2cli_req_recv(subreq
, transport
, &recv_iov
, NULL
, 0);
331 if (!NT_STATUS_IS_OK(status
)) {
332 TALLOC_FREE(recv_iov
);
333 smb2_transport_dead(transport
, status
);
338 * Setup the subreq to handle the
339 * next incoming SMB2 Break.
341 subreq
= smb2cli_req_create(transport
,
345 0, /* additional_flags */
347 0, /* timeout_msec */
354 0); /* max_dyn_len */
355 if (subreq
!= NULL
) {
356 smbXcli_req_set_pending(subreq
);
357 tevent_req_set_callback(subreq
,
358 smb2_transport_break_handler
,
360 transport
->break_subreq
= subreq
;
363 body
= recv_iov
[1].iov_base
;
365 len
= recv_iov
[1].iov_len
;
366 if (recv_iov
[1].iov_len
>= 2) {
367 len
= CVAL(body
, 0x00);
368 if (len
!= recv_iov
[1].iov_len
) {
369 len
= recv_iov
[1].iov_len
;
375 } else if (len
== 44) {
378 DEBUG(1,("Discarding smb2 oplock reply of invalid size %u\n",
380 TALLOC_FREE(recv_iov
);
381 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
382 smb2_transport_dead(transport
, status
);
386 if (!lease
&& transport
->oplock
.handler
) {
387 struct smb2_handle h
;
390 level
= CVAL(body
, 0x02);
391 smb2_pull_handle(body
+0x08, &h
);
393 TALLOC_FREE(recv_iov
);
395 transport
->oplock
.handler(transport
, &h
, level
,
396 transport
->oplock
.private_data
);
397 } else if (lease
&& transport
->lease
.handler
) {
398 struct smb2_lease_break lb
;
401 lb
.new_epoch
= SVAL(body
, 0x2);
402 lb
.break_flags
= SVAL(body
, 0x4);
403 memcpy(&lb
.current_lease
.lease_key
, body
+0x8,
404 sizeof(struct smb2_lease_key
));
405 lb
.current_lease
.lease_state
= SVAL(body
, 0x18);
406 lb
.new_lease_state
= SVAL(body
, 0x1C);
407 lb
.break_reason
= SVAL(body
, 0x20);
408 lb
.access_mask_hint
= SVAL(body
, 0x24);
409 lb
.share_mask_hint
= SVAL(body
, 0x28);
411 TALLOC_FREE(recv_iov
);
413 transport
->lease
.handler(transport
, &lb
,
414 transport
->lease
.private_data
);
416 DEBUG(5,("Got SMB2 %s break with no handler\n",
417 lease
? "lease" : "oplock"));
419 TALLOC_FREE(recv_iov
);
422 NTSTATUS
smb2_transport_compound_start(struct smb2_transport
*transport
,
425 TALLOC_FREE(transport
->compound
.reqs
);
426 ZERO_STRUCT(transport
->compound
);
428 transport
->compound
.reqs
= talloc_zero_array(transport
,
431 if (transport
->compound
.reqs
== NULL
) {
432 return NT_STATUS_NO_MEMORY
;
438 void smb2_transport_compound_set_related(struct smb2_transport
*transport
,
441 transport
->compound
.related
= related
;
444 void smb2_transport_credits_ask_num(struct smb2_transport
*transport
,
447 smb2cli_conn_set_max_credits(transport
->conn
, ask_num
);
450 static void idle_handler(struct tevent_context
*ev
,
451 struct tevent_timer
*te
, struct timeval t
, void *private_data
)
453 struct smb2_transport
*transport
= talloc_get_type(private_data
,
454 struct smb2_transport
);
457 transport
->idle
.func(transport
, transport
->idle
.private_data
);
459 next
= timeval_current_ofs_usec(transport
->idle
.period
);
460 transport
->idle
.te
= tevent_add_timer(transport
->ev
,
468 setup the idle handler for a transport
469 the period is in microseconds
471 void smb2_transport_idle_handler(struct smb2_transport
*transport
,
472 void (*idle_func
)(struct smb2_transport
*, void *),
476 TALLOC_FREE(transport
->idle
.te
);
478 transport
->idle
.func
= idle_func
;
479 transport
->idle
.private_data
= private_data
;
480 transport
->idle
.period
= period
;
482 transport
->idle
.te
= tevent_add_timer(transport
->ev
,
484 timeval_current_ofs_usec(period
),