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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * various SMB requests. Most of the routines merely packs data into mbufs.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
46 #include <sys/sysctl.h>
47 #include <sys/socket.h>
50 #include <sys/iconv.h>
52 #include <netsmb/smb.h>
53 #include <netsmb/smb_subr.h>
54 #include <netsmb/smb_rq.h>
55 #include <netsmb/smb_conn.h>
56 #include <netsmb/smb_tran.h>
58 #include "opt_netsmb.h"
65 static struct smb_dialect smb_dialects[] = {
66 {SMB_DIALECT_CORE, "PC NETWORK PROGRAM 1.0"},
67 {SMB_DIALECT_COREPLUS, "MICROSOFT NETWORKS 1.03"},
68 {SMB_DIALECT_LANMAN1_0, "MICROSOFT NETWORKS 3.0"},
69 {SMB_DIALECT_LANMAN1_0, "LANMAN1.0"},
70 {SMB_DIALECT_LANMAN2_0, "LM1.2X002"},
71 {SMB_DIALECT_LANMAN2_0, "Samba"},
72 {SMB_DIALECT_NTLM0_12, "NT LANMAN 1.0"},
73 {SMB_DIALECT_NTLM0_12, "NT LM 0.12"},
77 #define SMB_DIALECT_MAX (sizeof(smb_dialects) / sizeof(struct smb_dialect) - 2)
80 smb_vc_maxread(struct smb_vc *vcp)
83 * Specs say up to 64k data bytes, but Windows traffic
84 * uses 60k... no doubt for some good reason.
86 * Don't exceed the server's buffer size if signatures
87 * are enabled otherwise Windows 2003 chokes. Allow space
88 * for the SMB header & a little bit extra.
90 if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_READX) &&
91 (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0)
94 return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64);
98 smb_vc_maxwrite(struct smb_vc *vcp)
103 if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX) &&
104 (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0)
107 return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64);
111 smb_smb_nomux(struct smb_vc *vcp, struct smb_cred *scred, const char *name)
113 if (scred->scr_td->td_proc == vcp->vc_iod->iod_p)
115 SMBERROR("wrong function called(%s)\n", name);
120 smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred)
122 struct smb_dialect *dp;
123 struct smb_sopt *sp = NULL;
127 u_int8_t wc, stime[8], sblen;
128 u_int16_t dindex, tw, tw1, swlen, bc;
131 if (smb_smb_nomux(vcp, scred, __func__) != 0)
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_uint16(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 vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
203 if (dp->d_id == SMB_DIALECT_NTLM0_12 &&
204 sp->sv_maxtx < 4096 &&
205 (sp->sv_caps & SMB_CAP_NT_SMBS) == 0) {
206 vcp->obj.co_flags |= SMBV_WIN95;
207 SMBSDEBUG("Win95 detected\n");
209 } else if (dp->d_id > SMB_DIALECT_CORE) {
210 md_get_uint16le(mdp, &tw);
212 md_get_uint16le(mdp, &tw);
214 md_get_uint16le(mdp, &sp->sv_maxmux);
215 md_get_uint16le(mdp, &sp->sv_maxvcs);
216 md_get_uint16le(mdp, &tw); /* rawmode */
217 md_get_uint32le(mdp, &sp->sv_skey);
218 if (wc == 13) { /* >= LANMAN1 */
219 md_get_uint16(mdp, &tw); /* time */
220 md_get_uint16(mdp, &tw1); /* date */
221 md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz);
222 md_get_uint16le(mdp, &swlen);
223 if (swlen > SMB_MAXCHALLENGELEN)
225 md_get_uint16(mdp, NULL); /* mbz */
226 if (md_get_uint16(mdp, &bc) != 0)
230 if (swlen && (sp->sv_sm & SMB_SM_ENCRYPT)) {
231 error = md_get_mem(mdp, vcp->vc_ch, swlen, MB_MSYSTEM);
234 vcp->vc_chlen = swlen;
235 vcp->obj.co_flags |= SMBV_ENCRYPT;
238 vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
239 } else { /* an old CORE protocol */
245 vcp->vc_maxvcs = sp->sv_maxvcs;
246 if (vcp->vc_maxvcs <= 1) {
247 if (vcp->vc_maxvcs == 0)
250 if (sp->sv_maxtx <= 0 || sp->sv_maxtx > 0xffff)
253 sp->sv_maxtx = min(sp->sv_maxtx,
254 63*1024 + SMB_HDRLEN + 16);
255 SMB_TRAN_GETPARAM(vcp, SMBTP_RCVSZ, &maxqsz);
256 vcp->vc_rxmax = min(smb_vc_maxread(vcp), maxqsz - 1024);
257 SMB_TRAN_GETPARAM(vcp, SMBTP_SNDSZ, &maxqsz);
258 vcp->vc_wxmax = min(smb_vc_maxwrite(vcp), maxqsz - 1024);
259 vcp->vc_txmax = min(sp->sv_maxtx, maxqsz);
260 SMBSDEBUG("TZ = %d\n", sp->sv_tz);
261 SMBSDEBUG("CAPS = %x\n", sp->sv_caps);
262 SMBSDEBUG("MAXMUX = %d\n", sp->sv_maxmux);
263 SMBSDEBUG("MAXVCS = %d\n", sp->sv_maxvcs);
264 SMBSDEBUG("MAXRAW = %d\n", sp->sv_maxraw);
265 SMBSDEBUG("MAXTX = %d\n", sp->sv_maxtx);
273 smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred)
279 smb_uniptr unipp, ntencpass = NULL;
280 char *pp, *up, *pbuf, *encpass;
281 int error, plen, uniplen, ulen, upper;
287 vcp->vc_smbuid = SMB_UID_UNKNOWN;
289 if (smb_smb_nomux(vcp, scred, __func__) != 0)
292 error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_SESSION_SETUP_ANDX, scred, &rqp);
295 pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
296 encpass = malloc(24, M_SMBTEMP, M_WAITOK);
297 if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
299 * We try w/o uppercasing first so Samba mixed case
300 * passwords work. If that fails we come back and try
301 * uppercasing to satisfy OS/2 and Windows for Workgroups.
304 iconv_convstr(vcp->vc_toupper, pbuf,
305 smb_vc_getpass(vcp)/*, SMB_MAXPASSWORDLEN*/);
307 strncpy(pbuf, smb_vc_getpass(vcp), SMB_MAXPASSWORDLEN);
308 pbuf[SMB_MAXPASSWORDLEN] = '\0';
310 if (!SMB_UNICODE_STRINGS(vcp))
311 iconv_convstr(vcp->vc_toserver, pbuf, pbuf/*,
312 SMB_MAXPASSWORDLEN*/);
314 if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
316 smb_encrypt(pbuf, vcp->vc_ch, encpass);
317 ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
318 if (SMB_UNICODE_STRINGS(vcp)) {
319 strncpy(pbuf, smb_vc_getpass(vcp),
321 pbuf[SMB_MAXPASSWORDLEN] = '\0';
323 iconv_convstr(vcp->vc_toserver, pbuf,
324 smb_vc_getpass(vcp)/*,
325 SMB_MAXPASSWORDLEN*/);
326 smb_ntencrypt(pbuf, vcp->vc_ch, (u_char*)ntencpass);
330 plen = strlen(pbuf) + 1;
333 ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
334 smb_strtouni(ntencpass, smb_vc_getpass(vcp));
338 * The uniplen is zeroed because Samba cannot deal
339 * with this 2nd cleartext password. This Samba
340 * "bug" is actually a workaround for problems in
348 * In the share security mode password will be used
349 * only in the tree authentication
354 uniplen = 0 /* sizeof(smb_unieol) */;
358 up = vcp->vc_username;
359 ulen = strlen(up) + 1;
361 * If userid is null we are attempting anonymous browse login
362 * so passwords must be zero length.
366 mb_put_uint8(mbp, 0xff);
367 mb_put_uint8(mbp, 0);
368 mb_put_uint16le(mbp, 0);
369 mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxtx);
370 mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxmux);
371 mb_put_uint16le(mbp, vcp->vc_number);
372 mb_put_uint32le(mbp, vcp->vc_sopt.sv_skey);
373 mb_put_uint16le(mbp, plen);
374 if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12) {
375 mb_put_uint32le(mbp, 0);
378 mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
379 smb_put_dstring(mbp, vcp, up, SMB_CS_NONE);
381 mb_put_uint16le(mbp, uniplen);
382 mb_put_uint32le(mbp, 0); /* reserved */
383 mb_put_uint32le(mbp, vcp->obj.co_flags & SMBV_UNICODE ?
384 SMB_CAP_UNICODE : 0);
387 mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
388 mb_put_mem(mbp, (caddr_t)unipp, uniplen, MB_MSYSTEM);
389 smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); /* AccountName */
390 smb_put_dstring(mbp, vcp, vcp->vc_domain, SMB_CS_NONE); /* PrimaryDomain */
391 smb_put_dstring(mbp, vcp, "FreeBSD", SMB_CS_NONE); /* Client's OS */
392 smb_put_dstring(mbp, vcp, "NETSMB", SMB_CS_NONE); /* Client name */
396 free(ntencpass, M_SMBTEMP);
397 if (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE)
399 error = smb_rq_simple(rqp);
400 SMBSDEBUG("%d\n", error);
402 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnoaccess)
406 vcp->vc_smbuid = rqp->sr_rpuid;
408 free(encpass, M_SMBTEMP);
409 free(pbuf, M_SMBTEMP);
411 if (error && upper == 1 && vcp->vc_sopt.sv_sm & SMB_SM_USER)
417 smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred)
423 if (vcp->vc_smbuid == SMB_UID_UNKNOWN)
426 if (smb_smb_nomux(vcp, scred, __func__) != 0)
429 error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_LOGOFF_ANDX, scred, &rqp);
434 mb_put_uint8(mbp, 0xff);
435 mb_put_uint8(mbp, 0);
436 mb_put_uint16le(mbp, 0);
440 error = smb_rq_simple(rqp);
441 SMBSDEBUG("%d\n", error);
446 static char smb_any_share[] = "?????";
449 smb_share_typename(int stype)
458 pp = smb_any_share; /* can't use LPT: here... */
475 smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred)
478 struct smb_rq rq, *rqp = &rq;
480 char *pp, *pbuf, *encpass;
481 int error, plen, caseopt, upper;
488 /* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */
489 if (SSTOVC(ssp)->vc_hflags2 & SMB_FLAGS2_UNICODE) {
491 if (vcp->vc_toserver) {
492 iconv_close(vcp->vc_toserver);
493 /* Use NULL until UTF-8 -> ASCII works */
494 vcp->vc_toserver = NULL;
496 if (vcp->vc_tolocal) {
497 iconv_close(vcp->vc_tolocal);
498 /* Use NULL until ASCII -> UTF-8 works*/
499 vcp->vc_tolocal = NULL;
501 vcp->vc_hflags2 &= ~SMB_FLAGS2_UNICODE;
505 ssp->ss_tid = SMB_TID_UNKNOWN;
506 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_CONNECT_ANDX, scred, &rqp);
510 caseopt = SMB_CS_NONE;
511 if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
517 pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
518 encpass = malloc(24, M_SMBTEMP, M_WAITOK);
520 * We try w/o uppercasing first so Samba mixed case
521 * passwords work. If that fails we come back and try
522 * uppercasing to satisfy OS/2 and Windows for Workgroups.
525 iconv_convstr(vcp->vc_toupper, pbuf,
526 smb_share_getpass(ssp)/*,
527 SMB_MAXPASSWORDLEN*/);
529 strncpy(pbuf, smb_share_getpass(ssp),
531 pbuf[SMB_MAXPASSWORDLEN] = '\0';
533 if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
535 smb_encrypt(pbuf, vcp->vc_ch, encpass);
538 plen = strlen(pbuf) + 1;
544 mb_put_uint8(mbp, 0xff);
545 mb_put_uint8(mbp, 0);
546 mb_put_uint16le(mbp, 0);
547 mb_put_uint16le(mbp, 0); /* Flags */
548 mb_put_uint16le(mbp, plen);
551 mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
552 smb_put_dmem(mbp, vcp, "\\\\", 2, caseopt);
553 pp = vcp->vc_srvname;
554 smb_put_dmem(mbp, vcp, pp, strlen(pp), caseopt);
555 smb_put_dmem(mbp, vcp, "\\", 1, caseopt);
557 smb_put_dstring(mbp, vcp, pp, caseopt);
558 pp = smb_share_typename(ssp->ss_type);
559 smb_put_dstring(mbp, vcp, pp, caseopt);
561 error = smb_rq_simple(rqp);
562 SMBSDEBUG("%d\n", error);
565 ssp->ss_tid = rqp->sr_rptid;
566 ssp->ss_vcgenid = vcp->vc_genid;
567 ssp->ss_flags |= SMBS_CONNECTED;
570 free(encpass, M_SMBTEMP);
572 free(pbuf, M_SMBTEMP);
574 if (error && upper == 1)
580 smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred)
586 if (ssp->ss_tid == SMB_TID_UNKNOWN)
588 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_DISCONNECT, scred, &rqp);
596 error = smb_rq_simple(rqp);
597 SMBSDEBUG("%d\n", error);
599 ssp->ss_tid = SMB_TID_UNKNOWN;
604 smb_smb_readx(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
605 struct uio *uio, struct smb_cred *scred)
612 u_int16_t residhi, residlo, off, doff;
615 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ_ANDX, scred, &rqp);
618 smb_rq_getrequest(rqp, &mbp);
620 mb_put_uint8(mbp, 0xff); /* no secondary command */
621 mb_put_uint8(mbp, 0); /* MBZ */
622 mb_put_uint16le(mbp, 0); /* offset to secondary */
623 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
624 mb_put_uint32le(mbp, uio->uio_offset);
625 *len = min(SSTOVC(ssp)->vc_rxmax, *len);
626 mb_put_uint16le(mbp, *len); /* MaxCount */
627 mb_put_uint16le(mbp, *len); /* MinCount (only indicates blocking) */
628 mb_put_uint32le(mbp, (unsigned)*len >> 16); /* MaxCountHigh */
629 mb_put_uint16le(mbp, *len); /* Remaining ("obsolete") */
630 mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */
635 error = smb_rq_simple(rqp);
638 smb_rq_getreply(rqp, &mdp);
640 md_get_uint8(mdp, &wc);
646 md_get_uint8(mdp, NULL);
648 md_get_uint8(mdp, NULL);
650 md_get_uint16le(mdp, NULL);
652 md_get_uint16le(mdp, NULL);
654 md_get_uint16le(mdp, NULL); /* data compaction mode */
656 md_get_uint16le(mdp, NULL);
658 md_get_uint16le(mdp, &residlo);
660 md_get_uint16le(mdp, &doff); /* data offset */
662 md_get_uint16le(mdp, &residhi);
664 resid = (residhi << 16) | residlo;
665 md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
667 md_get_uint16le(mdp, NULL); /* ByteCount */
669 if (doff > off) /* pad byte(s)? */
670 md_get_mem(mdp, NULL, doff - off, MB_MSYSTEM);
675 error = md_get_uio(mdp, uio, resid);
685 smb_smb_writex(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
686 struct uio *uio, struct smb_cred *scred)
695 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE_ANDX, scred, &rqp);
698 smb_rq_getrequest(rqp, &mbp);
700 mb_put_uint8(mbp, 0xff); /* no secondary command */
701 mb_put_uint8(mbp, 0); /* MBZ */
702 mb_put_uint16le(mbp, 0); /* offset to secondary */
703 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
704 mb_put_uint32le(mbp, uio->uio_offset);
705 mb_put_uint32le(mbp, 0); /* MBZ (timeout) */
706 mb_put_uint16le(mbp, 0); /* !write-thru */
707 mb_put_uint16le(mbp, 0);
708 *len = min(SSTOVC(ssp)->vc_wxmax, *len);
709 mb_put_uint16le(mbp, (unsigned)*len >> 16);
710 mb_put_uint16le(mbp, *len);
711 mb_put_uint16le(mbp, 64); /* data offset from header start */
712 mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */
716 mb_put_uint8(mbp, 0xee); /* mimic xp pad byte! */
717 error = mb_put_uio(mbp, uio, *len);
721 error = smb_rq_simple(rqp);
724 smb_rq_getreply(rqp, &mdp);
725 md_get_uint8(mdp, &wc);
730 md_get_uint8(mdp, NULL);
731 md_get_uint8(mdp, NULL);
732 md_get_uint16le(mdp, NULL);
733 md_get_uint16le(mdp, &resid);
742 smb_smb_read(struct smb_share *ssp, u_int16_t fid,
743 int *len, int *rresid, struct uio *uio, struct smb_cred *scred)
750 int error, rlen, blksz;
752 if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_READX)
753 return (smb_smb_readx(ssp, fid, len, rresid, uio, scred));
755 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ, scred, &rqp);
759 blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
760 rlen = *len = min(blksz, *len);
762 smb_rq_getrequest(rqp, &mbp);
764 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
765 mb_put_uint16le(mbp, rlen);
766 mb_put_uint32le(mbp, uio->uio_offset);
767 mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
772 error = smb_rq_simple(rqp);
775 smb_rq_getreply(rqp, &mdp);
776 md_get_uint8(mdp, &wc);
781 md_get_uint16le(mdp, &resid);
782 md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
783 md_get_uint16le(mdp, &bc);
784 md_get_uint8(mdp, NULL); /* ignore buffer type */
785 md_get_uint16le(mdp, &resid);
790 error = md_get_uio(mdp, uio, resid);
800 smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
801 struct smb_cred *scred)
803 int tsize, len, resid;
806 tsize = uio->uio_resid;
810 error = smb_smb_read(ssp, fid, &len, &resid, uio, scred);
821 smb_smb_write(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
822 struct uio *uio, struct smb_cred *scred)
831 if (*len && SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX)
832 return (smb_smb_writex(ssp, fid, len, rresid, uio, scred));
834 blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
838 resid = *len = min(blksz, *len);
840 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp);
843 smb_rq_getrequest(rqp, &mbp);
845 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
846 mb_put_uint16le(mbp, resid);
847 mb_put_uint32le(mbp, uio->uio_offset);
848 mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
851 mb_put_uint8(mbp, SMB_DT_DATA);
852 mb_put_uint16le(mbp, resid);
854 error = mb_put_uio(mbp, uio, resid);
858 error = smb_rq_simple(rqp);
861 smb_rq_getreply(rqp, &mdp);
862 md_get_uint8(mdp, &wc);
867 md_get_uint16le(mdp, &resid);
875 smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
876 struct smb_cred *scred)
878 int error = 0, len, tsize, resid;
881 tsize = uio->uio_resid;
886 error = smb_smb_write(ssp, fid, &len, &resid, uio, scred);
897 * Errors can happen on the copyin, the rpc, etc. So they
898 * imply resid is unreliable. The only safe thing is
899 * to pretend zero bytes made it. We needn't restore the
900 * iovs because callers don't depend on them in error
901 * paths - uio_resid and uio_offset are what matter.
909 smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred)
915 error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_ECHO, scred, &rqp);
920 mb_put_uint16le(mbp, 1);
923 mb_put_uint32le(mbp, 0);
925 error = smb_rq_simple(rqp);
926 SMBSDEBUG("%d\n", error);