2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * various SMB requests. Most of the routines merely packs data into mbufs.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
41 #include <sys/socket.h>
44 #include <sys/iconv.h>
46 #include <netsmb/smb.h>
47 #include <netsmb/smb_subr.h>
48 #include <netsmb/smb_rq.h>
49 #include <netsmb/smb_conn.h>
50 #include <netsmb/smb_tran.h>
52 #include "opt_netsmb.h"
59 static struct smb_dialect smb_dialects
[] = {
60 {SMB_DIALECT_CORE
, "PC NETWORK PROGRAM 1.0"},
61 {SMB_DIALECT_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
62 {SMB_DIALECT_LANMAN1_0
, "MICROSOFT NETWORKS 3.0"},
63 {SMB_DIALECT_LANMAN1_0
, "LANMAN1.0"},
64 {SMB_DIALECT_LANMAN2_0
, "LM1.2X002"},
65 {SMB_DIALECT_LANMAN2_0
, "Samba"},
66 {SMB_DIALECT_NTLM0_12
, "NT LANMAN 1.0"},
67 {SMB_DIALECT_NTLM0_12
, "NT LM 0.12"},
72 smb_vc_maxread(struct smb_vc
*vcp
)
75 * Specs say up to 64k data bytes, but Windows traffic
76 * uses 60k... no doubt for some good reason.
78 * Don't exceed the server's buffer size if signatures
79 * are enabled otherwise Windows 2003 chokes. Allow space
80 * for the SMB header & a little bit extra.
82 if ((vcp
->vc_sopt
.sv_caps
& SMB_CAP_LARGE_READX
) &&
83 (vcp
->vc_hflags2
& SMB_FLAGS2_SECURITY_SIGNATURE
) == 0)
86 return (vcp
->vc_sopt
.sv_maxtx
- SMB_HDRLEN
- 64);
90 smb_vc_maxwrite(struct smb_vc
*vcp
)
95 if ((vcp
->vc_sopt
.sv_caps
& SMB_CAP_LARGE_WRITEX
) &&
96 (vcp
->vc_hflags2
& SMB_FLAGS2_SECURITY_SIGNATURE
) == 0)
99 return (vcp
->vc_sopt
.sv_maxtx
- SMB_HDRLEN
- 64);
103 smb_smb_nomux(struct smb_vc
*vcp
, struct smb_cred
*scred
, const char *name
)
105 if (scred
->scr_td
->td_proc
== vcp
->vc_iod
->iod_p
)
107 SMBERROR("wrong function called(%s)\n", name
);
112 smb_smb_negotiate(struct smb_vc
*vcp
, struct smb_cred
*scred
)
114 struct smb_dialect
*dp
;
115 struct smb_sopt
*sp
= NULL
;
119 u_int8_t wc
, stime
[8], sblen
;
120 u_int16_t dindex
, tw
, tw1
, swlen
, bc
;
122 int unicode
= SMB_UNICODE_STRINGS(vcp
);
123 void * servercharset
= vcp
->vc_toserver
;
124 void * localcharset
= vcp
->vc_tolocal
;
126 if (smb_smb_nomux(vcp
, scred
, __func__
) != 0)
128 /* Disable Unicode for SMB_COM_NEGOTIATE requests */
130 vcp
->vc_toserver
= vcp
->vc_cp_toserver
;
131 vcp
->vc_tolocal
= vcp
->vc_cp_tolocal
;
135 vcp
->obj
.co_flags
&= ~(SMBV_ENCRYPT
);
137 bzero(sp
, sizeof(struct smb_sopt
));
138 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_NEGOTIATE
, scred
, &rqp
);
141 smb_rq_getrequest(rqp
, &mbp
);
145 for(dp
= smb_dialects
; dp
->d_id
!= -1; dp
++) {
146 mb_put_uint8(mbp
, SMB_DT_DIALECT
);
147 smb_put_dstring(mbp
, vcp
, dp
->d_name
, SMB_CS_NONE
);
150 error
= smb_rq_simple(rqp
);
151 SMBSDEBUG("%d\n", error
);
154 smb_rq_getreply(rqp
, &mdp
);
156 error
= md_get_uint8(mdp
, &wc
);
159 error
= md_get_uint16le(mdp
, &dindex
);
163 SMBERROR("Don't know how to talk with server %s (%d)\n", "xxx", dindex
);
167 dp
= smb_dialects
+ dindex
;
168 sp
->sv_proto
= dp
->d_id
;
169 SMBSDEBUG("Dialect %s (%d, %d)\n", dp
->d_name
, dindex
, wc
);
171 if (dp
->d_id
>= SMB_DIALECT_NTLM0_12
) {
174 md_get_uint8(mdp
, &sp
->sv_sm
);
175 md_get_uint16le(mdp
, &sp
->sv_maxmux
);
176 md_get_uint16le(mdp
, &sp
->sv_maxvcs
);
177 md_get_uint32le(mdp
, &sp
->sv_maxtx
);
178 md_get_uint32le(mdp
, &sp
->sv_maxraw
);
179 md_get_uint32le(mdp
, &sp
->sv_skey
);
180 md_get_uint32le(mdp
, &sp
->sv_caps
);
181 md_get_mem(mdp
, stime
, 8, MB_MSYSTEM
);
182 md_get_uint16le(mdp
, (u_int16_t
*)&sp
->sv_tz
);
183 md_get_uint8(mdp
, &sblen
);
184 if (sblen
&& (sp
->sv_sm
& SMB_SM_ENCRYPT
)) {
185 if (sblen
!= SMB_MAXCHALLENGELEN
) {
186 SMBERROR("Unexpected length of security blob (%d)\n", sblen
);
189 error
= md_get_uint16le(mdp
, &bc
);
192 if (sp
->sv_caps
& SMB_CAP_EXT_SECURITY
)
193 md_get_mem(mdp
, NULL
, 16, MB_MSYSTEM
);
194 error
= md_get_mem(mdp
, vcp
->vc_ch
, sblen
, MB_MSYSTEM
);
197 vcp
->vc_chlen
= sblen
;
198 vcp
->obj
.co_flags
|= SMBV_ENCRYPT
;
200 if (sp
->sv_sm
& SMB_SM_SIGS_REQUIRE
)
201 vcp
->vc_hflags2
|= SMB_FLAGS2_SECURITY_SIGNATURE
;
202 if (vcp
->vc_ucs_toserver
&&
203 sp
->sv_caps
& SMB_CAP_UNICODE
) {
207 vcp
->obj
.co_flags
|= SMBV_UNICODE
;
209 vcp
->vc_hflags2
|= SMB_FLAGS2_KNOWS_LONG_NAMES
;
210 if (dp
->d_id
== SMB_DIALECT_NTLM0_12
&&
211 sp
->sv_maxtx
< 4096 &&
212 (sp
->sv_caps
& SMB_CAP_NT_SMBS
) == 0) {
213 vcp
->obj
.co_flags
|= SMBV_WIN95
;
214 SMBSDEBUG("Win95 detected\n");
219 vcp
->vc_hflags2
&= ~(SMB_FLAGS2_EXT_SEC
|SMB_FLAGS2_DFS
|
220 SMB_FLAGS2_ERR_STATUS
|SMB_FLAGS2_UNICODE
);
222 if (dp
->d_id
> SMB_DIALECT_CORE
) {
223 md_get_uint16le(mdp
, &tw
);
225 md_get_uint16le(mdp
, &tw
);
227 md_get_uint16le(mdp
, &sp
->sv_maxmux
);
228 md_get_uint16le(mdp
, &sp
->sv_maxvcs
);
229 md_get_uint16le(mdp
, &tw
); /* rawmode */
230 md_get_uint32le(mdp
, &sp
->sv_skey
);
231 if (wc
== 13) { /* >= LANMAN1 */
232 md_get_uint16(mdp
, &tw
); /* time */
233 md_get_uint16(mdp
, &tw1
); /* date */
234 md_get_uint16le(mdp
, (u_int16_t
*)&sp
->sv_tz
);
235 md_get_uint16le(mdp
, &swlen
);
236 if (swlen
> SMB_MAXCHALLENGELEN
)
238 md_get_uint16(mdp
, NULL
); /* mbz */
239 if (md_get_uint16le(mdp
, &bc
) != 0)
243 if (swlen
&& (sp
->sv_sm
& SMB_SM_ENCRYPT
)) {
244 error
= md_get_mem(mdp
, vcp
->vc_ch
, swlen
, MB_MSYSTEM
);
247 vcp
->vc_chlen
= swlen
;
248 vcp
->obj
.co_flags
|= SMBV_ENCRYPT
;
251 vcp
->vc_hflags2
|= SMB_FLAGS2_KNOWS_LONG_NAMES
;
252 } else { /* an old CORE protocol */
258 vcp
->vc_maxvcs
= sp
->sv_maxvcs
;
259 if (vcp
->vc_maxvcs
<= 1) {
260 if (vcp
->vc_maxvcs
== 0)
263 if (sp
->sv_maxtx
<= 0 || sp
->sv_maxtx
> 0xffff)
266 sp
->sv_maxtx
= min(sp
->sv_maxtx
,
267 63*1024 + SMB_HDRLEN
+ 16);
268 SMB_TRAN_GETPARAM(vcp
, SMBTP_RCVSZ
, &maxqsz
);
269 vcp
->vc_rxmax
= min(smb_vc_maxread(vcp
), maxqsz
- 1024);
270 SMB_TRAN_GETPARAM(vcp
, SMBTP_SNDSZ
, &maxqsz
);
271 vcp
->vc_wxmax
= min(smb_vc_maxwrite(vcp
), maxqsz
- 1024);
272 vcp
->vc_txmax
= min(sp
->sv_maxtx
, maxqsz
);
273 SMBSDEBUG("TZ = %d\n", sp
->sv_tz
);
274 SMBSDEBUG("CAPS = %x\n", sp
->sv_caps
);
275 SMBSDEBUG("MAXMUX = %d\n", sp
->sv_maxmux
);
276 SMBSDEBUG("MAXVCS = %d\n", sp
->sv_maxvcs
);
277 SMBSDEBUG("MAXRAW = %d\n", sp
->sv_maxraw
);
278 SMBSDEBUG("MAXTX = %d\n", sp
->sv_maxtx
);
281 /* Restore Unicode conversion state */
283 vcp
->vc_toserver
= servercharset
;
284 vcp
->vc_tolocal
= localcharset
;
285 vcp
->vc_hflags2
|= SMB_FLAGS2_UNICODE
;
292 smb_smb_ssnsetup(struct smb_vc
*vcp
, struct smb_cred
*scred
)
298 smb_uniptr unipp
, ntencpass
= NULL
;
299 char *pp
, *up
, *pbuf
, *encpass
;
300 int error
, plen
, uniplen
, ulen
, upper
;
305 if (vcp
->obj
.co_flags
& SMBV_UNICODE
)
306 caps
|= SMB_CAP_UNICODE
;
310 vcp
->vc_smbuid
= SMB_UID_UNKNOWN
;
312 if (smb_smb_nomux(vcp
, scred
, __func__
) != 0)
315 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_SESSION_SETUP_ANDX
, scred
, &rqp
);
318 pbuf
= malloc(SMB_MAXPASSWORDLEN
+ 1, M_SMBTEMP
, M_WAITOK
);
319 encpass
= malloc(24, M_SMBTEMP
, M_WAITOK
);
320 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_USER
) {
322 * We try w/o uppercasing first so Samba mixed case
323 * passwords work. If that fails we come back and try
324 * uppercasing to satisfy OS/2 and Windows for Workgroups.
327 iconv_convstr(vcp
->vc_toupper
, pbuf
,
328 smb_vc_getpass(vcp
)/*, SMB_MAXPASSWORDLEN*/);
330 strncpy(pbuf
, smb_vc_getpass(vcp
), SMB_MAXPASSWORDLEN
);
331 pbuf
[SMB_MAXPASSWORDLEN
] = '\0';
333 if (!SMB_UNICODE_STRINGS(vcp
))
334 iconv_convstr(vcp
->vc_toserver
, pbuf
, pbuf
/*,
335 SMB_MAXPASSWORDLEN*/);
337 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_ENCRYPT
) {
339 smb_encrypt(pbuf
, vcp
->vc_ch
, encpass
);
340 ntencpass
= malloc(uniplen
, M_SMBTEMP
, M_WAITOK
);
341 if (SMB_UNICODE_STRINGS(vcp
)) {
342 strncpy(pbuf
, smb_vc_getpass(vcp
),
344 pbuf
[SMB_MAXPASSWORDLEN
] = '\0';
346 iconv_convstr(vcp
->vc_toserver
, pbuf
,
347 smb_vc_getpass(vcp
)/*,
348 SMB_MAXPASSWORDLEN*/);
349 smb_ntencrypt(pbuf
, vcp
->vc_ch
, (u_char
*)ntencpass
);
353 plen
= strlen(pbuf
) + 1;
356 ntencpass
= malloc(uniplen
, M_SMBTEMP
, M_WAITOK
);
357 smb_strtouni(ntencpass
, smb_vc_getpass(vcp
));
361 * The uniplen is zeroed because Samba cannot deal
362 * with this 2nd cleartext password. This Samba
363 * "bug" is actually a workaround for problems in
371 * In the share security mode password will be used
372 * only in the tree authentication
377 uniplen
= 0 /* sizeof(smb_unieol) */;
381 up
= vcp
->vc_username
;
382 ulen
= strlen(up
) + 1;
384 * If userid is null we are attempting anonymous browse login
385 * so passwords must be zero length.
389 mb_put_uint8(mbp
, 0xff);
390 mb_put_uint8(mbp
, 0);
391 mb_put_uint16le(mbp
, 0);
392 mb_put_uint16le(mbp
, vcp
->vc_sopt
.sv_maxtx
);
393 mb_put_uint16le(mbp
, vcp
->vc_sopt
.sv_maxmux
);
394 mb_put_uint16le(mbp
, vcp
->vc_number
);
395 mb_put_uint32le(mbp
, vcp
->vc_sopt
.sv_skey
);
396 mb_put_uint16le(mbp
, plen
);
397 if (SMB_DIALECT(vcp
) < SMB_DIALECT_NTLM0_12
) {
398 mb_put_uint32le(mbp
, 0);
401 mb_put_mem(mbp
, pp
, plen
, MB_MSYSTEM
);
402 smb_put_dstring(mbp
, vcp
, up
, SMB_CS_NONE
);
404 mb_put_uint16le(mbp
, uniplen
);
405 mb_put_uint32le(mbp
, 0); /* reserved */
406 mb_put_uint32le(mbp
, caps
);
409 mb_put_mem(mbp
, pp
, plen
, MB_MSYSTEM
);
410 mb_put_mem(mbp
, (caddr_t
)unipp
, uniplen
, MB_MSYSTEM
);
411 smb_put_dstring(mbp
, vcp
, up
, SMB_CS_NONE
); /* AccountName */
412 smb_put_dstring(mbp
, vcp
, vcp
->vc_domain
, SMB_CS_NONE
); /* PrimaryDomain */
413 smb_put_dstring(mbp
, vcp
, "FreeBSD", SMB_CS_NONE
); /* Client's OS */
414 smb_put_dstring(mbp
, vcp
, "NETSMB", SMB_CS_NONE
); /* Client name */
418 free(ntencpass
, M_SMBTEMP
);
419 if (vcp
->vc_hflags2
& SMB_FLAGS2_SECURITY_SIGNATURE
)
421 error
= smb_rq_simple(rqp
);
422 SMBSDEBUG("%d\n", error
);
424 if (rqp
->sr_errclass
== ERRDOS
&& rqp
->sr_serror
== ERRnoaccess
)
428 vcp
->vc_smbuid
= rqp
->sr_rpuid
;
430 free(encpass
, M_SMBTEMP
);
431 free(pbuf
, M_SMBTEMP
);
433 if (error
&& upper
== 1 && vcp
->vc_sopt
.sv_sm
& SMB_SM_USER
)
439 smb_smb_ssnclose(struct smb_vc
*vcp
, struct smb_cred
*scred
)
445 if (vcp
->vc_smbuid
== SMB_UID_UNKNOWN
)
448 if (smb_smb_nomux(vcp
, scred
, __func__
) != 0)
451 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_LOGOFF_ANDX
, scred
, &rqp
);
456 mb_put_uint8(mbp
, 0xff);
457 mb_put_uint8(mbp
, 0);
458 mb_put_uint16le(mbp
, 0);
462 error
= smb_rq_simple(rqp
);
463 SMBSDEBUG("%d\n", error
);
468 static char smb_any_share
[] = "?????";
471 smb_share_typename(int stype
)
480 pp
= smb_any_share
; /* can't use LPT: here... */
497 smb_smb_treeconnect(struct smb_share
*ssp
, struct smb_cred
*scred
)
500 struct smb_rq rq
, *rqp
= &rq
;
502 char *pp
, *pbuf
, *encpass
;
503 int error
, plen
, caseopt
, upper
;
508 /* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */
509 if (SSTOVC(ssp
)->vc_hflags2
& SMB_FLAGS2_UNICODE
) {
511 vcp
->vc_toserver
= vcp
->vc_cp_toserver
;
512 vcp
->vc_tolocal
= vcp
->vc_cp_tolocal
;
513 vcp
->vc_hflags2
&= ~SMB_FLAGS2_UNICODE
;
516 ssp
->ss_tid
= SMB_TID_UNKNOWN
;
517 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_TREE_CONNECT_ANDX
, scred
, &rqp
);
521 caseopt
= SMB_CS_NONE
;
522 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_USER
) {
528 pbuf
= malloc(SMB_MAXPASSWORDLEN
+ 1, M_SMBTEMP
, M_WAITOK
);
529 encpass
= malloc(24, M_SMBTEMP
, M_WAITOK
);
531 * We try w/o uppercasing first so Samba mixed case
532 * passwords work. If that fails we come back and try
533 * uppercasing to satisfy OS/2 and Windows for Workgroups.
536 iconv_convstr(vcp
->vc_toupper
, pbuf
,
537 smb_share_getpass(ssp
)/*,
538 SMB_MAXPASSWORDLEN*/);
540 strncpy(pbuf
, smb_share_getpass(ssp
),
542 pbuf
[SMB_MAXPASSWORDLEN
] = '\0';
544 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_ENCRYPT
) {
546 smb_encrypt(pbuf
, vcp
->vc_ch
, encpass
);
549 plen
= strlen(pbuf
) + 1;
555 mb_put_uint8(mbp
, 0xff);
556 mb_put_uint8(mbp
, 0);
557 mb_put_uint16le(mbp
, 0);
558 mb_put_uint16le(mbp
, 0); /* Flags */
559 mb_put_uint16le(mbp
, plen
);
562 mb_put_mem(mbp
, pp
, plen
, MB_MSYSTEM
);
563 smb_put_dmem(mbp
, vcp
, "\\\\", 2, caseopt
);
564 pp
= vcp
->vc_srvname
;
565 smb_put_dmem(mbp
, vcp
, pp
, strlen(pp
), caseopt
);
566 smb_put_dmem(mbp
, vcp
, "\\", 1, caseopt
);
568 smb_put_dstring(mbp
, vcp
, pp
, caseopt
);
569 pp
= smb_share_typename(ssp
->ss_type
);
570 smb_put_dstring(mbp
, vcp
, pp
, caseopt
);
572 error
= smb_rq_simple(rqp
);
573 SMBSDEBUG("%d\n", error
);
576 ssp
->ss_tid
= rqp
->sr_rptid
;
577 ssp
->ss_vcgenid
= vcp
->vc_genid
;
578 ssp
->ss_flags
|= SMBS_CONNECTED
;
580 * If the server can speak Unicode then switch
581 * our converters to do Unicode <--> Local
583 if (vcp
->obj
.co_flags
& SMBV_UNICODE
) {
584 vcp
->vc_toserver
= vcp
->vc_ucs_toserver
;
585 vcp
->vc_tolocal
= vcp
->vc_ucs_tolocal
;
586 vcp
->vc_hflags2
|= SMB_FLAGS2_UNICODE
;
590 free(encpass
, M_SMBTEMP
);
592 free(pbuf
, M_SMBTEMP
);
594 if (error
&& upper
== 1)
600 smb_smb_treedisconnect(struct smb_share
*ssp
, struct smb_cred
*scred
)
606 if (ssp
->ss_tid
== SMB_TID_UNKNOWN
)
608 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_TREE_DISCONNECT
, scred
, &rqp
);
616 error
= smb_rq_simple(rqp
);
617 SMBSDEBUG("%d\n", error
);
619 ssp
->ss_tid
= SMB_TID_UNKNOWN
;
624 smb_smb_readx(struct smb_share
*ssp
, u_int16_t fid
, int *len
, int *rresid
,
625 struct uio
*uio
, struct smb_cred
*scred
)
632 u_int16_t residhi
, residlo
, off
, doff
;
635 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_READ_ANDX
, scred
, &rqp
);
638 smb_rq_getrequest(rqp
, &mbp
);
640 mb_put_uint8(mbp
, 0xff); /* no secondary command */
641 mb_put_uint8(mbp
, 0); /* MBZ */
642 mb_put_uint16le(mbp
, 0); /* offset to secondary */
643 mb_put_mem(mbp
, (caddr_t
)&fid
, sizeof(fid
), MB_MSYSTEM
);
644 mb_put_uint32le(mbp
, uio
->uio_offset
);
645 *len
= min(SSTOVC(ssp
)->vc_rxmax
, *len
);
646 mb_put_uint16le(mbp
, *len
); /* MaxCount */
647 mb_put_uint16le(mbp
, *len
); /* MinCount (only indicates blocking) */
648 mb_put_uint32le(mbp
, (unsigned)*len
>> 16); /* MaxCountHigh */
649 mb_put_uint16le(mbp
, *len
); /* Remaining ("obsolete") */
650 mb_put_uint32le(mbp
, uio
->uio_offset
>> 32); /* OffsetHigh */
655 error
= smb_rq_simple(rqp
);
658 smb_rq_getreply(rqp
, &mdp
);
660 md_get_uint8(mdp
, &wc
);
666 md_get_uint8(mdp
, NULL
);
668 md_get_uint8(mdp
, NULL
);
670 md_get_uint16le(mdp
, NULL
);
672 md_get_uint16le(mdp
, NULL
);
674 md_get_uint16le(mdp
, NULL
); /* data compaction mode */
676 md_get_uint16le(mdp
, NULL
);
678 md_get_uint16le(mdp
, &residlo
);
680 md_get_uint16le(mdp
, &doff
); /* data offset */
682 md_get_uint16le(mdp
, &residhi
);
684 resid
= (residhi
<< 16) | residlo
;
685 md_get_mem(mdp
, NULL
, 4 * 2, MB_MSYSTEM
);
687 md_get_uint16le(mdp
, NULL
); /* ByteCount */
689 if (doff
> off
) /* pad byte(s)? */
690 md_get_mem(mdp
, NULL
, doff
- off
, MB_MSYSTEM
);
695 error
= md_get_uio(mdp
, uio
, resid
);
705 smb_smb_writex(struct smb_share
*ssp
, u_int16_t fid
, int *len
, int *rresid
,
706 struct uio
*uio
, struct smb_cred
*scred
)
715 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_WRITE_ANDX
, scred
, &rqp
);
718 smb_rq_getrequest(rqp
, &mbp
);
720 mb_put_uint8(mbp
, 0xff); /* no secondary command */
721 mb_put_uint8(mbp
, 0); /* MBZ */
722 mb_put_uint16le(mbp
, 0); /* offset to secondary */
723 mb_put_mem(mbp
, (caddr_t
)&fid
, sizeof(fid
), MB_MSYSTEM
);
724 mb_put_uint32le(mbp
, uio
->uio_offset
);
725 mb_put_uint32le(mbp
, 0); /* MBZ (timeout) */
726 mb_put_uint16le(mbp
, 0); /* !write-thru */
727 mb_put_uint16le(mbp
, 0);
728 *len
= min(SSTOVC(ssp
)->vc_wxmax
, *len
);
729 mb_put_uint16le(mbp
, (unsigned)*len
>> 16);
730 mb_put_uint16le(mbp
, *len
);
731 mb_put_uint16le(mbp
, 64); /* data offset from header start */
732 mb_put_uint32le(mbp
, uio
->uio_offset
>> 32); /* OffsetHigh */
736 mb_put_uint8(mbp
, 0xee); /* mimic xp pad byte! */
737 error
= mb_put_uio(mbp
, uio
, *len
);
741 error
= smb_rq_simple(rqp
);
744 smb_rq_getreply(rqp
, &mdp
);
745 md_get_uint8(mdp
, &wc
);
750 md_get_uint8(mdp
, NULL
);
751 md_get_uint8(mdp
, NULL
);
752 md_get_uint16le(mdp
, NULL
);
753 md_get_uint16le(mdp
, &resid
);
762 smb_smb_read(struct smb_share
*ssp
, u_int16_t fid
,
763 int *len
, int *rresid
, struct uio
*uio
, struct smb_cred
*scred
)
770 int error
, rlen
, blksz
;
772 if (SSTOVC(ssp
)->vc_sopt
.sv_caps
& SMB_CAP_LARGE_READX
)
773 return (smb_smb_readx(ssp
, fid
, len
, rresid
, uio
, scred
));
775 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_READ
, scred
, &rqp
);
779 blksz
= SSTOVC(ssp
)->vc_txmax
- SMB_HDRLEN
- 16;
780 rlen
= *len
= min(blksz
, *len
);
782 smb_rq_getrequest(rqp
, &mbp
);
784 mb_put_mem(mbp
, (caddr_t
)&fid
, sizeof(fid
), MB_MSYSTEM
);
785 mb_put_uint16le(mbp
, rlen
);
786 mb_put_uint32le(mbp
, uio
->uio_offset
);
787 mb_put_uint16le(mbp
, min(uio
->uio_resid
, 0xffff));
792 error
= smb_rq_simple(rqp
);
795 smb_rq_getreply(rqp
, &mdp
);
796 md_get_uint8(mdp
, &wc
);
801 md_get_uint16le(mdp
, &resid
);
802 md_get_mem(mdp
, NULL
, 4 * 2, MB_MSYSTEM
);
803 md_get_uint16le(mdp
, &bc
);
804 md_get_uint8(mdp
, NULL
); /* ignore buffer type */
805 md_get_uint16le(mdp
, &resid
);
810 error
= md_get_uio(mdp
, uio
, resid
);
820 smb_read(struct smb_share
*ssp
, u_int16_t fid
, struct uio
*uio
,
821 struct smb_cred
*scred
)
823 int tsize
, len
, resid
;
826 tsize
= uio
->uio_resid
;
830 error
= smb_smb_read(ssp
, fid
, &len
, &resid
, uio
, scred
);
841 smb_smb_write(struct smb_share
*ssp
, u_int16_t fid
, int *len
, int *rresid
,
842 struct uio
*uio
, struct smb_cred
*scred
)
851 if (*len
&& SSTOVC(ssp
)->vc_sopt
.sv_caps
& SMB_CAP_LARGE_WRITEX
)
852 return (smb_smb_writex(ssp
, fid
, len
, rresid
, uio
, scred
));
854 blksz
= SSTOVC(ssp
)->vc_txmax
- SMB_HDRLEN
- 16;
858 resid
= *len
= min(blksz
, *len
);
860 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_WRITE
, scred
, &rqp
);
863 smb_rq_getrequest(rqp
, &mbp
);
865 mb_put_mem(mbp
, (caddr_t
)&fid
, sizeof(fid
), MB_MSYSTEM
);
866 mb_put_uint16le(mbp
, resid
);
867 mb_put_uint32le(mbp
, uio
->uio_offset
);
868 mb_put_uint16le(mbp
, min(uio
->uio_resid
, 0xffff));
871 mb_put_uint8(mbp
, SMB_DT_DATA
);
872 mb_put_uint16le(mbp
, resid
);
874 error
= mb_put_uio(mbp
, uio
, resid
);
878 error
= smb_rq_simple(rqp
);
881 smb_rq_getreply(rqp
, &mdp
);
882 md_get_uint8(mdp
, &wc
);
887 md_get_uint16le(mdp
, &resid
);
895 smb_write(struct smb_share
*ssp
, u_int16_t fid
, struct uio
*uio
,
896 struct smb_cred
*scred
)
898 int error
= 0, len
, tsize
, resid
;
901 tsize
= uio
->uio_resid
;
906 error
= smb_smb_write(ssp
, fid
, &len
, &resid
, uio
, scred
);
917 * Errors can happen on the copyin, the rpc, etc. So they
918 * imply resid is unreliable. The only safe thing is
919 * to pretend zero bytes made it. We needn't restore the
920 * iovs because callers don't depend on them in error
921 * paths - uio_resid and uio_offset are what matter.
929 smb_smb_echo(struct smb_vc
*vcp
, struct smb_cred
*scred
)
935 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_ECHO
, scred
, &rqp
);
940 mb_put_uint16le(mbp
, 1);
943 mb_put_uint32le(mbp
, 0);
945 error
= smb_rq_simple(rqp
);
946 SMBSDEBUG("%d\n", error
);