2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000-2001 Boris Popov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * various SMB requests. Most of the routines merely packs data into mbufs.
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
39 #include <sys/sysctl.h>
40 #include <sys/socket.h>
43 #include <sys/iconv.h>
45 #include <netsmb/smb.h>
46 #include <netsmb/smb_subr.h>
47 #include <netsmb/smb_rq.h>
48 #include <netsmb/smb_conn.h>
49 #include <netsmb/smb_tran.h>
51 #include "opt_netsmb.h"
58 static struct smb_dialect smb_dialects
[] = {
59 {SMB_DIALECT_CORE
, "PC NETWORK PROGRAM 1.0"},
60 {SMB_DIALECT_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
61 {SMB_DIALECT_LANMAN1_0
, "MICROSOFT NETWORKS 3.0"},
62 {SMB_DIALECT_LANMAN1_0
, "LANMAN1.0"},
63 {SMB_DIALECT_LANMAN2_0
, "LM1.2X002"},
64 {SMB_DIALECT_LANMAN2_0
, "Samba"},
65 {SMB_DIALECT_NTLM0_12
, "NT LANMAN 1.0"},
66 {SMB_DIALECT_NTLM0_12
, "NT LM 0.12"},
71 smb_vc_maxread(struct smb_vc
*vcp
)
74 * Specs say up to 64k data bytes, but Windows traffic
75 * uses 60k... no doubt for some good reason.
77 * Don't exceed the server's buffer size if signatures
78 * are enabled otherwise Windows 2003 chokes. Allow space
79 * for the SMB header & a little bit extra.
81 if ((vcp
->vc_sopt
.sv_caps
& SMB_CAP_LARGE_READX
) &&
82 (vcp
->vc_hflags2
& SMB_FLAGS2_SECURITY_SIGNATURE
) == 0)
85 return (vcp
->vc_sopt
.sv_maxtx
- SMB_HDRLEN
- 64);
89 smb_vc_maxwrite(struct smb_vc
*vcp
)
94 if ((vcp
->vc_sopt
.sv_caps
& SMB_CAP_LARGE_WRITEX
) &&
95 (vcp
->vc_hflags2
& SMB_FLAGS2_SECURITY_SIGNATURE
) == 0)
98 return (vcp
->vc_sopt
.sv_maxtx
- SMB_HDRLEN
- 64);
102 smb_smb_nomux(struct smb_vc
*vcp
, struct smb_cred
*scred
, const char *name
)
104 if (scred
->scr_td
->td_proc
== vcp
->vc_iod
->iod_p
)
106 SMBERROR("wrong function called(%s)\n", name
);
111 smb_smb_negotiate(struct smb_vc
*vcp
, struct smb_cred
*scred
)
113 struct smb_dialect
*dp
;
114 struct smb_sopt
*sp
= NULL
;
118 u_int8_t wc
, stime
[8], sblen
;
119 u_int16_t dindex
, tw
, tw1
, swlen
, bc
;
121 int unicode
= SMB_UNICODE_STRINGS(vcp
);
122 void * servercharset
= vcp
->vc_toserver
;
123 void * localcharset
= vcp
->vc_tolocal
;
125 if (smb_smb_nomux(vcp
, scred
, __func__
) != 0)
127 /* Disable Unicode for SMB_COM_NEGOTIATE requests */
129 vcp
->vc_toserver
= vcp
->vc_cp_toserver
;
130 vcp
->vc_tolocal
= vcp
->vc_cp_tolocal
;
134 vcp
->obj
.co_flags
&= ~(SMBV_ENCRYPT
);
136 bzero(sp
, sizeof(struct smb_sopt
));
137 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_NEGOTIATE
, scred
, &rqp
);
140 smb_rq_getrequest(rqp
, &mbp
);
144 for(dp
= smb_dialects
; dp
->d_id
!= -1; dp
++) {
145 mb_put_uint8(mbp
, SMB_DT_DIALECT
);
146 smb_put_dstring(mbp
, vcp
, dp
->d_name
, SMB_CS_NONE
);
149 error
= smb_rq_simple(rqp
);
150 SMBSDEBUG("%d\n", error
);
153 smb_rq_getreply(rqp
, &mdp
);
155 error
= md_get_uint8(mdp
, &wc
);
158 error
= md_get_uint16le(mdp
, &dindex
);
162 SMBERROR("Don't know how to talk with server %s (%d)\n", "xxx", dindex
);
166 dp
= smb_dialects
+ dindex
;
167 sp
->sv_proto
= dp
->d_id
;
168 SMBSDEBUG("Dialect %s (%d, %d)\n", dp
->d_name
, dindex
, wc
);
170 if (dp
->d_id
>= SMB_DIALECT_NTLM0_12
) {
173 md_get_uint8(mdp
, &sp
->sv_sm
);
174 md_get_uint16le(mdp
, &sp
->sv_maxmux
);
175 md_get_uint16le(mdp
, &sp
->sv_maxvcs
);
176 md_get_uint32le(mdp
, &sp
->sv_maxtx
);
177 md_get_uint32le(mdp
, &sp
->sv_maxraw
);
178 md_get_uint32le(mdp
, &sp
->sv_skey
);
179 md_get_uint32le(mdp
, &sp
->sv_caps
);
180 md_get_mem(mdp
, stime
, 8, MB_MSYSTEM
);
181 md_get_uint16le(mdp
, (u_int16_t
*)&sp
->sv_tz
);
182 md_get_uint8(mdp
, &sblen
);
183 if (sblen
&& (sp
->sv_sm
& SMB_SM_ENCRYPT
)) {
184 if (sblen
!= SMB_MAXCHALLENGELEN
) {
185 SMBERROR("Unexpected length of security blob (%d)\n", sblen
);
188 error
= md_get_uint16le(mdp
, &bc
);
191 if (sp
->sv_caps
& SMB_CAP_EXT_SECURITY
)
192 md_get_mem(mdp
, NULL
, 16, MB_MSYSTEM
);
193 error
= md_get_mem(mdp
, vcp
->vc_ch
, sblen
, MB_MSYSTEM
);
196 vcp
->vc_chlen
= sblen
;
197 vcp
->obj
.co_flags
|= SMBV_ENCRYPT
;
199 if (sp
->sv_sm
& SMB_SM_SIGS_REQUIRE
)
200 vcp
->vc_hflags2
|= SMB_FLAGS2_SECURITY_SIGNATURE
;
201 if (vcp
->vc_ucs_toserver
&&
202 sp
->sv_caps
& SMB_CAP_UNICODE
) {
206 vcp
->obj
.co_flags
|= SMBV_UNICODE
;
208 vcp
->vc_hflags2
|= SMB_FLAGS2_KNOWS_LONG_NAMES
;
209 if (dp
->d_id
== SMB_DIALECT_NTLM0_12
&&
210 sp
->sv_maxtx
< 4096 &&
211 (sp
->sv_caps
& SMB_CAP_NT_SMBS
) == 0) {
212 vcp
->obj
.co_flags
|= SMBV_WIN95
;
213 SMBSDEBUG("Win95 detected\n");
218 vcp
->vc_hflags2
&= ~(SMB_FLAGS2_EXT_SEC
|SMB_FLAGS2_DFS
|
219 SMB_FLAGS2_ERR_STATUS
|SMB_FLAGS2_UNICODE
);
221 if (dp
->d_id
> SMB_DIALECT_CORE
) {
222 md_get_uint16le(mdp
, &tw
);
224 md_get_uint16le(mdp
, &tw
);
226 md_get_uint16le(mdp
, &sp
->sv_maxmux
);
227 md_get_uint16le(mdp
, &sp
->sv_maxvcs
);
228 md_get_uint16le(mdp
, &tw
); /* rawmode */
229 md_get_uint32le(mdp
, &sp
->sv_skey
);
230 if (wc
== 13) { /* >= LANMAN1 */
231 md_get_uint16(mdp
, &tw
); /* time */
232 md_get_uint16(mdp
, &tw1
); /* date */
233 md_get_uint16le(mdp
, (u_int16_t
*)&sp
->sv_tz
);
234 md_get_uint16le(mdp
, &swlen
);
235 if (swlen
> SMB_MAXCHALLENGELEN
)
237 md_get_uint16(mdp
, NULL
); /* mbz */
238 if (md_get_uint16le(mdp
, &bc
) != 0)
242 if (swlen
&& (sp
->sv_sm
& SMB_SM_ENCRYPT
)) {
243 error
= md_get_mem(mdp
, vcp
->vc_ch
, swlen
, MB_MSYSTEM
);
246 vcp
->vc_chlen
= swlen
;
247 vcp
->obj
.co_flags
|= SMBV_ENCRYPT
;
250 vcp
->vc_hflags2
|= SMB_FLAGS2_KNOWS_LONG_NAMES
;
251 } else { /* an old CORE protocol */
257 vcp
->vc_maxvcs
= sp
->sv_maxvcs
;
258 if (vcp
->vc_maxvcs
<= 1) {
259 if (vcp
->vc_maxvcs
== 0)
262 if (sp
->sv_maxtx
<= 0 || sp
->sv_maxtx
> 0xffff)
265 sp
->sv_maxtx
= min(sp
->sv_maxtx
,
266 63*1024 + SMB_HDRLEN
+ 16);
267 SMB_TRAN_GETPARAM(vcp
, SMBTP_RCVSZ
, &maxqsz
);
268 vcp
->vc_rxmax
= min(smb_vc_maxread(vcp
), maxqsz
- 1024);
269 SMB_TRAN_GETPARAM(vcp
, SMBTP_SNDSZ
, &maxqsz
);
270 vcp
->vc_wxmax
= min(smb_vc_maxwrite(vcp
), maxqsz
- 1024);
271 vcp
->vc_txmax
= min(sp
->sv_maxtx
, maxqsz
);
272 SMBSDEBUG("TZ = %d\n", sp
->sv_tz
);
273 SMBSDEBUG("CAPS = %x\n", sp
->sv_caps
);
274 SMBSDEBUG("MAXMUX = %d\n", sp
->sv_maxmux
);
275 SMBSDEBUG("MAXVCS = %d\n", sp
->sv_maxvcs
);
276 SMBSDEBUG("MAXRAW = %d\n", sp
->sv_maxraw
);
277 SMBSDEBUG("MAXTX = %d\n", sp
->sv_maxtx
);
280 /* Restore Unicode conversion state */
282 vcp
->vc_toserver
= servercharset
;
283 vcp
->vc_tolocal
= localcharset
;
284 vcp
->vc_hflags2
|= SMB_FLAGS2_UNICODE
;
291 smb_smb_ssnsetup(struct smb_vc
*vcp
, struct smb_cred
*scred
)
297 smb_uniptr unipp
, ntencpass
= NULL
;
298 char *pp
, *up
, *pbuf
, *encpass
;
299 int error
, plen
, uniplen
, ulen
, upper
;
304 if (vcp
->obj
.co_flags
& SMBV_UNICODE
)
305 caps
|= SMB_CAP_UNICODE
;
309 vcp
->vc_smbuid
= SMB_UID_UNKNOWN
;
311 if (smb_smb_nomux(vcp
, scred
, __func__
) != 0)
314 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_SESSION_SETUP_ANDX
, scred
, &rqp
);
317 pbuf
= malloc(SMB_MAXPASSWORDLEN
+ 1, M_SMBTEMP
, M_WAITOK
);
318 encpass
= malloc(24, M_SMBTEMP
, M_WAITOK
);
319 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_USER
) {
321 * We try w/o uppercasing first so Samba mixed case
322 * passwords work. If that fails we come back and try
323 * uppercasing to satisfy OS/2 and Windows for Workgroups.
326 iconv_convstr(vcp
->vc_toupper
, pbuf
,
327 smb_vc_getpass(vcp
)/*, SMB_MAXPASSWORDLEN*/);
329 strncpy(pbuf
, smb_vc_getpass(vcp
), SMB_MAXPASSWORDLEN
);
330 pbuf
[SMB_MAXPASSWORDLEN
] = '\0';
332 if (!SMB_UNICODE_STRINGS(vcp
))
333 iconv_convstr(vcp
->vc_toserver
, pbuf
, pbuf
/*,
334 SMB_MAXPASSWORDLEN*/);
336 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_ENCRYPT
) {
338 smb_encrypt(pbuf
, vcp
->vc_ch
, encpass
);
339 ntencpass
= malloc(uniplen
, M_SMBTEMP
, M_WAITOK
);
340 if (SMB_UNICODE_STRINGS(vcp
)) {
341 strncpy(pbuf
, smb_vc_getpass(vcp
),
343 pbuf
[SMB_MAXPASSWORDLEN
] = '\0';
345 iconv_convstr(vcp
->vc_toserver
, pbuf
,
346 smb_vc_getpass(vcp
)/*,
347 SMB_MAXPASSWORDLEN*/);
348 smb_ntencrypt(pbuf
, vcp
->vc_ch
, (u_char
*)ntencpass
);
352 plen
= strlen(pbuf
) + 1;
355 ntencpass
= malloc(uniplen
, M_SMBTEMP
, M_WAITOK
);
356 smb_strtouni(ntencpass
, smb_vc_getpass(vcp
));
360 * The uniplen is zeroed because Samba cannot deal
361 * with this 2nd cleartext password. This Samba
362 * "bug" is actually a workaround for problems in
370 * In the share security mode password will be used
371 * only in the tree authentication
376 uniplen
= 0 /* sizeof(smb_unieol) */;
380 up
= vcp
->vc_username
;
381 ulen
= strlen(up
) + 1;
383 * If userid is null we are attempting anonymous browse login
384 * so passwords must be zero length.
388 mb_put_uint8(mbp
, 0xff);
389 mb_put_uint8(mbp
, 0);
390 mb_put_uint16le(mbp
, 0);
391 mb_put_uint16le(mbp
, vcp
->vc_sopt
.sv_maxtx
);
392 mb_put_uint16le(mbp
, vcp
->vc_sopt
.sv_maxmux
);
393 mb_put_uint16le(mbp
, vcp
->vc_number
);
394 mb_put_uint32le(mbp
, vcp
->vc_sopt
.sv_skey
);
395 mb_put_uint16le(mbp
, plen
);
396 if (SMB_DIALECT(vcp
) < SMB_DIALECT_NTLM0_12
) {
397 mb_put_uint32le(mbp
, 0);
400 mb_put_mem(mbp
, pp
, plen
, MB_MSYSTEM
);
401 smb_put_dstring(mbp
, vcp
, up
, SMB_CS_NONE
);
403 mb_put_uint16le(mbp
, uniplen
);
404 mb_put_uint32le(mbp
, 0); /* reserved */
405 mb_put_uint32le(mbp
, caps
);
408 mb_put_mem(mbp
, pp
, plen
, MB_MSYSTEM
);
409 mb_put_mem(mbp
, (caddr_t
)unipp
, uniplen
, MB_MSYSTEM
);
410 smb_put_dstring(mbp
, vcp
, up
, SMB_CS_NONE
); /* AccountName */
411 smb_put_dstring(mbp
, vcp
, vcp
->vc_domain
, SMB_CS_NONE
); /* PrimaryDomain */
412 smb_put_dstring(mbp
, vcp
, "FreeBSD", SMB_CS_NONE
); /* Client's OS */
413 smb_put_dstring(mbp
, vcp
, "NETSMB", SMB_CS_NONE
); /* Client name */
417 free(ntencpass
, M_SMBTEMP
);
418 if (vcp
->vc_hflags2
& SMB_FLAGS2_SECURITY_SIGNATURE
)
420 error
= smb_rq_simple(rqp
);
421 SMBSDEBUG("%d\n", error
);
423 if (rqp
->sr_errclass
== ERRDOS
&& rqp
->sr_serror
== ERRnoaccess
)
427 vcp
->vc_smbuid
= rqp
->sr_rpuid
;
429 free(encpass
, M_SMBTEMP
);
430 free(pbuf
, M_SMBTEMP
);
432 if (error
&& upper
== 1 && vcp
->vc_sopt
.sv_sm
& SMB_SM_USER
)
438 smb_smb_ssnclose(struct smb_vc
*vcp
, struct smb_cred
*scred
)
444 if (vcp
->vc_smbuid
== SMB_UID_UNKNOWN
)
447 if (smb_smb_nomux(vcp
, scred
, __func__
) != 0)
450 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_LOGOFF_ANDX
, scred
, &rqp
);
455 mb_put_uint8(mbp
, 0xff);
456 mb_put_uint8(mbp
, 0);
457 mb_put_uint16le(mbp
, 0);
461 error
= smb_rq_simple(rqp
);
462 SMBSDEBUG("%d\n", error
);
467 static char smb_any_share
[] = "?????";
470 smb_share_typename(int stype
)
479 pp
= smb_any_share
; /* can't use LPT: here... */
496 smb_smb_treeconnect(struct smb_share
*ssp
, struct smb_cred
*scred
)
499 struct smb_rq rq
, *rqp
= &rq
;
501 char *pp
, *pbuf
, *encpass
;
502 int error
, plen
, caseopt
, upper
;
507 /* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */
508 if (SSTOVC(ssp
)->vc_hflags2
& SMB_FLAGS2_UNICODE
) {
510 vcp
->vc_toserver
= vcp
->vc_cp_toserver
;
511 vcp
->vc_tolocal
= vcp
->vc_cp_tolocal
;
512 vcp
->vc_hflags2
&= ~SMB_FLAGS2_UNICODE
;
515 ssp
->ss_tid
= SMB_TID_UNKNOWN
;
516 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_TREE_CONNECT_ANDX
, scred
, &rqp
);
520 caseopt
= SMB_CS_NONE
;
521 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_USER
) {
527 pbuf
= malloc(SMB_MAXPASSWORDLEN
+ 1, M_SMBTEMP
, M_WAITOK
);
528 encpass
= malloc(24, M_SMBTEMP
, M_WAITOK
);
530 * We try w/o uppercasing first so Samba mixed case
531 * passwords work. If that fails we come back and try
532 * uppercasing to satisfy OS/2 and Windows for Workgroups.
535 iconv_convstr(vcp
->vc_toupper
, pbuf
,
536 smb_share_getpass(ssp
)/*,
537 SMB_MAXPASSWORDLEN*/);
539 strncpy(pbuf
, smb_share_getpass(ssp
),
541 pbuf
[SMB_MAXPASSWORDLEN
] = '\0';
543 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_ENCRYPT
) {
545 smb_encrypt(pbuf
, vcp
->vc_ch
, encpass
);
548 plen
= strlen(pbuf
) + 1;
554 mb_put_uint8(mbp
, 0xff);
555 mb_put_uint8(mbp
, 0);
556 mb_put_uint16le(mbp
, 0);
557 mb_put_uint16le(mbp
, 0); /* Flags */
558 mb_put_uint16le(mbp
, plen
);
561 mb_put_mem(mbp
, pp
, plen
, MB_MSYSTEM
);
562 smb_put_dmem(mbp
, vcp
, "\\\\", 2, caseopt
);
563 pp
= vcp
->vc_srvname
;
564 smb_put_dmem(mbp
, vcp
, pp
, strlen(pp
), caseopt
);
565 smb_put_dmem(mbp
, vcp
, "\\", 1, caseopt
);
567 smb_put_dstring(mbp
, vcp
, pp
, caseopt
);
568 pp
= smb_share_typename(ssp
->ss_type
);
569 smb_put_dstring(mbp
, vcp
, pp
, caseopt
);
571 error
= smb_rq_simple(rqp
);
572 SMBSDEBUG("%d\n", error
);
575 ssp
->ss_tid
= rqp
->sr_rptid
;
576 ssp
->ss_vcgenid
= vcp
->vc_genid
;
577 ssp
->ss_flags
|= SMBS_CONNECTED
;
579 * If the server can speak Unicode then switch
580 * our converters to do Unicode <--> Local
582 if (vcp
->obj
.co_flags
& SMBV_UNICODE
) {
583 vcp
->vc_toserver
= vcp
->vc_ucs_toserver
;
584 vcp
->vc_tolocal
= vcp
->vc_ucs_tolocal
;
585 vcp
->vc_hflags2
|= SMB_FLAGS2_UNICODE
;
589 free(encpass
, M_SMBTEMP
);
591 free(pbuf
, M_SMBTEMP
);
593 if (error
&& upper
== 1)
599 smb_smb_treedisconnect(struct smb_share
*ssp
, struct smb_cred
*scred
)
604 if (ssp
->ss_tid
== SMB_TID_UNKNOWN
)
606 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_TREE_DISCONNECT
, scred
, &rqp
);
613 error
= smb_rq_simple(rqp
);
614 SMBSDEBUG("%d\n", error
);
616 ssp
->ss_tid
= SMB_TID_UNKNOWN
;
621 smb_smb_readx(struct smb_share
*ssp
, u_int16_t fid
, int *len
, int *rresid
,
622 struct uio
*uio
, struct smb_cred
*scred
)
629 u_int16_t residhi
, residlo
, off
, doff
;
632 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_READ_ANDX
, scred
, &rqp
);
635 smb_rq_getrequest(rqp
, &mbp
);
637 mb_put_uint8(mbp
, 0xff); /* no secondary command */
638 mb_put_uint8(mbp
, 0); /* MBZ */
639 mb_put_uint16le(mbp
, 0); /* offset to secondary */
640 mb_put_mem(mbp
, (caddr_t
)&fid
, sizeof(fid
), MB_MSYSTEM
);
641 mb_put_uint32le(mbp
, uio
->uio_offset
);
642 *len
= min(SSTOVC(ssp
)->vc_rxmax
, *len
);
643 mb_put_uint16le(mbp
, *len
); /* MaxCount */
644 mb_put_uint16le(mbp
, *len
); /* MinCount (only indicates blocking) */
645 mb_put_uint32le(mbp
, (unsigned)*len
>> 16); /* MaxCountHigh */
646 mb_put_uint16le(mbp
, *len
); /* Remaining ("obsolete") */
647 mb_put_uint32le(mbp
, uio
->uio_offset
>> 32); /* OffsetHigh */
652 error
= smb_rq_simple(rqp
);
655 smb_rq_getreply(rqp
, &mdp
);
657 md_get_uint8(mdp
, &wc
);
663 md_get_uint8(mdp
, NULL
);
665 md_get_uint8(mdp
, NULL
);
667 md_get_uint16le(mdp
, NULL
);
669 md_get_uint16le(mdp
, NULL
);
671 md_get_uint16le(mdp
, NULL
); /* data compaction mode */
673 md_get_uint16le(mdp
, NULL
);
675 md_get_uint16le(mdp
, &residlo
);
677 md_get_uint16le(mdp
, &doff
); /* data offset */
679 md_get_uint16le(mdp
, &residhi
);
681 resid
= (residhi
<< 16) | residlo
;
682 md_get_mem(mdp
, NULL
, 4 * 2, MB_MSYSTEM
);
684 md_get_uint16le(mdp
, NULL
); /* ByteCount */
686 if (doff
> off
) /* pad byte(s)? */
687 md_get_mem(mdp
, NULL
, doff
- off
, MB_MSYSTEM
);
692 error
= md_get_uio(mdp
, uio
, resid
);
702 smb_smb_writex(struct smb_share
*ssp
, u_int16_t fid
, int *len
, int *rresid
,
703 struct uio
*uio
, struct smb_cred
*scred
)
712 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_WRITE_ANDX
, scred
, &rqp
);
715 smb_rq_getrequest(rqp
, &mbp
);
717 mb_put_uint8(mbp
, 0xff); /* no secondary command */
718 mb_put_uint8(mbp
, 0); /* MBZ */
719 mb_put_uint16le(mbp
, 0); /* offset to secondary */
720 mb_put_mem(mbp
, (caddr_t
)&fid
, sizeof(fid
), MB_MSYSTEM
);
721 mb_put_uint32le(mbp
, uio
->uio_offset
);
722 mb_put_uint32le(mbp
, 0); /* MBZ (timeout) */
723 mb_put_uint16le(mbp
, 0); /* !write-thru */
724 mb_put_uint16le(mbp
, 0);
725 *len
= min(SSTOVC(ssp
)->vc_wxmax
, *len
);
726 mb_put_uint16le(mbp
, (unsigned)*len
>> 16);
727 mb_put_uint16le(mbp
, *len
);
728 mb_put_uint16le(mbp
, 64); /* data offset from header start */
729 mb_put_uint32le(mbp
, uio
->uio_offset
>> 32); /* OffsetHigh */
733 mb_put_uint8(mbp
, 0xee); /* mimic xp pad byte! */
734 error
= mb_put_uio(mbp
, uio
, *len
);
738 error
= smb_rq_simple(rqp
);
741 smb_rq_getreply(rqp
, &mdp
);
742 md_get_uint8(mdp
, &wc
);
747 md_get_uint8(mdp
, NULL
);
748 md_get_uint8(mdp
, NULL
);
749 md_get_uint16le(mdp
, NULL
);
750 md_get_uint16le(mdp
, &resid
);
759 smb_smb_read(struct smb_share
*ssp
, u_int16_t fid
,
760 int *len
, int *rresid
, struct uio
*uio
, struct smb_cred
*scred
)
767 int error
, rlen
, blksz
;
769 if (SSTOVC(ssp
)->vc_sopt
.sv_caps
& SMB_CAP_LARGE_READX
)
770 return (smb_smb_readx(ssp
, fid
, len
, rresid
, uio
, scred
));
772 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_READ
, scred
, &rqp
);
776 blksz
= SSTOVC(ssp
)->vc_txmax
- SMB_HDRLEN
- 16;
777 rlen
= *len
= min(blksz
, *len
);
779 smb_rq_getrequest(rqp
, &mbp
);
781 mb_put_mem(mbp
, (caddr_t
)&fid
, sizeof(fid
), MB_MSYSTEM
);
782 mb_put_uint16le(mbp
, rlen
);
783 mb_put_uint32le(mbp
, uio
->uio_offset
);
784 mb_put_uint16le(mbp
, min(uio
->uio_resid
, 0xffff));
789 error
= smb_rq_simple(rqp
);
792 smb_rq_getreply(rqp
, &mdp
);
793 md_get_uint8(mdp
, &wc
);
798 md_get_uint16le(mdp
, &resid
);
799 md_get_mem(mdp
, NULL
, 4 * 2, MB_MSYSTEM
);
800 md_get_uint16le(mdp
, &bc
);
801 md_get_uint8(mdp
, NULL
); /* ignore buffer type */
802 md_get_uint16le(mdp
, &resid
);
807 error
= md_get_uio(mdp
, uio
, resid
);
817 smb_read(struct smb_share
*ssp
, u_int16_t fid
, struct uio
*uio
,
818 struct smb_cred
*scred
)
820 int tsize
, len
, resid
;
823 tsize
= uio
->uio_resid
;
827 error
= smb_smb_read(ssp
, fid
, &len
, &resid
, uio
, scred
);
838 smb_smb_write(struct smb_share
*ssp
, u_int16_t fid
, int *len
, int *rresid
,
839 struct uio
*uio
, struct smb_cred
*scred
)
848 if (*len
&& SSTOVC(ssp
)->vc_sopt
.sv_caps
& SMB_CAP_LARGE_WRITEX
)
849 return (smb_smb_writex(ssp
, fid
, len
, rresid
, uio
, scred
));
851 blksz
= SSTOVC(ssp
)->vc_txmax
- SMB_HDRLEN
- 16;
855 resid
= *len
= min(blksz
, *len
);
857 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_WRITE
, scred
, &rqp
);
860 smb_rq_getrequest(rqp
, &mbp
);
862 mb_put_mem(mbp
, (caddr_t
)&fid
, sizeof(fid
), MB_MSYSTEM
);
863 mb_put_uint16le(mbp
, resid
);
864 mb_put_uint32le(mbp
, uio
->uio_offset
);
865 mb_put_uint16le(mbp
, min(uio
->uio_resid
, 0xffff));
868 mb_put_uint8(mbp
, SMB_DT_DATA
);
869 mb_put_uint16le(mbp
, resid
);
871 error
= mb_put_uio(mbp
, uio
, resid
);
875 error
= smb_rq_simple(rqp
);
878 smb_rq_getreply(rqp
, &mdp
);
879 md_get_uint8(mdp
, &wc
);
884 md_get_uint16le(mdp
, &resid
);
892 smb_write(struct smb_share
*ssp
, u_int16_t fid
, struct uio
*uio
,
893 struct smb_cred
*scred
)
895 int error
= 0, len
, tsize
, resid
;
898 tsize
= uio
->uio_resid
;
903 error
= smb_smb_write(ssp
, fid
, &len
, &resid
, uio
, scred
);
914 * Errors can happen on the copyin, the rpc, etc. So they
915 * imply resid is unreliable. The only safe thing is
916 * to pretend zero bytes made it. We needn't restore the
917 * iovs because callers don't depend on them in error
918 * paths - uio_resid and uio_offset are what matter.
926 smb_smb_echo(struct smb_vc
*vcp
, struct smb_cred
*scred
)
932 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_ECHO
, scred
, &rqp
);
937 mb_put_uint16le(mbp
, 1);
940 mb_put_uint32le(mbp
, 0);
942 error
= smb_rq_simple(rqp
);
943 SMBSDEBUG("%d\n", error
);