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"},
71 #define SMB_DIALECT_MAX (sizeof(smb_dialects) / sizeof(struct smb_dialect) - 2)
74 smb_vc_maxread(struct smb_vc *vcp)
77 * Specs say up to 64k data bytes, but Windows traffic
78 * uses 60k... no doubt for some good reason.
80 * Don't exceed the server's buffer size if signatures
81 * are enabled otherwise Windows 2003 chokes. Allow space
82 * for the SMB header & a little bit extra.
84 if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_READX) &&
85 (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0)
88 return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64);
92 smb_vc_maxwrite(struct smb_vc *vcp)
97 if ((vcp->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX) &&
98 (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0)
101 return (vcp->vc_sopt.sv_maxtx - SMB_HDRLEN - 64);
105 smb_smb_nomux(struct smb_vc *vcp, struct smb_cred *scred, const char *name)
107 if (scred->scr_td->td_proc == vcp->vc_iod->iod_p)
109 SMBERROR("wrong function called(%s)\n", name);
114 smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred)
116 struct smb_dialect *dp;
117 struct smb_sopt *sp = NULL;
121 u_int8_t wc, stime[8], sblen;
122 u_int16_t dindex, tw, tw1, swlen, bc;
125 if (smb_smb_nomux(vcp, scred, __func__) != 0)
129 vcp->obj.co_flags &= ~(SMBV_ENCRYPT);
131 bzero(sp, sizeof(struct smb_sopt));
132 error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_NEGOTIATE, scred, &rqp);
135 smb_rq_getrequest(rqp, &mbp);
139 for(dp = smb_dialects; dp->d_id != -1; dp++) {
140 mb_put_uint8(mbp, SMB_DT_DIALECT);
141 smb_put_dstring(mbp, vcp, dp->d_name, SMB_CS_NONE);
144 error = smb_rq_simple(rqp);
145 SMBSDEBUG("%d\n", error);
148 smb_rq_getreply(rqp, &mdp);
150 error = md_get_uint8(mdp, &wc);
153 error = md_get_uint16le(mdp, &dindex);
157 SMBERROR("Don't know how to talk with server %s (%d)\n", "xxx", dindex);
161 dp = smb_dialects + dindex;
162 sp->sv_proto = dp->d_id;
163 SMBSDEBUG("Dialect %s (%d, %d)\n", dp->d_name, dindex, wc);
165 if (dp->d_id >= SMB_DIALECT_NTLM0_12) {
168 md_get_uint8(mdp, &sp->sv_sm);
169 md_get_uint16le(mdp, &sp->sv_maxmux);
170 md_get_uint16le(mdp, &sp->sv_maxvcs);
171 md_get_uint32le(mdp, &sp->sv_maxtx);
172 md_get_uint32le(mdp, &sp->sv_maxraw);
173 md_get_uint32le(mdp, &sp->sv_skey);
174 md_get_uint32le(mdp, &sp->sv_caps);
175 md_get_mem(mdp, stime, 8, MB_MSYSTEM);
176 md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz);
177 md_get_uint8(mdp, &sblen);
178 if (sblen && (sp->sv_sm & SMB_SM_ENCRYPT)) {
179 if (sblen != SMB_MAXCHALLENGELEN) {
180 SMBERROR("Unexpected length of security blob (%d)\n", sblen);
183 error = md_get_uint16(mdp, &bc);
186 if (sp->sv_caps & SMB_CAP_EXT_SECURITY)
187 md_get_mem(mdp, NULL, 16, MB_MSYSTEM);
188 error = md_get_mem(mdp, vcp->vc_ch, sblen, MB_MSYSTEM);
191 vcp->vc_chlen = sblen;
192 vcp->obj.co_flags |= SMBV_ENCRYPT;
194 if (sp->sv_sm & SMB_SM_SIGS_REQUIRE)
195 vcp->vc_hflags2 |= SMB_FLAGS2_SECURITY_SIGNATURE;
196 vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
197 if (dp->d_id == SMB_DIALECT_NTLM0_12 &&
198 sp->sv_maxtx < 4096 &&
199 (sp->sv_caps & SMB_CAP_NT_SMBS) == 0) {
200 vcp->obj.co_flags |= SMBV_WIN95;
201 SMBSDEBUG("Win95 detected\n");
203 } else if (dp->d_id > SMB_DIALECT_CORE) {
204 md_get_uint16le(mdp, &tw);
206 md_get_uint16le(mdp, &tw);
208 md_get_uint16le(mdp, &sp->sv_maxmux);
209 md_get_uint16le(mdp, &sp->sv_maxvcs);
210 md_get_uint16le(mdp, &tw); /* rawmode */
211 md_get_uint32le(mdp, &sp->sv_skey);
212 if (wc == 13) { /* >= LANMAN1 */
213 md_get_uint16(mdp, &tw); /* time */
214 md_get_uint16(mdp, &tw1); /* date */
215 md_get_uint16le(mdp, (u_int16_t*)&sp->sv_tz);
216 md_get_uint16le(mdp, &swlen);
217 if (swlen > SMB_MAXCHALLENGELEN)
219 md_get_uint16(mdp, NULL); /* mbz */
220 if (md_get_uint16(mdp, &bc) != 0)
224 if (swlen && (sp->sv_sm & SMB_SM_ENCRYPT)) {
225 error = md_get_mem(mdp, vcp->vc_ch, swlen, MB_MSYSTEM);
228 vcp->vc_chlen = swlen;
229 vcp->obj.co_flags |= SMBV_ENCRYPT;
232 vcp->vc_hflags2 |= SMB_FLAGS2_KNOWS_LONG_NAMES;
233 } else { /* an old CORE protocol */
239 vcp->vc_maxvcs = sp->sv_maxvcs;
240 if (vcp->vc_maxvcs <= 1) {
241 if (vcp->vc_maxvcs == 0)
244 if (sp->sv_maxtx <= 0 || sp->sv_maxtx > 0xffff)
247 sp->sv_maxtx = min(sp->sv_maxtx,
248 63*1024 + SMB_HDRLEN + 16);
249 SMB_TRAN_GETPARAM(vcp, SMBTP_RCVSZ, &maxqsz);
250 vcp->vc_rxmax = min(smb_vc_maxread(vcp), maxqsz - 1024);
251 SMB_TRAN_GETPARAM(vcp, SMBTP_SNDSZ, &maxqsz);
252 vcp->vc_wxmax = min(smb_vc_maxwrite(vcp), maxqsz - 1024);
253 vcp->vc_txmax = min(sp->sv_maxtx, maxqsz);
254 SMBSDEBUG("TZ = %d\n", sp->sv_tz);
255 SMBSDEBUG("CAPS = %x\n", sp->sv_caps);
256 SMBSDEBUG("MAXMUX = %d\n", sp->sv_maxmux);
257 SMBSDEBUG("MAXVCS = %d\n", sp->sv_maxvcs);
258 SMBSDEBUG("MAXRAW = %d\n", sp->sv_maxraw);
259 SMBSDEBUG("MAXTX = %d\n", sp->sv_maxtx);
267 smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred)
273 smb_uniptr unipp, ntencpass = NULL;
274 char *pp, *up, *pbuf, *encpass;
275 int error, plen, uniplen, ulen, upper;
281 vcp->vc_smbuid = SMB_UID_UNKNOWN;
283 if (smb_smb_nomux(vcp, scred, __func__) != 0)
286 error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_SESSION_SETUP_ANDX, scred, &rqp);
289 pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
290 encpass = malloc(24, M_SMBTEMP, M_WAITOK);
291 if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
293 * We try w/o uppercasing first so Samba mixed case
294 * passwords work. If that fails we come back and try
295 * uppercasing to satisfy OS/2 and Windows for Workgroups.
298 iconv_convstr(vcp->vc_toupper, pbuf,
299 smb_vc_getpass(vcp)/*, SMB_MAXPASSWORDLEN*/);
301 strncpy(pbuf, smb_vc_getpass(vcp), SMB_MAXPASSWORDLEN);
302 pbuf[SMB_MAXPASSWORDLEN] = '\0';
304 if (!SMB_UNICODE_STRINGS(vcp))
305 iconv_convstr(vcp->vc_toserver, pbuf, pbuf/*,
306 SMB_MAXPASSWORDLEN*/);
308 if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
310 smb_encrypt(pbuf, vcp->vc_ch, encpass);
311 ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
312 if (SMB_UNICODE_STRINGS(vcp)) {
313 strncpy(pbuf, smb_vc_getpass(vcp),
315 pbuf[SMB_MAXPASSWORDLEN] = '\0';
317 iconv_convstr(vcp->vc_toserver, pbuf,
318 smb_vc_getpass(vcp)/*,
319 SMB_MAXPASSWORDLEN*/);
320 smb_ntencrypt(pbuf, vcp->vc_ch, (u_char*)ntencpass);
324 plen = strlen(pbuf) + 1;
327 ntencpass = malloc(uniplen, M_SMBTEMP, M_WAITOK);
328 smb_strtouni(ntencpass, smb_vc_getpass(vcp));
332 * The uniplen is zeroed because Samba cannot deal
333 * with this 2nd cleartext password. This Samba
334 * "bug" is actually a workaround for problems in
342 * In the share security mode password will be used
343 * only in the tree authentication
348 uniplen = 0 /* sizeof(smb_unieol) */;
352 up = vcp->vc_username;
353 ulen = strlen(up) + 1;
355 * If userid is null we are attempting anonymous browse login
356 * so passwords must be zero length.
360 mb_put_uint8(mbp, 0xff);
361 mb_put_uint8(mbp, 0);
362 mb_put_uint16le(mbp, 0);
363 mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxtx);
364 mb_put_uint16le(mbp, vcp->vc_sopt.sv_maxmux);
365 mb_put_uint16le(mbp, vcp->vc_number);
366 mb_put_uint32le(mbp, vcp->vc_sopt.sv_skey);
367 mb_put_uint16le(mbp, plen);
368 if (SMB_DIALECT(vcp) < SMB_DIALECT_NTLM0_12) {
369 mb_put_uint32le(mbp, 0);
372 mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
373 smb_put_dstring(mbp, vcp, up, SMB_CS_NONE);
375 mb_put_uint16le(mbp, uniplen);
376 mb_put_uint32le(mbp, 0); /* reserved */
377 mb_put_uint32le(mbp, vcp->obj.co_flags & SMBV_UNICODE ?
378 SMB_CAP_UNICODE : 0);
381 mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
382 mb_put_mem(mbp, (caddr_t)unipp, uniplen, MB_MSYSTEM);
383 smb_put_dstring(mbp, vcp, up, SMB_CS_NONE); /* AccountName */
384 smb_put_dstring(mbp, vcp, vcp->vc_domain, SMB_CS_NONE); /* PrimaryDomain */
385 smb_put_dstring(mbp, vcp, "FreeBSD", SMB_CS_NONE); /* Client's OS */
386 smb_put_dstring(mbp, vcp, "NETSMB", SMB_CS_NONE); /* Client name */
390 free(ntencpass, M_SMBTEMP);
391 if (vcp->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE)
393 error = smb_rq_simple(rqp);
394 SMBSDEBUG("%d\n", error);
396 if (rqp->sr_errclass == ERRDOS && rqp->sr_serror == ERRnoaccess)
400 vcp->vc_smbuid = rqp->sr_rpuid;
402 free(encpass, M_SMBTEMP);
403 free(pbuf, M_SMBTEMP);
405 if (error && upper == 1 && vcp->vc_sopt.sv_sm & SMB_SM_USER)
411 smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred)
417 if (vcp->vc_smbuid == SMB_UID_UNKNOWN)
420 if (smb_smb_nomux(vcp, scred, __func__) != 0)
423 error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_LOGOFF_ANDX, scred, &rqp);
428 mb_put_uint8(mbp, 0xff);
429 mb_put_uint8(mbp, 0);
430 mb_put_uint16le(mbp, 0);
434 error = smb_rq_simple(rqp);
435 SMBSDEBUG("%d\n", error);
440 static char smb_any_share[] = "?????";
443 smb_share_typename(int stype)
452 pp = smb_any_share; /* can't use LPT: here... */
469 smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred)
472 struct smb_rq rq, *rqp = &rq;
474 char *pp, *pbuf, *encpass;
475 int error, plen, caseopt, upper;
482 /* Disable Unicode for SMB_COM_TREE_CONNECT_ANDX requests */
483 if (SSTOVC(ssp)->vc_hflags2 & SMB_FLAGS2_UNICODE) {
485 if (vcp->vc_toserver) {
486 iconv_close(vcp->vc_toserver);
487 /* Use NULL until UTF-8 -> ASCII works */
488 vcp->vc_toserver = NULL;
490 if (vcp->vc_tolocal) {
491 iconv_close(vcp->vc_tolocal);
492 /* Use NULL until ASCII -> UTF-8 works*/
493 vcp->vc_tolocal = NULL;
495 vcp->vc_hflags2 &= ~SMB_FLAGS2_UNICODE;
499 ssp->ss_tid = SMB_TID_UNKNOWN;
500 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_CONNECT_ANDX, scred, &rqp);
504 caseopt = SMB_CS_NONE;
505 if (vcp->vc_sopt.sv_sm & SMB_SM_USER) {
511 pbuf = malloc(SMB_MAXPASSWORDLEN + 1, M_SMBTEMP, M_WAITOK);
512 encpass = malloc(24, M_SMBTEMP, M_WAITOK);
514 * We try w/o uppercasing first so Samba mixed case
515 * passwords work. If that fails we come back and try
516 * uppercasing to satisfy OS/2 and Windows for Workgroups.
519 iconv_convstr(vcp->vc_toupper, pbuf,
520 smb_share_getpass(ssp)/*,
521 SMB_MAXPASSWORDLEN*/);
523 strncpy(pbuf, smb_share_getpass(ssp),
525 pbuf[SMB_MAXPASSWORDLEN] = '\0';
527 if (vcp->vc_sopt.sv_sm & SMB_SM_ENCRYPT) {
529 smb_encrypt(pbuf, vcp->vc_ch, encpass);
532 plen = strlen(pbuf) + 1;
538 mb_put_uint8(mbp, 0xff);
539 mb_put_uint8(mbp, 0);
540 mb_put_uint16le(mbp, 0);
541 mb_put_uint16le(mbp, 0); /* Flags */
542 mb_put_uint16le(mbp, plen);
545 mb_put_mem(mbp, pp, plen, MB_MSYSTEM);
546 smb_put_dmem(mbp, vcp, "\\\\", 2, caseopt);
547 pp = vcp->vc_srvname;
548 smb_put_dmem(mbp, vcp, pp, strlen(pp), caseopt);
549 smb_put_dmem(mbp, vcp, "\\", 1, caseopt);
551 smb_put_dstring(mbp, vcp, pp, caseopt);
552 pp = smb_share_typename(ssp->ss_type);
553 smb_put_dstring(mbp, vcp, pp, caseopt);
555 error = smb_rq_simple(rqp);
556 SMBSDEBUG("%d\n", error);
559 ssp->ss_tid = rqp->sr_rptid;
560 ssp->ss_vcgenid = vcp->vc_genid;
561 ssp->ss_flags |= SMBS_CONNECTED;
564 free(encpass, M_SMBTEMP);
566 free(pbuf, M_SMBTEMP);
568 if (error && upper == 1)
574 smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred)
580 if (ssp->ss_tid == SMB_TID_UNKNOWN)
582 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_TREE_DISCONNECT, scred, &rqp);
590 error = smb_rq_simple(rqp);
591 SMBSDEBUG("%d\n", error);
593 ssp->ss_tid = SMB_TID_UNKNOWN;
598 smb_smb_readx(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
599 struct uio *uio, struct smb_cred *scred)
606 u_int16_t residhi, residlo, off, doff;
609 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ_ANDX, scred, &rqp);
612 smb_rq_getrequest(rqp, &mbp);
614 mb_put_uint8(mbp, 0xff); /* no secondary command */
615 mb_put_uint8(mbp, 0); /* MBZ */
616 mb_put_uint16le(mbp, 0); /* offset to secondary */
617 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
618 mb_put_uint32le(mbp, uio->uio_offset);
619 *len = min(SSTOVC(ssp)->vc_rxmax, *len);
620 mb_put_uint16le(mbp, *len); /* MaxCount */
621 mb_put_uint16le(mbp, *len); /* MinCount (only indicates blocking) */
622 mb_put_uint32le(mbp, (unsigned)*len >> 16); /* MaxCountHigh */
623 mb_put_uint16le(mbp, *len); /* Remaining ("obsolete") */
624 mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */
629 error = smb_rq_simple(rqp);
632 smb_rq_getreply(rqp, &mdp);
634 md_get_uint8(mdp, &wc);
640 md_get_uint8(mdp, NULL);
642 md_get_uint8(mdp, NULL);
644 md_get_uint16le(mdp, NULL);
646 md_get_uint16le(mdp, NULL);
648 md_get_uint16le(mdp, NULL); /* data compaction mode */
650 md_get_uint16le(mdp, NULL);
652 md_get_uint16le(mdp, &residlo);
654 md_get_uint16le(mdp, &doff); /* data offset */
656 md_get_uint16le(mdp, &residhi);
658 resid = (residhi << 16) | residlo;
659 md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
661 md_get_uint16le(mdp, NULL); /* ByteCount */
663 if (doff > off) /* pad byte(s)? */
664 md_get_mem(mdp, NULL, doff - off, MB_MSYSTEM);
669 error = md_get_uio(mdp, uio, resid);
679 smb_smb_writex(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
680 struct uio *uio, struct smb_cred *scred)
689 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE_ANDX, scred, &rqp);
692 smb_rq_getrequest(rqp, &mbp);
694 mb_put_uint8(mbp, 0xff); /* no secondary command */
695 mb_put_uint8(mbp, 0); /* MBZ */
696 mb_put_uint16le(mbp, 0); /* offset to secondary */
697 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
698 mb_put_uint32le(mbp, uio->uio_offset);
699 mb_put_uint32le(mbp, 0); /* MBZ (timeout) */
700 mb_put_uint16le(mbp, 0); /* !write-thru */
701 mb_put_uint16le(mbp, 0);
702 *len = min(SSTOVC(ssp)->vc_wxmax, *len);
703 mb_put_uint16le(mbp, (unsigned)*len >> 16);
704 mb_put_uint16le(mbp, *len);
705 mb_put_uint16le(mbp, 64); /* data offset from header start */
706 mb_put_uint32le(mbp, uio->uio_offset >> 32); /* OffsetHigh */
710 mb_put_uint8(mbp, 0xee); /* mimic xp pad byte! */
711 error = mb_put_uio(mbp, uio, *len);
715 error = smb_rq_simple(rqp);
718 smb_rq_getreply(rqp, &mdp);
719 md_get_uint8(mdp, &wc);
724 md_get_uint8(mdp, NULL);
725 md_get_uint8(mdp, NULL);
726 md_get_uint16le(mdp, NULL);
727 md_get_uint16le(mdp, &resid);
736 smb_smb_read(struct smb_share *ssp, u_int16_t fid,
737 int *len, int *rresid, struct uio *uio, struct smb_cred *scred)
744 int error, rlen, blksz;
746 if (SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_READX)
747 return (smb_smb_readx(ssp, fid, len, rresid, uio, scred));
749 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_READ, scred, &rqp);
753 blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
754 rlen = *len = min(blksz, *len);
756 smb_rq_getrequest(rqp, &mbp);
758 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
759 mb_put_uint16le(mbp, rlen);
760 mb_put_uint32le(mbp, uio->uio_offset);
761 mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
766 error = smb_rq_simple(rqp);
769 smb_rq_getreply(rqp, &mdp);
770 md_get_uint8(mdp, &wc);
775 md_get_uint16le(mdp, &resid);
776 md_get_mem(mdp, NULL, 4 * 2, MB_MSYSTEM);
777 md_get_uint16le(mdp, &bc);
778 md_get_uint8(mdp, NULL); /* ignore buffer type */
779 md_get_uint16le(mdp, &resid);
784 error = md_get_uio(mdp, uio, resid);
794 smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
795 struct smb_cred *scred)
797 int tsize, len, resid;
800 tsize = uio->uio_resid;
804 error = smb_smb_read(ssp, fid, &len, &resid, uio, scred);
815 smb_smb_write(struct smb_share *ssp, u_int16_t fid, int *len, int *rresid,
816 struct uio *uio, struct smb_cred *scred)
825 if (*len && SSTOVC(ssp)->vc_sopt.sv_caps & SMB_CAP_LARGE_WRITEX)
826 return (smb_smb_writex(ssp, fid, len, rresid, uio, scred));
828 blksz = SSTOVC(ssp)->vc_txmax - SMB_HDRLEN - 16;
832 resid = *len = min(blksz, *len);
834 error = smb_rq_alloc(SSTOCP(ssp), SMB_COM_WRITE, scred, &rqp);
837 smb_rq_getrequest(rqp, &mbp);
839 mb_put_mem(mbp, (caddr_t)&fid, sizeof(fid), MB_MSYSTEM);
840 mb_put_uint16le(mbp, resid);
841 mb_put_uint32le(mbp, uio->uio_offset);
842 mb_put_uint16le(mbp, min(uio->uio_resid, 0xffff));
845 mb_put_uint8(mbp, SMB_DT_DATA);
846 mb_put_uint16le(mbp, resid);
848 error = mb_put_uio(mbp, uio, resid);
852 error = smb_rq_simple(rqp);
855 smb_rq_getreply(rqp, &mdp);
856 md_get_uint8(mdp, &wc);
861 md_get_uint16le(mdp, &resid);
869 smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio,
870 struct smb_cred *scred)
872 int error = 0, len, tsize, resid;
875 tsize = uio->uio_resid;
880 error = smb_smb_write(ssp, fid, &len, &resid, uio, scred);
891 * Errors can happen on the copyin, the rpc, etc. So they
892 * imply resid is unreliable. The only safe thing is
893 * to pretend zero bytes made it. We needn't restore the
894 * iovs because callers don't depend on them in error
895 * paths - uio_resid and uio_offset are what matter.
903 smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred)
909 error = smb_rq_alloc(VCTOCP(vcp), SMB_COM_ECHO, scred, &rqp);
914 mb_put_uint16le(mbp, 1);
917 mb_put_uint32le(mbp, 0);
919 error = smb_rq_simple(rqp);
920 SMBSDEBUG("%d\n", error);