1 /* $NetBSD: smb_smb.c,v 1.31 2009/10/18 22:53:36 tron Exp $ */
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.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Boris Popov.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * FreeBSD: src/sys/netsmb/smb_smb.c,v 1.10 2003/02/19 05:47:38 imp Exp
37 * various SMB requests. Most of the routines merely packs data into mbufs.
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: smb_smb.c,v 1.31 2009/10/18 22:53:36 tron Exp $");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
50 #include <sys/socket.h>
53 #include <netsmb/iconv.h>
55 #include <netsmb/smb.h>
56 #include <netsmb/smb_subr.h>
57 #include <netsmb/smb_rq.h>
58 #include <netsmb/smb_conn.h>
59 #include <netsmb/smb_tran.h>
66 static const struct smb_dialect smb_dialects
[] = {
67 {SMB_DIALECT_CORE
, "PC NETWORK PROGRAM 1.0"},
68 {SMB_DIALECT_COREPLUS
, "MICROSOFT NETWORKS 1.03"},
69 {SMB_DIALECT_LANMAN1_0
, "MICROSOFT NETWORKS 3.0"},
70 {SMB_DIALECT_LANMAN1_0
, "LANMAN1.0"},
71 {SMB_DIALECT_LANMAN2_0
, "LM1.2X002"},
72 {SMB_DIALECT_LANMAN2_0
, "Samba"},
73 {SMB_DIALECT_NTLM0_12
, "NT LANMAN 1.0"},
74 {SMB_DIALECT_NTLM0_12
, "NT LM 0.12"},
79 smb_vc_maxread(struct smb_vc
*vcp
)
82 * Specs say up to 64k data bytes, but Windows traffic
83 * uses 60k... no doubt for some good reason.
85 if (SMB_CAPS(vcp
) & SMB_CAP_LARGE_READX
)
88 return (vcp
->vc_sopt
.sv_maxtx
);
92 smb_vc_maxwrite(struct smb_vc
*vcp
)
95 * Specs say up to 64k data bytes, but Windows traffic
96 * uses 60k... probably for some good reason.
98 if (SMB_CAPS(vcp
) & SMB_CAP_LARGE_WRITEX
)
101 return (vcp
->vc_sopt
.sv_maxtx
);
105 smb_smb_negotiate(struct smb_vc
*vcp
, struct smb_cred
*scred
)
107 const struct smb_dialect
*dp
;
108 struct smb_sopt
*sp
= NULL
;
112 u_int8_t wc
, stime
[8], sblen
;
113 u_int16_t dindex
, tw
, swlen
, bc
;
116 KASSERT(scred
->scr_l
== vcp
->vc_iod
->iod_l
);
120 vcp
->obj
.co_flags
&= ~(SMBV_ENCRYPT
);
122 memset(sp
, 0, sizeof(struct smb_sopt
));
123 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_NEGOTIATE
, scred
, &rqp
);
126 smb_rq_getrequest(rqp
, &mbp
);
130 for(dp
= smb_dialects
; dp
->d_id
!= -1; dp
++) {
131 mb_put_uint8(mbp
, SMB_DT_DIALECT
);
132 smb_put_dstring(mbp
, vcp
, dp
->d_name
, SMB_CS_NONE
);
135 error
= smb_rq_simple(rqp
);
136 SMBSDEBUG(("%d\n", error
));
139 smb_rq_getreply(rqp
, &mdp
);
141 error
= md_get_uint8(mdp
, &wc
);
144 error
= md_get_uint16le(mdp
, &dindex
);
148 SMBERROR(("Don't know how to talk with server %s (%d)\n", "xxx", dindex
));
152 dp
= smb_dialects
+ dindex
;
153 sp
->sv_proto
= dp
->d_id
;
154 SMBSDEBUG(("Dialect %s (%d, %d)\n", dp
->d_name
, dindex
, wc
));
156 if (dp
->d_id
>= SMB_DIALECT_NTLM0_12
) {
161 md_get_uint8(mdp
, &tb
);
163 md_get_uint16le(mdp
, &sp
->sv_maxmux
);
164 md_get_uint16le(mdp
, &sp
->sv_maxvcs
);
165 md_get_uint32le(mdp
, &sp
->sv_maxtx
);
166 md_get_uint32le(mdp
, &sp
->sv_maxraw
);
167 md_get_uint32le(mdp
, &sp
->sv_skey
);
168 md_get_uint32le(mdp
, &sp
->sv_caps
);
169 md_get_mem(mdp
, stime
, 8, MB_MSYSTEM
);
170 md_get_uint16le(mdp
, &tw
);
172 md_get_uint8(mdp
, &sblen
);
173 if (sblen
&& (sp
->sv_sm
& SMB_SM_ENCRYPT
)) {
174 if (sblen
!= SMB_MAXCHALLENGELEN
) {
175 SMBERROR(("Unexpected length of security blob (%d)\n", sblen
));
178 error
= md_get_uint16(mdp
, &bc
);
181 if (sp
->sv_caps
& SMB_CAP_EXT_SECURITY
)
182 md_get_mem(mdp
, NULL
, 16, MB_MSYSTEM
);
183 error
= md_get_mem(mdp
, vcp
->vc_ch
, sblen
, MB_MSYSTEM
);
186 vcp
->vc_chlen
= sblen
;
187 vcp
->obj
.co_flags
|= SMBV_ENCRYPT
;
189 vcp
->vc_hflags2
|= SMB_FLAGS2_KNOWS_LONG_NAMES
;
190 if (dp
->d_id
== SMB_DIALECT_NTLM0_12
&&
191 sp
->sv_maxtx
< 4096 &&
192 (sp
->sv_caps
& SMB_CAP_NT_SMBS
) == 0) {
193 vcp
->obj
.co_flags
|= SMBV_WIN95
;
194 SMBSDEBUG(("Win95 detected\n"));
196 } else if (dp
->d_id
> SMB_DIALECT_CORE
) {
197 md_get_uint16le(mdp
, &sp
->sv_sm
);
198 md_get_uint16le(mdp
, &tw
);
200 md_get_uint16le(mdp
, &sp
->sv_maxmux
);
201 md_get_uint16le(mdp
, &sp
->sv_maxvcs
);
202 md_get_uint16(mdp
, NULL
); /* rawmode */
203 md_get_uint32le(mdp
, &sp
->sv_skey
);
204 if (wc
== 13) { /* >= LANMAN1 */
205 md_get_uint16(mdp
, NULL
); /* time */
206 md_get_uint16(mdp
, NULL
); /* date */
207 md_get_uint16le(mdp
, &tw
);
209 md_get_uint16le(mdp
, &swlen
);
210 if (swlen
> SMB_MAXCHALLENGELEN
)
212 md_get_uint16(mdp
, NULL
); /* mbz */
213 if (md_get_uint16(mdp
, &bc
) != 0)
217 if (swlen
&& (sp
->sv_sm
& SMB_SM_ENCRYPT
)) {
218 error
= md_get_mem(mdp
, vcp
->vc_ch
, swlen
, MB_MSYSTEM
);
221 vcp
->vc_chlen
= swlen
;
222 vcp
->obj
.co_flags
|= SMBV_ENCRYPT
;
225 vcp
->vc_hflags2
|= SMB_FLAGS2_KNOWS_LONG_NAMES
;
226 } else { /* an old CORE protocol */
232 vcp
->vc_maxvcs
= sp
->sv_maxvcs
;
233 if (vcp
->vc_maxvcs
<= 1) {
234 if (vcp
->vc_maxvcs
== 0)
237 if (sp
->sv_maxtx
<= 0 || sp
->sv_maxtx
> 0xffff)
240 sp
->sv_maxtx
= min(sp
->sv_maxtx
,
241 63*1024 + SMB_HDRLEN
+ 16);
242 SMB_TRAN_GETPARAM(vcp
, SMBTP_RCVSZ
, &maxqsz
);
243 vcp
->vc_rxmax
= min(smb_vc_maxread(vcp
), maxqsz
- 1024);
244 SMB_TRAN_GETPARAM(vcp
, SMBTP_SNDSZ
, &maxqsz
);
245 vcp
->vc_wxmax
= min(smb_vc_maxwrite(vcp
), maxqsz
- 1024);
246 vcp
->vc_txmax
= min(sp
->sv_maxtx
, maxqsz
);
247 SMBSDEBUG(("TZ = %d\n", sp
->sv_tz
));
248 SMBSDEBUG(("CAPS = %x\n", sp
->sv_caps
));
249 SMBSDEBUG(("MAXMUX = %d\n", sp
->sv_maxmux
));
250 SMBSDEBUG(("MAXVCS = %d\n", sp
->sv_maxvcs
));
251 SMBSDEBUG(("MAXRAW = %d\n", sp
->sv_maxraw
));
252 SMBSDEBUG(("MAXTX = %d\n", sp
->sv_maxtx
));
260 smb_smb_ssnsetup(struct smb_vc
*vcp
, struct smb_cred
*scred
)
264 const smb_unichar
*unipp
;
265 smb_uniptr ntencpass
= NULL
;
266 char *up
, *pbuf
, *encpass
;
268 int error
, plen
, uniplen
, ulen
, upper
;
270 KASSERT(scred
->scr_l
== vcp
->vc_iod
->iod_l
);
276 vcp
->vc_smbuid
= SMB_UID_UNKNOWN
;
278 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_SESSION_SETUP_ANDX
, scred
, &rqp
);
281 pbuf
= malloc(SMB_MAXPASSWORDLEN
+ 1, M_SMBTEMP
, M_WAITOK
);
282 encpass
= malloc(24, M_SMBTEMP
, M_WAITOK
);
283 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_USER
) {
285 * We try w/o uppercasing first so Samba mixed case
286 * passwords work. If that fails we come back and try
287 * uppercasing to satisfy OS/2 and Windows for Workgroups.
290 iconv_convstr(vcp
->vc_toupper
, pbuf
,
291 smb_vc_getpass(vcp
), SMB_MAXPASSWORDLEN
+ 1);
293 strlcpy(pbuf
, smb_vc_getpass(vcp
),
294 SMB_MAXPASSWORDLEN
+ 1);
296 if (!SMB_UNICODE_STRINGS(vcp
))
297 iconv_convstr(vcp
->vc_toserver
, pbuf
, pbuf
,
298 SMB_MAXPASSWORDLEN
+ 1);
300 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_ENCRYPT
) {
302 smb_encrypt(pbuf
, vcp
->vc_ch
, encpass
);
303 ntencpass
= malloc(uniplen
, M_SMBTEMP
, M_WAITOK
);
304 if (SMB_UNICODE_STRINGS(vcp
)) {
305 strlcpy(pbuf
, smb_vc_getpass(vcp
),
306 SMB_MAXPASSWORDLEN
+ 1);
308 iconv_convstr(vcp
->vc_toserver
, pbuf
,
310 SMB_MAXPASSWORDLEN
+ 1);
311 smb_ntencrypt(pbuf
, vcp
->vc_ch
, (u_char
*)ntencpass
);
315 plen
= strlen(pbuf
) + 1;
318 ntencpass
= malloc(uniplen
, M_SMBTEMP
, M_WAITOK
);
319 smb_strtouni(ntencpass
, smb_vc_getpass(vcp
));
323 * The uniplen is zeroed because Samba cannot deal
324 * with this 2nd cleartext password. This Samba
325 * "bug" is actually a workaround for problems in
333 * In the share security mode password will be used
334 * only in the tree authentication
343 up
= vcp
->vc_username
;
344 ulen
= strlen(up
) + 1;
346 * If userid is null we are attempting anonymous browse login
347 * so passwords must be zero length.
351 mb_put_uint8(mbp
, 0xff);
352 mb_put_uint8(mbp
, 0);
353 mb_put_uint16le(mbp
, 0);
354 mb_put_uint16le(mbp
, vcp
->vc_sopt
.sv_maxtx
);
355 mb_put_uint16le(mbp
, vcp
->vc_sopt
.sv_maxmux
);
356 mb_put_uint16le(mbp
, vcp
->vc_number
);
357 mb_put_uint32le(mbp
, vcp
->vc_sopt
.sv_skey
);
358 mb_put_uint16le(mbp
, plen
);
359 if (SMB_DIALECT(vcp
) < SMB_DIALECT_NTLM0_12
) {
360 mb_put_uint32le(mbp
, 0);
363 mb_put_mem(mbp
, pp
, plen
, MB_MSYSTEM
);
364 smb_put_dstring(mbp
, vcp
, up
, SMB_CS_NONE
);
366 mb_put_uint16le(mbp
, uniplen
);
367 mb_put_uint32le(mbp
, 0); /* reserved */
368 mb_put_uint32le(mbp
, vcp
->obj
.co_flags
& SMBV_UNICODE
?
369 SMB_CAP_UNICODE
: 0);
372 mb_put_mem(mbp
, pp
, plen
, MB_MSYSTEM
);
373 mb_put_mem(mbp
, (const void *)unipp
, uniplen
, MB_MSYSTEM
);
374 smb_put_dstring(mbp
, vcp
, up
, SMB_CS_NONE
); /* AccountName */
375 smb_put_dstring(mbp
, vcp
, vcp
->vc_domain
, SMB_CS_NONE
); /* PrimaryDomain */
376 smb_put_dstring(mbp
, vcp
, "NetBSD", SMB_CS_NONE
); /* Client's OS */
377 smb_put_dstring(mbp
, vcp
, "NETSMB", SMB_CS_NONE
); /* Client name */
381 free(ntencpass
, M_SMBTEMP
);
382 error
= smb_rq_simple(rqp
);
383 SMBSDEBUG(("%d\n", error
));
389 vcp
->vc_smbuid
= rqp
->sr_rpuid
;
391 free(encpass
, M_SMBTEMP
);
392 free(pbuf
, M_SMBTEMP
);
394 if (error
&& !upper
&& vcp
->vc_sopt
.sv_sm
& SMB_SM_USER
) {
402 smb_smb_ssnclose(struct smb_vc
*vcp
, struct smb_cred
*scred
)
408 KASSERT(scred
->scr_l
== vcp
->vc_iod
->iod_l
);
410 if (vcp
->vc_smbuid
== SMB_UID_UNKNOWN
)
413 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_LOGOFF_ANDX
, scred
, &rqp
);
418 mb_put_uint8(mbp
, 0xff);
419 mb_put_uint8(mbp
, 0);
420 mb_put_uint16le(mbp
, 0);
424 error
= smb_rq_simple(rqp
);
425 SMBSDEBUG(("%d\n", error
));
431 smb_share_typename(int stype
)
433 static const char smb_any_share
[] = "?????";
441 pp
= smb_any_share
; /* can't use LPT: here... */
458 smb_smb_treeconnect(struct smb_share
*ssp
, struct smb_cred
*scred
)
461 struct smb_rq rq
, *rqp
= &rq
;
464 char *pbuf
, *encpass
;
465 int error
, plen
, caseopt
, upper
;
472 /* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */
473 if (SSTOVC(ssp
)->vc_hflags2
& SMB_FLAGS2_UNICODE
) {
475 if (vcp
->vc_toserver
) {
476 iconv_close(vcp
->vc_toserver
);
477 /* Use NULL until UTF-8 -> ASCII works */
478 vcp
->vc_toserver
= NULL
;
480 if (vcp
->vc_tolocal
) {
481 iconv_close(vcp
->vc_tolocal
);
482 /* Use NULL until ASCII -> UTF-8 works*/
483 vcp
->vc_tolocal
= NULL
;
485 vcp
->vc_hflags2
&= ~SMB_FLAGS2_UNICODE
;
489 ssp
->ss_tid
= SMB_TID_UNKNOWN
;
490 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_TREE_CONNECT_ANDX
, scred
, &rqp
);
494 caseopt
= SMB_CS_NONE
;
495 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_USER
) {
501 pbuf
= malloc(SMB_MAXPASSWORDLEN
+ 1, M_SMBTEMP
, M_WAITOK
);
502 encpass
= malloc(24, M_SMBTEMP
, M_WAITOK
);
504 * We try w/o uppercasing first so Samba mixed case
505 * passwords work. If that fails we come back and try
506 * uppercasing to satisfy OS/2 and Windows for Workgroups.
509 iconv_convstr(vcp
->vc_toupper
, pbuf
,
510 smb_share_getpass(ssp
), SMB_MAXPASSWORDLEN
+ 1);
512 strlcpy(pbuf
, smb_share_getpass(ssp
),
513 SMB_MAXPASSWORDLEN
+ 1);
515 if (vcp
->vc_sopt
.sv_sm
& SMB_SM_ENCRYPT
) {
517 smb_encrypt(pbuf
, vcp
->vc_ch
, encpass
);
520 plen
= strlen(pbuf
) + 1;
526 mb_put_uint8(mbp
, 0xff);
527 mb_put_uint8(mbp
, 0);
528 mb_put_uint16le(mbp
, 0);
529 mb_put_uint16le(mbp
, 0); /* Flags */
530 mb_put_uint16le(mbp
, plen
);
533 mb_put_mem(mbp
, pp
, plen
, MB_MSYSTEM
);
534 smb_put_dmem(mbp
, vcp
, "\\\\", 2, caseopt
);
535 pp
= vcp
->vc_srvname
;
536 smb_put_dmem(mbp
, vcp
, pp
, strlen(pp
), caseopt
);
537 smb_put_dmem(mbp
, vcp
, "\\", 1, caseopt
);
539 smb_put_dstring(mbp
, vcp
, pp
, caseopt
);
540 pp
= smb_share_typename(ssp
->ss_type
);
541 smb_put_dstring(mbp
, vcp
, pp
, caseopt
);
543 error
= smb_rq_simple(rqp
);
544 SMBSDEBUG(("%d\n", error
));
547 ssp
->ss_tid
= rqp
->sr_rptid
;
548 ssp
->ss_vcgenid
= vcp
->vc_genid
;
549 ssp
->ss_flags
|= SMBS_CONNECTED
;
552 free(encpass
, M_SMBTEMP
);
554 free(pbuf
, M_SMBTEMP
);
556 if (error
&& !upper
) {
564 smb_smb_treedisconnect(struct smb_share
*ssp
, struct smb_cred
*scred
)
569 if (ssp
->ss_tid
== SMB_TID_UNKNOWN
)
571 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_TREE_DISCONNECT
, scred
, &rqp
);
578 error
= smb_rq_simple(rqp
);
579 SMBSDEBUG(("%d\n", error
));
581 ssp
->ss_tid
= SMB_TID_UNKNOWN
;
586 smb_smb_readx(struct smb_share
*ssp
, u_int16_t fid
, size_t *len
, size_t *rresid
,
587 struct uio
*uio
, struct smb_cred
*scred
)
594 u_int16_t residhi
, residlo
, off
, doff
;
597 if (!(SMB_CAPS(SSTOVC(ssp
)) & SMB_CAP_LARGE_FILES
) &&
598 uio
->uio_offset
>= (1LL << 32)) {
599 /* Cannot read at/beyond 4G */
603 if (!(SMB_CAPS(SSTOVC(ssp
)) & SMB_CAP_LARGE_READX
)) {
606 blksz
= SSTOVC(ssp
)->vc_txmax
- SMB_HDRLEN
- 64;
610 *len
= min(blksz
, *len
);
613 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_READ_ANDX
, scred
, &rqp
);
616 smb_rq_getrequest(rqp
, &mbp
);
618 mb_put_uint8(mbp
, 0xff); /* no secondary command */
619 mb_put_uint8(mbp
, 0); /* MBZ */
620 mb_put_uint16le(mbp
, 0); /* offset to secondary */
621 mb_put_mem(mbp
, (void *)&fid
, sizeof(fid
), MB_MSYSTEM
);
622 mb_put_uint32le(mbp
, uio
->uio_offset
);
623 *len
= min(SSTOVC(ssp
)->vc_rxmax
, *len
);
624 mb_put_uint16le(mbp
, *len
); /* MaxCount */
625 mb_put_uint16le(mbp
, *len
); /* MinCount (only indicates blocking) */
626 mb_put_uint32le(mbp
, *len
>> 16); /* MaxCountHigh */
627 mb_put_uint16le(mbp
, *len
); /* Remaining ("obsolete") */
628 mb_put_uint32le(mbp
, uio
->uio_offset
>> 32); /* OffsetHigh */
633 error
= smb_rq_simple(rqp
);
636 smb_rq_getreply(rqp
, &mdp
);
638 md_get_uint8(mdp
, &wc
);
644 md_get_uint8(mdp
, NULL
);
646 md_get_uint8(mdp
, NULL
);
648 md_get_uint16(mdp
, NULL
);
650 md_get_uint16(mdp
, NULL
);
652 md_get_uint16(mdp
, NULL
); /* data compaction mode */
654 md_get_uint16(mdp
, NULL
);
656 md_get_uint16le(mdp
, &residlo
);
658 md_get_uint16le(mdp
, &doff
); /* data offset */
660 md_get_uint16le(mdp
, &residhi
);
662 resid
= (residhi
<< 16) | residlo
;
663 md_get_mem(mdp
, NULL
, 4 * 2, MB_MSYSTEM
);
665 md_get_uint16(mdp
, NULL
); /* ByteCount */
667 if (doff
> off
) /* pad byte(s)? */
668 md_get_mem(mdp
, NULL
, doff
- off
, MB_MSYSTEM
);
673 error
= md_get_uio(mdp
, uio
, resid
);
683 smb_smb_writex(struct smb_share
*ssp
, u_int16_t fid
, size_t *len
, size_t *rresid
,
684 struct uio
*uio
, struct smb_cred
*scred
)
693 if (!(SMB_CAPS(SSTOVC(ssp
)) & SMB_CAP_LARGE_FILES
) &&
694 uio
->uio_offset
>= (1LL << 32)) {
695 /* Cannot write at/beyond 4G */
699 if (SMB_CAPS(SSTOVC(ssp
)) & SMB_CAP_LARGE_WRITEX
) {
700 *len
= min(SSTOVC(ssp
)->vc_wxmax
, *len
);
704 blksz
= SSTOVC(ssp
)->vc_txmax
- SMB_HDRLEN
- 64;
708 *len
= min(blksz
, *len
);
711 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_WRITE_ANDX
, scred
, &rqp
);
714 smb_rq_getrequest(rqp
, &mbp
);
716 mb_put_uint8(mbp
, 0xff); /* no secondary command */
717 mb_put_uint8(mbp
, 0); /* MBZ */
718 mb_put_uint16le(mbp
, 0); /* offset to secondary */
719 mb_put_mem(mbp
, (void *)&fid
, sizeof(fid
), MB_MSYSTEM
);
720 mb_put_uint32le(mbp
, uio
->uio_offset
);
721 mb_put_uint32le(mbp
, 0); /* MBZ (timeout) */
722 mb_put_uint16le(mbp
, 0); /* !write-thru */
723 mb_put_uint16le(mbp
, 0);
724 mb_put_uint16le(mbp
, *len
>> 16);
725 mb_put_uint16le(mbp
, *len
);
726 mb_put_uint16le(mbp
, 64); /* data offset from header start */
727 mb_put_uint32le(mbp
, uio
->uio_offset
>> 32); /* OffsetHigh */
731 mb_put_uint8(mbp
, 0xee); /* mimic xp pad byte! */
732 error
= mb_put_uio(mbp
, uio
, *len
);
736 error
= smb_rq_simple(rqp
);
739 smb_rq_getreply(rqp
, &mdp
);
740 md_get_uint8(mdp
, &wc
);
745 md_get_uint8(mdp
, NULL
);
746 md_get_uint8(mdp
, NULL
);
747 md_get_uint16(mdp
, NULL
);
748 md_get_uint16le(mdp
, &resid
);
757 smb_smb_read(struct smb_share
*ssp
, u_int16_t fid
,
758 size_t *len
, size_t *rresid
, struct uio
*uio
, struct smb_cred
*scred
)
765 int error
, rlen
, blksz
;
767 /* Cannot read at/beyond 4G */
768 if (uio
->uio_offset
>= (1LL << 32))
771 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_READ
, scred
, &rqp
);
775 blksz
= SSTOVC(ssp
)->vc_txmax
- SMB_HDRLEN
- 16;
776 rlen
= *len
= min(blksz
, *len
);
778 smb_rq_getrequest(rqp
, &mbp
);
780 mb_put_mem(mbp
, (void *)&fid
, sizeof(fid
), MB_MSYSTEM
);
781 mb_put_uint16le(mbp
, rlen
);
782 mb_put_uint32le(mbp
, uio
->uio_offset
);
783 mb_put_uint16le(mbp
, min(uio
->uio_resid
, 0xffff));
788 error
= smb_rq_simple(rqp
);
791 smb_rq_getreply(rqp
, &mdp
);
792 md_get_uint8(mdp
, &wc
);
797 md_get_uint16le(mdp
, &resid
);
798 md_get_mem(mdp
, NULL
, 4 * 2, MB_MSYSTEM
);
799 md_get_uint16le(mdp
, &bc
);
800 md_get_uint8(mdp
, NULL
); /* ignore buffer type */
801 md_get_uint16le(mdp
, &resid
);
806 error
= md_get_uio(mdp
, uio
, resid
);
816 smb_read(struct smb_share
*ssp
, u_int16_t fid
, struct uio
*uio
,
817 struct smb_cred
*scred
)
819 size_t tsize
, len
, resid
;
821 bool rx
= (SMB_CAPS(SSTOVC(ssp
)) &
822 (SMB_CAP_LARGE_FILES
|SMB_CAP_LARGE_READX
)) != 0;
824 resid
= 0; /* XXX gcc */
826 tsize
= uio
->uio_resid
;
830 error
= smb_smb_readx(ssp
, fid
, &len
, &resid
, uio
, scred
);
832 error
= smb_smb_read(ssp
, fid
, &len
, &resid
, uio
, scred
);
843 smb_smb_write(struct smb_share
*ssp
, u_int16_t fid
, size_t *len
, size_t *rresid
,
844 struct uio
*uio
, struct smb_cred
*scred
)
853 /* Cannot write at/beyond 4G */
854 if (uio
->uio_offset
>= (1LL << 32))
857 blksz
= SSTOVC(ssp
)->vc_txmax
- SMB_HDRLEN
- 16;
861 resid
= *len
= min(blksz
, *len
);
863 error
= smb_rq_alloc(SSTOCP(ssp
), SMB_COM_WRITE
, scred
, &rqp
);
866 smb_rq_getrequest(rqp
, &mbp
);
868 mb_put_mem(mbp
, (void *)&fid
, sizeof(fid
), MB_MSYSTEM
);
869 mb_put_uint16le(mbp
, resid
);
870 mb_put_uint32le(mbp
, uio
->uio_offset
);
871 mb_put_uint16le(mbp
, min(uio
->uio_resid
, 0xffff));
874 mb_put_uint8(mbp
, SMB_DT_DATA
);
875 mb_put_uint16le(mbp
, resid
);
877 error
= mb_put_uio(mbp
, uio
, resid
);
881 error
= smb_rq_simple(rqp
);
884 smb_rq_getreply(rqp
, &mdp
);
885 md_get_uint8(mdp
, &wc
);
890 md_get_uint16le(mdp
, &resid
);
898 smb_write(struct smb_share
*ssp
, u_int16_t fid
, struct uio
*uio
,
899 struct smb_cred
*scred
)
902 size_t len
, tsize
, resid
;
903 bool wx
= (SMB_CAPS(SSTOVC(ssp
)) &
904 (SMB_CAP_LARGE_FILES
|SMB_CAP_LARGE_WRITEX
)) != 0;
906 resid
= 0; /* XXX gcc */
908 tsize
= uio
->uio_resid
;
912 error
= smb_smb_writex(ssp
, fid
, &len
, &resid
, uio
, scred
);
914 error
= smb_smb_write(ssp
, fid
, &len
, &resid
, uio
, scred
);
928 smb_smb_echo(struct smb_vc
*vcp
, struct smb_cred
*scred
)
934 error
= smb_rq_alloc(VCTOCP(vcp
), SMB_COM_ECHO
, scred
, &rqp
);
939 mb_put_uint16le(mbp
, 1);
942 mb_put_uint32le(mbp
, 0);
944 error
= smb_rq_simple(rqp
);
945 SMBSDEBUG(("%d\n", error
));