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/stream/packet.h"
31 #include "../lib/util/dlinklist.h"
32 #include "../libcli/smb/smbXcli_base.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 TALLOC_FREE(sock
->event
.fde
);
59 TALLOC_FREE(sock
->event
.te
);
61 transport
->conn
= smbXcli_conn_create(transport
,
65 0, /* smb1_capabilities */
66 NULL
); /* client_guid */
67 if (transport
->conn
== NULL
) {
68 talloc_free(transport
);
74 talloc_set_destructor(transport
, transport_destructor
);
80 mark the transport as dead
82 void smb2_transport_dead(struct smb2_transport
*transport
, NTSTATUS status
)
84 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL
, status
)) {
85 status
= NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
87 if (NT_STATUS_IS_OK(status
)) {
88 status
= NT_STATUS_LOCAL_DISCONNECT
;
91 smbXcli_conn_disconnect(transport
->conn
, status
);
94 static void smb2_request_done(struct tevent_req
*subreq
);
95 static void smb2_transport_break_handler(struct tevent_req
*subreq
);
98 put a request into the send queue
100 void smb2_transport_send(struct smb2_request
*req
)
103 struct smb2_transport
*transport
= req
->transport
;
104 struct tevent_req
**reqs
= transport
->compound
.reqs
;
105 size_t num_reqs
= talloc_array_length(reqs
);
107 uint16_t cmd
= SVAL(req
->out
.hdr
, SMB2_HDR_OPCODE
);
108 uint32_t additional_flags
= IVAL(req
->out
.hdr
, SMB2_HDR_FLAGS
);
109 uint32_t clear_flags
= 0;
110 uint32_t pid
= IVAL(req
->out
.hdr
, SMB2_HDR_PID
);
111 uint32_t tid
= IVAL(req
->out
.hdr
, SMB2_HDR_TID
);
112 struct smbXcli_session
*session
= NULL
;
113 bool need_pending_break
= false;
116 DATA_BLOB body
= data_blob_null
;
117 DATA_BLOB dyn
= data_blob_null
;
118 uint32_t timeout_msec
= transport
->options
.request_timeout
* 1000;
120 if (transport
->oplock
.handler
) {
121 need_pending_break
= true;
124 if (transport
->lease
.handler
) {
125 need_pending_break
= true;
128 if (transport
->break_subreq
) {
129 need_pending_break
= false;
132 if (need_pending_break
) {
133 struct tevent_req
*subreq
;
135 subreq
= smb2cli_req_create(transport
,
139 0, /* additional_flags */
141 0, /* timeout_msec */
149 if (subreq
!= NULL
) {
150 smbXcli_req_set_pending(subreq
);
151 tevent_req_set_callback(subreq
,
152 smb2_transport_break_handler
,
154 transport
->break_subreq
= subreq
;
159 session
= req
->session
->smbXcli
;
162 if (transport
->compound
.related
) {
163 additional_flags
|= SMB2_HDR_FLAG_CHAINED
;
166 hdr_ofs
= PTR_DIFF(req
->out
.hdr
, req
->out
.buffer
);
167 pdu_len
= req
->out
.size
- hdr_ofs
;
168 body
.data
= req
->out
.body
;
169 body
.length
= req
->out
.body_fixed
;
170 dyn
.data
= req
->out
.body
+ req
->out
.body_fixed
;
171 dyn
.length
= pdu_len
- (SMB2_HDR_BODY
+ req
->out
.body_fixed
);
173 req
->subreq
= smb2cli_req_create(req
,
183 body
.data
, body
.length
,
184 dyn
.data
, dyn
.length
);
185 if (req
->subreq
== NULL
) {
186 req
->state
= SMB2_REQUEST_ERROR
;
187 req
->status
= NT_STATUS_NO_MEMORY
;
191 if (!tevent_req_is_in_progress(req
->subreq
)) {
192 req
->state
= SMB2_REQUEST_ERROR
;
193 req
->status
= NT_STATUS_INTERNAL_ERROR
;/* TODO */
197 tevent_req_set_callback(req
->subreq
, smb2_request_done
, req
);
199 smb2cli_req_set_notify_async(req
->subreq
);
200 if (req
->credit_charge
) {
201 smb2cli_req_set_credit_charge(req
->subreq
, req
->credit_charge
);
204 ZERO_STRUCT(req
->out
);
205 req
->state
= SMB2_REQUEST_RECV
;
208 for (i
=0; i
< num_reqs
; i
++) {
209 if (reqs
[i
] != NULL
) {
213 reqs
[i
] = req
->subreq
;
225 status
= smb2cli_req_compound_submit(reqs
, num_reqs
);
227 TALLOC_FREE(transport
->compound
.reqs
);
229 if (!NT_STATUS_IS_OK(status
)) {
230 smbXcli_conn_disconnect(transport
->conn
, status
);
234 static void smb2_request_done(struct tevent_req
*subreq
)
236 struct smb2_request
*req
=
237 tevent_req_callback_data(subreq
,
238 struct smb2_request
);
242 req
->recv_iov
= NULL
;
244 req
->status
= smb2cli_req_recv(req
->subreq
, req
, &req
->recv_iov
, NULL
, 0);
245 if (NT_STATUS_EQUAL(req
->status
, STATUS_PENDING
)) {
246 req
->cancel
.can_cancel
= true;
249 TALLOC_FREE(req
->subreq
);
250 if (!NT_STATUS_IS_OK(req
->status
)) {
251 if (req
->recv_iov
== NULL
) {
252 req
->state
= SMB2_REQUEST_ERROR
;
260 len
= req
->recv_iov
[0].iov_len
;
261 for (i
=1; i
< 3; i
++) {
262 uint8_t *p
= req
->recv_iov
[i
-1].iov_base
;
263 uint8_t *c1
= req
->recv_iov
[i
].iov_base
;
264 uint8_t *c2
= p
+ req
->recv_iov
[i
-1].iov_len
;
266 len
+= req
->recv_iov
[i
].iov_len
;
268 if (req
->recv_iov
[i
].iov_len
== 0) {
273 req
->status
= NT_STATUS_INTERNAL_ERROR
;
274 req
->state
= SMB2_REQUEST_ERROR
;
282 req
->in
.buffer
= req
->recv_iov
[0].iov_base
;
284 req
->in
.allocated
= req
->in
.size
;
286 req
->in
.hdr
= req
->recv_iov
[0].iov_base
;
287 req
->in
.body
= req
->recv_iov
[1].iov_base
;
288 req
->in
.dynamic
= req
->recv_iov
[2].iov_base
;
289 req
->in
.body_fixed
= req
->recv_iov
[1].iov_len
;
290 req
->in
.body_size
= req
->in
.body_fixed
;
291 req
->in
.body_size
+= req
->recv_iov
[2].iov_len
;
293 smb2_setup_bufinfo(req
);
295 req
->state
= SMB2_REQUEST_DONE
;
301 static void smb2_transport_break_handler(struct tevent_req
*subreq
)
303 struct smb2_transport
*transport
=
304 tevent_req_callback_data(subreq
,
305 struct smb2_transport
);
311 struct iovec
*recv_iov
= NULL
;
313 transport
->break_subreq
= NULL
;
315 status
= smb2cli_req_recv(subreq
, transport
, &recv_iov
, NULL
, 0);
317 if (!NT_STATUS_IS_OK(status
)) {
318 TALLOC_FREE(recv_iov
);
319 smb2_transport_dead(transport
, status
);
324 * Setup the subreq to handle the
325 * next incoming SMB2 Break.
327 subreq
= smb2cli_req_create(transport
,
331 0, /* additional_flags */
333 0, /* timeout_msec */
341 if (subreq
!= NULL
) {
342 smbXcli_req_set_pending(subreq
);
343 tevent_req_set_callback(subreq
,
344 smb2_transport_break_handler
,
346 transport
->break_subreq
= subreq
;
349 hdr
= recv_iov
[0].iov_base
;
350 body
= recv_iov
[1].iov_base
;
352 len
= recv_iov
[1].iov_len
;
353 if (recv_iov
[1].iov_len
>= 2) {
354 len
= CVAL(body
, 0x00);
355 if (len
!= recv_iov
[1].iov_len
) {
356 len
= recv_iov
[1].iov_len
;
362 } else if (len
== 44) {
365 DEBUG(1,("Discarding smb2 oplock reply of invalid size %u\n",
367 TALLOC_FREE(recv_iov
);
368 status
= NT_STATUS_INVALID_NETWORK_RESPONSE
;
369 smb2_transport_dead(transport
, status
);
373 if (!lease
&& transport
->oplock
.handler
) {
374 struct smb2_handle h
;
377 level
= CVAL(body
, 0x02);
378 smb2_pull_handle(body
+0x08, &h
);
380 TALLOC_FREE(recv_iov
);
382 transport
->oplock
.handler(transport
, &h
, level
,
383 transport
->oplock
.private_data
);
384 } else if (lease
&& transport
->lease
.handler
) {
385 struct smb2_lease_break lb
;
388 lb
.break_flags
= SVAL(body
, 0x4);
389 memcpy(&lb
.current_lease
.lease_key
, body
+0x8,
390 sizeof(struct smb2_lease_key
));
391 lb
.current_lease
.lease_state
= SVAL(body
, 0x18);
392 lb
.new_lease_state
= SVAL(body
, 0x1C);
393 lb
.break_reason
= SVAL(body
, 0x20);
394 lb
.access_mask_hint
= SVAL(body
, 0x24);
395 lb
.share_mask_hint
= SVAL(body
, 0x28);
397 TALLOC_FREE(recv_iov
);
399 transport
->lease
.handler(transport
, &lb
,
400 transport
->lease
.private_data
);
402 DEBUG(5,("Got SMB2 %s break with no handler\n",
403 lease
? "lease" : "oplock"));
405 TALLOC_FREE(recv_iov
);
408 NTSTATUS
smb2_transport_compound_start(struct smb2_transport
*transport
,
411 TALLOC_FREE(transport
->compound
.reqs
);
412 ZERO_STRUCT(transport
->compound
);
414 transport
->compound
.reqs
= talloc_zero_array(transport
,
417 if (transport
->compound
.reqs
== NULL
) {
418 return NT_STATUS_NO_MEMORY
;
424 void smb2_transport_compound_set_related(struct smb2_transport
*transport
,
427 transport
->compound
.related
= related
;
430 void smb2_transport_credits_ask_num(struct smb2_transport
*transport
,
433 smb2cli_conn_set_max_credits(transport
->conn
, ask_num
);
436 static void idle_handler(struct tevent_context
*ev
,
437 struct tevent_timer
*te
, struct timeval t
, void *private_data
)
439 struct smb2_transport
*transport
= talloc_get_type(private_data
,
440 struct smb2_transport
);
441 struct timeval next
= timeval_add(&t
, 0, transport
->idle
.period
);
442 tevent_add_timer(transport
->ev
,
445 idle_handler
, transport
);
446 transport
->idle
.func(transport
, transport
->idle
.private_data
);
450 setup the idle handler for a transport
451 the period is in microseconds
453 void smb2_transport_idle_handler(struct smb2_transport
*transport
,
454 void (*idle_func
)(struct smb2_transport
*, void *),
458 transport
->idle
.func
= idle_func
;
459 transport
->idle
.private_data
= private_data
;
460 transport
->idle
.period
= period
;
462 tevent_add_timer(transport
->ev
,
464 timeval_current_ofs(0, period
),
465 idle_handler
, transport
);