smb_rq.c revision 95533
175374Sbp/* 275374Sbp * Copyright (c) 2000-2001, Boris Popov 375374Sbp * All rights reserved. 475374Sbp * 575374Sbp * Redistribution and use in source and binary forms, with or without 675374Sbp * modification, are permitted provided that the following conditions 775374Sbp * are met: 875374Sbp * 1. Redistributions of source code must retain the above copyright 975374Sbp * notice, this list of conditions and the following disclaimer. 1075374Sbp * 2. Redistributions in binary form must reproduce the above copyright 1175374Sbp * notice, this list of conditions and the following disclaimer in the 1275374Sbp * documentation and/or other materials provided with the distribution. 1375374Sbp * 3. All advertising materials mentioning features or use of this software 1475374Sbp * must display the following acknowledgement: 1575374Sbp * This product includes software developed by Boris Popov. 1675374Sbp * 4. Neither the name of the author nor the names of any co-contributors 1775374Sbp * may be used to endorse or promote products derived from this software 1875374Sbp * without specific prior written permission. 1975374Sbp * 2075374Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2175374Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2275374Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2375374Sbp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2475374Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2575374Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2675374Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2775374Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2875374Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2975374Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3075374Sbp * SUCH DAMAGE. 3175374Sbp * 3275374Sbp * $FreeBSD: head/sys/netsmb/smb_rq.c 95533 2002-04-26 22:48:23Z mike $ 3375374Sbp */ 3475374Sbp#include <sys/param.h> 3575374Sbp#include <sys/systm.h> 3695533Smike#include <sys/endian.h> 3775374Sbp#include <sys/kernel.h> 3875374Sbp#include <sys/malloc.h> 3975374Sbp#include <sys/proc.h> 4075374Sbp#include <sys/lock.h> 4175374Sbp#include <sys/sysctl.h> 4275374Sbp#include <sys/socket.h> 4375374Sbp#include <sys/socketvar.h> 4475374Sbp#include <sys/mbuf.h> 4575374Sbp 4675374Sbp#include <netsmb/smb.h> 4775374Sbp#include <netsmb/smb_conn.h> 4875374Sbp#include <netsmb/smb_rq.h> 4975374Sbp#include <netsmb/smb_subr.h> 5075374Sbp#include <netsmb/smb_tran.h> 5175374Sbp 5275374SbpMALLOC_DEFINE(M_SMBRQ, "SMBRQ", "SMB request"); 5375374Sbp 5475374SbpMODULE_DEPEND(netsmb, libmchain, 1, 1, 1); 5575374Sbp 5675374Sbpstatic int smb_rq_reply(struct smb_rq *rqp); 5775374Sbpstatic int smb_rq_enqueue(struct smb_rq *rqp); 5875374Sbpstatic int smb_rq_getenv(struct smb_connobj *layer, 5975374Sbp struct smb_vc **vcpp, struct smb_share **sspp); 6075374Sbpstatic int smb_rq_new(struct smb_rq *rqp, u_char cmd); 6175374Sbpstatic int smb_t2_reply(struct smb_t2rq *t2p); 6275374Sbp 6375374Sbpint 6475374Sbpsmb_rq_alloc(struct smb_connobj *layer, u_char cmd, struct smb_cred *scred, 6575374Sbp struct smb_rq **rqpp) 6675374Sbp{ 6775374Sbp struct smb_rq *rqp; 6875374Sbp int error; 6975374Sbp 7075374Sbp MALLOC(rqp, struct smb_rq *, sizeof(*rqp), M_SMBRQ, M_WAITOK); 7175374Sbp if (rqp == NULL) 7275374Sbp return ENOMEM; 7375374Sbp error = smb_rq_init(rqp, layer, cmd, scred); 7475374Sbp rqp->sr_flags |= SMBR_ALLOCED; 7575374Sbp if (error) { 7675374Sbp smb_rq_done(rqp); 7775374Sbp return error; 7875374Sbp } 7975374Sbp *rqpp = rqp; 8075374Sbp return 0; 8175374Sbp} 8275374Sbp 8375374Sbpstatic char tzero[12]; 8475374Sbp 8575374Sbpint 8675374Sbpsmb_rq_init(struct smb_rq *rqp, struct smb_connobj *layer, u_char cmd, 8775374Sbp struct smb_cred *scred) 8875374Sbp{ 8975374Sbp int error; 9075374Sbp 9175374Sbp bzero(rqp, sizeof(*rqp)); 9275374Sbp smb_sl_init(&rqp->sr_slock, "srslock"); 9375374Sbp error = smb_rq_getenv(layer, &rqp->sr_vc, &rqp->sr_share); 9475374Sbp if (error) 9575374Sbp return error; 9675374Sbp error = smb_vc_access(rqp->sr_vc, scred, SMBM_EXEC); 9775374Sbp if (error) 9875374Sbp return error; 9975374Sbp if (rqp->sr_share) { 10075374Sbp error = smb_share_access(rqp->sr_share, scred, SMBM_EXEC); 10175374Sbp if (error) 10275374Sbp return error; 10375374Sbp } 10475374Sbp rqp->sr_cred = scred; 10575374Sbp rqp->sr_mid = smb_vc_nextmid(rqp->sr_vc); 10675374Sbp return smb_rq_new(rqp, cmd); 10775374Sbp} 10875374Sbp 10975374Sbpstatic int 11075374Sbpsmb_rq_new(struct smb_rq *rqp, u_char cmd) 11175374Sbp{ 11275374Sbp struct smb_vc *vcp = rqp->sr_vc; 11375374Sbp struct mbchain *mbp = &rqp->sr_rq; 11475374Sbp int error; 11575374Sbp 11675374Sbp rqp->sr_sendcnt = 0; 11775374Sbp mb_done(mbp); 11875374Sbp md_done(&rqp->sr_rp); 11975374Sbp error = mb_init(mbp); 12075374Sbp if (error) 12175374Sbp return error; 12275374Sbp mb_put_mem(mbp, SMB_SIGNATURE, SMB_SIGLEN, MB_MSYSTEM); 12375374Sbp mb_put_uint8(mbp, cmd); 12475374Sbp mb_put_uint32le(mbp, 0); /* DosError */ 12575374Sbp mb_put_uint8(mbp, vcp->vc_hflags); 12675374Sbp mb_put_uint16le(mbp, vcp->vc_hflags2); 12775374Sbp mb_put_mem(mbp, tzero, 12, MB_MSYSTEM); 12875374Sbp rqp->sr_rqtid = (u_int16_t*)mb_reserve(mbp, sizeof(u_int16_t)); 12975374Sbp mb_put_uint16le(mbp, 1 /*scred->sc_p->p_pid & 0xffff*/); 13075374Sbp rqp->sr_rquid = (u_int16_t*)mb_reserve(mbp, sizeof(u_int16_t)); 13175374Sbp mb_put_uint16le(mbp, rqp->sr_mid); 13275374Sbp return 0; 13375374Sbp} 13475374Sbp 13575374Sbpvoid 13675374Sbpsmb_rq_done(struct smb_rq *rqp) 13775374Sbp{ 13875374Sbp mb_done(&rqp->sr_rq); 13975374Sbp md_done(&rqp->sr_rp); 14075374Sbp smb_sl_destroy(&rqp->sr_slock); 14175374Sbp if (rqp->sr_flags & SMBR_ALLOCED) 14275374Sbp free(rqp, M_SMBRQ); 14375374Sbp} 14475374Sbp 14575374Sbp/* 14675374Sbp * Simple request-reply exchange 14775374Sbp */ 14875374Sbpint 14975374Sbpsmb_rq_simple(struct smb_rq *rqp) 15075374Sbp{ 15175374Sbp struct smb_vc *vcp = rqp->sr_vc; 15275374Sbp int error = EINVAL, i; 15375374Sbp 15475374Sbp for (i = 0; i < SMB_MAXRCN; i++) { 15575374Sbp rqp->sr_flags &= ~SMBR_RESTART; 15675374Sbp rqp->sr_timo = vcp->vc_timo; 15775374Sbp rqp->sr_state = SMBRQ_NOTSENT; 15875374Sbp error = smb_rq_enqueue(rqp); 15975374Sbp if (error) 16075374Sbp return error; 16175374Sbp error = smb_rq_reply(rqp); 16275374Sbp if (error == 0) 16375374Sbp break; 16475374Sbp if ((rqp->sr_flags & (SMBR_RESTART | SMBR_NORESTART)) != SMBR_RESTART) 16575374Sbp break; 16675374Sbp } 16775374Sbp return error; 16875374Sbp} 16975374Sbp 17075374Sbpstatic int 17175374Sbpsmb_rq_enqueue(struct smb_rq *rqp) 17275374Sbp{ 17375374Sbp struct smb_share *ssp = rqp->sr_share; 17475374Sbp int error; 17575374Sbp 17675374Sbp if (ssp == NULL || rqp->sr_cred == &rqp->sr_vc->vc_iod->iod_scred) { 17775374Sbp return smb_iod_addrq(rqp); 17875374Sbp } 17975374Sbp for (;;) { 18075374Sbp SMBS_ST_LOCK(ssp); 18175374Sbp if (ssp->ss_flags & SMBS_RECONNECTING) { 18275374Sbp msleep(&ssp->ss_vcgenid, SMBS_ST_LOCKPTR(ssp), 18375374Sbp PWAIT | PDROP, "90trcn", hz); 18487192Sbp if (smb_proc_intr(rqp->sr_cred->scr_td->td_proc)) 18575374Sbp return EINTR; 18675374Sbp continue; 18775374Sbp } 18875374Sbp if (smb_share_valid(ssp) || (ssp->ss_flags & SMBS_CONNECTED) == 0) { 18975374Sbp SMBS_ST_UNLOCK(ssp); 19075374Sbp } else { 19175374Sbp SMBS_ST_UNLOCK(ssp); 19275374Sbp error = smb_iod_request(rqp->sr_vc->vc_iod, 19375374Sbp SMBIOD_EV_TREECONNECT | SMBIOD_EV_SYNC, ssp); 19475374Sbp if (error) 19575374Sbp return error; 19675374Sbp } 19775374Sbp error = smb_iod_addrq(rqp); 19875374Sbp if (error != EXDEV) 19975374Sbp break; 20075374Sbp } 20175374Sbp return error; 20275374Sbp} 20375374Sbp 20475374Sbpvoid 20575374Sbpsmb_rq_wstart(struct smb_rq *rqp) 20675374Sbp{ 20775374Sbp rqp->sr_wcount = mb_reserve(&rqp->sr_rq, sizeof(u_int8_t)); 20875374Sbp rqp->sr_rq.mb_count = 0; 20975374Sbp} 21075374Sbp 21175374Sbpvoid 21275374Sbpsmb_rq_wend(struct smb_rq *rqp) 21375374Sbp{ 21475374Sbp if (rqp->sr_wcount == NULL) { 21575374Sbp SMBERROR("no wcount\n"); /* actually panic */ 21675374Sbp return; 21775374Sbp } 21875374Sbp if (rqp->sr_rq.mb_count & 1) 21975374Sbp SMBERROR("odd word count\n"); 22075374Sbp *rqp->sr_wcount = rqp->sr_rq.mb_count / 2; 22175374Sbp} 22275374Sbp 22375374Sbpvoid 22475374Sbpsmb_rq_bstart(struct smb_rq *rqp) 22575374Sbp{ 22675374Sbp rqp->sr_bcount = (u_short*)mb_reserve(&rqp->sr_rq, sizeof(u_short)); 22775374Sbp rqp->sr_rq.mb_count = 0; 22875374Sbp} 22975374Sbp 23075374Sbpvoid 23175374Sbpsmb_rq_bend(struct smb_rq *rqp) 23275374Sbp{ 23375374Sbp int bcnt; 23475374Sbp 23575374Sbp if (rqp->sr_bcount == NULL) { 23675374Sbp SMBERROR("no bcount\n"); /* actually panic */ 23775374Sbp return; 23875374Sbp } 23975374Sbp bcnt = rqp->sr_rq.mb_count; 24075374Sbp if (bcnt > 0xffff) 24175374Sbp SMBERROR("byte count too large (%d)\n", bcnt); 24282037Sbp *rqp->sr_bcount = htoles(bcnt); 24375374Sbp} 24475374Sbp 24575374Sbpint 24675374Sbpsmb_rq_intr(struct smb_rq *rqp) 24775374Sbp{ 24887192Sbp struct proc *p = rqp->sr_cred->scr_td->td_proc; 24975374Sbp 25075374Sbp if (rqp->sr_flags & SMBR_INTR) 25175374Sbp return EINTR; 25275374Sbp return smb_proc_intr(p); 25375374Sbp} 25475374Sbp 25575374Sbpint 25675374Sbpsmb_rq_getrequest(struct smb_rq *rqp, struct mbchain **mbpp) 25775374Sbp{ 25875374Sbp *mbpp = &rqp->sr_rq; 25975374Sbp return 0; 26075374Sbp} 26175374Sbp 26275374Sbpint 26375374Sbpsmb_rq_getreply(struct smb_rq *rqp, struct mdchain **mbpp) 26475374Sbp{ 26575374Sbp *mbpp = &rqp->sr_rp; 26675374Sbp return 0; 26775374Sbp} 26875374Sbp 26975374Sbpstatic int 27075374Sbpsmb_rq_getenv(struct smb_connobj *layer, 27175374Sbp struct smb_vc **vcpp, struct smb_share **sspp) 27275374Sbp{ 27375374Sbp struct smb_vc *vcp = NULL; 27475374Sbp struct smb_share *ssp = NULL; 27575374Sbp struct smb_connobj *cp; 27675374Sbp int error = 0; 27775374Sbp 27875374Sbp switch (layer->co_level) { 27975374Sbp case SMBL_VC: 28075374Sbp vcp = CPTOVC(layer); 28175374Sbp if (layer->co_parent == NULL) { 28275374Sbp SMBERROR("zombie VC %s\n", vcp->vc_srvname); 28375374Sbp error = EINVAL; 28475374Sbp break; 28575374Sbp } 28675374Sbp break; 28775374Sbp case SMBL_SHARE: 28875374Sbp ssp = CPTOSS(layer); 28975374Sbp cp = layer->co_parent; 29075374Sbp if (cp == NULL) { 29175374Sbp SMBERROR("zombie share %s\n", ssp->ss_name); 29275374Sbp error = EINVAL; 29375374Sbp break; 29475374Sbp } 29575374Sbp error = smb_rq_getenv(cp, &vcp, NULL); 29675374Sbp if (error) 29775374Sbp break; 29875374Sbp break; 29975374Sbp default: 30075374Sbp SMBERROR("invalid layer %d passed\n", layer->co_level); 30175374Sbp error = EINVAL; 30275374Sbp } 30375374Sbp if (vcpp) 30475374Sbp *vcpp = vcp; 30575374Sbp if (sspp) 30675374Sbp *sspp = ssp; 30775374Sbp return error; 30875374Sbp} 30975374Sbp 31075374Sbp/* 31175374Sbp * Wait for reply on the request 31275374Sbp */ 31375374Sbpstatic int 31475374Sbpsmb_rq_reply(struct smb_rq *rqp) 31575374Sbp{ 31675374Sbp struct mdchain *mdp = &rqp->sr_rp; 31775374Sbp u_int32_t tdw; 31875374Sbp u_int8_t tb; 31975374Sbp int error, rperror = 0; 32075374Sbp 32175374Sbp error = smb_iod_waitrq(rqp); 32275374Sbp if (error) 32375374Sbp return error; 32475374Sbp error = md_get_uint32(mdp, &tdw); 32575374Sbp if (error) 32675374Sbp return error; 32775374Sbp error = md_get_uint8(mdp, &tb); 32875374Sbp if (rqp->sr_vc->vc_hflags2 & SMB_FLAGS2_ERR_STATUS) { 32975374Sbp error = md_get_uint32le(mdp, &rqp->sr_error); 33075374Sbp } else { 33175374Sbp error = md_get_uint8(mdp, &rqp->sr_errclass); 33275374Sbp error = md_get_uint8(mdp, &tb); 33375374Sbp error = md_get_uint16le(mdp, &rqp->sr_serror); 33475374Sbp if (!error) 33575374Sbp rperror = smb_maperror(rqp->sr_errclass, rqp->sr_serror); 33675374Sbp } 33775374Sbp error = md_get_uint8(mdp, &rqp->sr_rpflags); 33875374Sbp error = md_get_uint16le(mdp, &rqp->sr_rpflags2); 33975374Sbp 34075374Sbp error = md_get_uint32(mdp, &tdw); 34175374Sbp error = md_get_uint32(mdp, &tdw); 34275374Sbp error = md_get_uint32(mdp, &tdw); 34375374Sbp 34475374Sbp error = md_get_uint16le(mdp, &rqp->sr_rptid); 34575374Sbp error = md_get_uint16le(mdp, &rqp->sr_rppid); 34675374Sbp error = md_get_uint16le(mdp, &rqp->sr_rpuid); 34775374Sbp error = md_get_uint16le(mdp, &rqp->sr_rpmid); 34875374Sbp 34975374Sbp SMBSDEBUG("M:%04x, P:%04x, U:%04x, T:%04x, E: %d:%d\n", 35075374Sbp rqp->sr_rpmid, rqp->sr_rppid, rqp->sr_rpuid, rqp->sr_rptid, 35175374Sbp rqp->sr_errclass, rqp->sr_serror); 35275374Sbp return error ? error : rperror; 35375374Sbp} 35475374Sbp 35575374Sbp 35675374Sbp#define ALIGN4(a) (((a) + 3) & ~3) 35775374Sbp 35875374Sbp/* 35975374Sbp * TRANS2 request implementation 36075374Sbp */ 36175374Sbpint 36275374Sbpsmb_t2_alloc(struct smb_connobj *layer, u_short setup, struct smb_cred *scred, 36375374Sbp struct smb_t2rq **t2pp) 36475374Sbp{ 36575374Sbp struct smb_t2rq *t2p; 36675374Sbp int error; 36775374Sbp 36875374Sbp MALLOC(t2p, struct smb_t2rq *, sizeof(*t2p), M_SMBRQ, M_WAITOK); 36975374Sbp if (t2p == NULL) 37075374Sbp return ENOMEM; 37175374Sbp error = smb_t2_init(t2p, layer, setup, scred); 37275374Sbp t2p->t2_flags |= SMBT2_ALLOCED; 37375374Sbp if (error) { 37475374Sbp smb_t2_done(t2p); 37575374Sbp return error; 37675374Sbp } 37775374Sbp *t2pp = t2p; 37875374Sbp return 0; 37975374Sbp} 38075374Sbp 38175374Sbpint 38275374Sbpsmb_t2_init(struct smb_t2rq *t2p, struct smb_connobj *source, u_short setup, 38375374Sbp struct smb_cred *scred) 38475374Sbp{ 38575374Sbp int error; 38675374Sbp 38775374Sbp bzero(t2p, sizeof(*t2p)); 38875374Sbp t2p->t2_source = source; 38975374Sbp t2p->t2_setupcount = 1; 39075374Sbp t2p->t2_setupdata = t2p->t2_setup; 39175374Sbp t2p->t2_setup[0] = setup; 39275374Sbp t2p->t2_fid = 0xffff; 39375374Sbp t2p->t2_cred = scred; 39475374Sbp error = smb_rq_getenv(source, &t2p->t2_vc, NULL); 39575374Sbp if (error) 39675374Sbp return error; 39775374Sbp return 0; 39875374Sbp} 39975374Sbp 40075374Sbpvoid 40175374Sbpsmb_t2_done(struct smb_t2rq *t2p) 40275374Sbp{ 40375374Sbp mb_done(&t2p->t2_tparam); 40475374Sbp mb_done(&t2p->t2_tdata); 40575374Sbp md_done(&t2p->t2_rparam); 40675374Sbp md_done(&t2p->t2_rdata); 40775374Sbp if (t2p->t2_flags & SMBT2_ALLOCED) 40875374Sbp free(t2p, M_SMBRQ); 40975374Sbp} 41075374Sbp 41175374Sbpstatic int 41275374Sbpsmb_t2_placedata(struct mbuf *mtop, u_int16_t offset, u_int16_t count, 41375374Sbp struct mdchain *mdp) 41475374Sbp{ 41575374Sbp struct mbuf *m, *m0; 41675374Sbp int len; 41775374Sbp 41887565Sarr m0 = m_split(mtop, offset, M_TRYWAIT); 41975374Sbp if (m0 == NULL) 42075374Sbp return EBADRPC; 42175374Sbp for(len = 0, m = m0; m->m_next; m = m->m_next) 42275374Sbp len += m->m_len; 42375374Sbp len += m->m_len; 42475374Sbp m->m_len -= len - count; 42575374Sbp if (mdp->md_top == NULL) { 42675374Sbp md_initm(mdp, m0); 42775374Sbp } else 42875374Sbp m_cat(mdp->md_top, m0); 42975374Sbp return 0; 43075374Sbp} 43175374Sbp 43275374Sbpstatic int 43375374Sbpsmb_t2_reply(struct smb_t2rq *t2p) 43475374Sbp{ 43575374Sbp struct mdchain *mdp; 43675374Sbp struct smb_rq *rqp = t2p->t2_rq; 43775374Sbp int error, totpgot, totdgot; 43875374Sbp u_int16_t totpcount, totdcount, pcount, poff, doff, pdisp, ddisp; 43975374Sbp u_int16_t tmp, bc, dcount; 44075374Sbp u_int8_t wc; 44175374Sbp 44275374Sbp error = smb_rq_reply(rqp); 44375374Sbp if (error) 44475374Sbp return error; 44575374Sbp if ((t2p->t2_flags & SMBT2_ALLSENT) == 0) { 44675374Sbp /* 44775374Sbp * this is an interim response, ignore it. 44875374Sbp */ 44975374Sbp SMBRQ_SLOCK(rqp); 45075374Sbp md_next_record(&rqp->sr_rp); 45175374Sbp SMBRQ_SUNLOCK(rqp); 45275374Sbp return 0; 45375374Sbp } 45475374Sbp /* 45588741Sbp * Now we have to get all subsequent responses. The CIFS specification 45688741Sbp * says that they can be disordered which is weird. 45775374Sbp * TODO: timo 45875374Sbp */ 45975374Sbp totpgot = totdgot = 0; 46075374Sbp totpcount = totdcount = 0xffff; 46175374Sbp mdp = &rqp->sr_rp; 46275374Sbp for (;;) { 46375374Sbp m_dumpm(mdp->md_top); 46475374Sbp if ((error = md_get_uint8(mdp, &wc)) != 0) 46575374Sbp break; 46675374Sbp if (wc < 10) { 46775374Sbp error = ENOENT; 46875374Sbp break; 46975374Sbp } 47075374Sbp if ((error = md_get_uint16le(mdp, &tmp)) != 0) 47175374Sbp break; 47275374Sbp if (totpcount > tmp) 47375374Sbp totpcount = tmp; 47475374Sbp md_get_uint16le(mdp, &tmp); 47575374Sbp if (totdcount > tmp) 47675374Sbp totdcount = tmp; 47775374Sbp if ((error = md_get_uint16le(mdp, &tmp)) != 0 || /* reserved */ 47875374Sbp (error = md_get_uint16le(mdp, &pcount)) != 0 || 47975374Sbp (error = md_get_uint16le(mdp, &poff)) != 0 || 48075374Sbp (error = md_get_uint16le(mdp, &pdisp)) != 0) 48175374Sbp break; 48275374Sbp if (pcount != 0 && pdisp != totpgot) { 48388741Sbp SMBERROR("Can't handle disordered parameters %d:%d\n", 48475374Sbp pdisp, totpgot); 48575374Sbp error = EINVAL; 48675374Sbp break; 48775374Sbp } 48875374Sbp if ((error = md_get_uint16le(mdp, &dcount)) != 0 || 48975374Sbp (error = md_get_uint16le(mdp, &doff)) != 0 || 49075374Sbp (error = md_get_uint16le(mdp, &ddisp)) != 0) 49175374Sbp break; 49275374Sbp if (dcount != 0 && ddisp != totdgot) { 49388741Sbp SMBERROR("Can't handle disordered data\n"); 49475374Sbp error = EINVAL; 49575374Sbp break; 49675374Sbp } 49775374Sbp md_get_uint8(mdp, &wc); 49875374Sbp md_get_uint8(mdp, NULL); 49975374Sbp tmp = wc; 50075374Sbp while (tmp--) 50175374Sbp md_get_uint16(mdp, NULL); 50275374Sbp if ((error = md_get_uint16le(mdp, &bc)) != 0) 50375374Sbp break; 50475374Sbp/* tmp = SMB_HDRLEN + 1 + 10 * 2 + 2 * wc + 2;*/ 50575374Sbp if (dcount) { 50675374Sbp error = smb_t2_placedata(mdp->md_top, doff, dcount, 50775374Sbp &t2p->t2_rdata); 50875374Sbp if (error) 50975374Sbp break; 51075374Sbp } 51175374Sbp if (pcount) { 51275374Sbp error = smb_t2_placedata(mdp->md_top, poff, pcount, 51375374Sbp &t2p->t2_rparam); 51475374Sbp if (error) 51575374Sbp break; 51675374Sbp } 51775374Sbp totpgot += pcount; 51875374Sbp totdgot += dcount; 51975374Sbp if (totpgot >= totpcount && totdgot >= totdcount) { 52075374Sbp error = 0; 52175374Sbp t2p->t2_flags |= SMBT2_ALLRECV; 52275374Sbp break; 52375374Sbp } 52475374Sbp /* 52575374Sbp * We're done with this reply, look for the next one. 52675374Sbp */ 52775374Sbp SMBRQ_SLOCK(rqp); 52875374Sbp md_next_record(&rqp->sr_rp); 52975374Sbp SMBRQ_SUNLOCK(rqp); 53075374Sbp error = smb_rq_reply(rqp); 53175374Sbp if (error) 53275374Sbp break; 53375374Sbp } 53475374Sbp return error; 53575374Sbp} 53675374Sbp 53775374Sbp/* 53875374Sbp * Perform a full round of TRANS2 request 53975374Sbp */ 54075374Sbpstatic int 54175374Sbpsmb_t2_request_int(struct smb_t2rq *t2p) 54275374Sbp{ 54375374Sbp struct smb_vc *vcp = t2p->t2_vc; 54475374Sbp struct smb_cred *scred = t2p->t2_cred; 54575374Sbp struct mbchain *mbp; 54675374Sbp struct mdchain *mdp, mbparam, mbdata; 54775374Sbp struct mbuf *m; 54875374Sbp struct smb_rq *rqp; 54975374Sbp int totpcount, leftpcount, totdcount, leftdcount, len, txmax, i; 55075374Sbp int error, doff, poff, txdcount, txpcount, nmlen; 55175374Sbp 55275374Sbp m = t2p->t2_tparam.mb_top; 55375374Sbp if (m) { 55475374Sbp md_initm(&mbparam, m); /* do not free it! */ 55575374Sbp totpcount = m_fixhdr(m); 55675374Sbp if (totpcount > 0xffff) /* maxvalue for u_short */ 55775374Sbp return EINVAL; 55875374Sbp } else 55975374Sbp totpcount = 0; 56075374Sbp m = t2p->t2_tdata.mb_top; 56175374Sbp if (m) { 56275374Sbp md_initm(&mbdata, m); /* do not free it! */ 56375374Sbp totdcount = m_fixhdr(m); 56475374Sbp if (totdcount > 0xffff) 56575374Sbp return EINVAL; 56675374Sbp } else 56775374Sbp totdcount = 0; 56875374Sbp leftdcount = totdcount; 56975374Sbp leftpcount = totpcount; 57075374Sbp txmax = vcp->vc_txmax; 57175374Sbp error = smb_rq_alloc(t2p->t2_source, t2p->t_name ? 57275374Sbp SMB_COM_TRANSACTION : SMB_COM_TRANSACTION2, scred, &rqp); 57375374Sbp if (error) 57475374Sbp return error; 57575374Sbp rqp->sr_flags |= SMBR_MULTIPACKET; 57675374Sbp t2p->t2_rq = rqp; 57775374Sbp mbp = &rqp->sr_rq; 57875374Sbp smb_rq_wstart(rqp); 57975374Sbp mb_put_uint16le(mbp, totpcount); 58075374Sbp mb_put_uint16le(mbp, totdcount); 58175374Sbp mb_put_uint16le(mbp, t2p->t2_maxpcount); 58275374Sbp mb_put_uint16le(mbp, t2p->t2_maxdcount); 58375374Sbp mb_put_uint8(mbp, t2p->t2_maxscount); 58475374Sbp mb_put_uint8(mbp, 0); /* reserved */ 58575374Sbp mb_put_uint16le(mbp, 0); /* flags */ 58675374Sbp mb_put_uint32le(mbp, 0); /* Timeout */ 58775374Sbp mb_put_uint16le(mbp, 0); /* reserved 2 */ 58875374Sbp len = mb_fixhdr(mbp); 58975374Sbp /* 59075374Sbp * now we have known packet size as 59175374Sbp * ALIGN4(len + 5 * 2 + setupcount * 2 + 2 + strlen(name) + 1), 59275374Sbp * and need to decide which parts should go into the first request 59375374Sbp */ 59475374Sbp nmlen = t2p->t_name ? strlen(t2p->t_name) : 0; 59575374Sbp len = ALIGN4(len + 5 * 2 + t2p->t2_setupcount * 2 + 2 + nmlen + 1); 59675374Sbp if (len + leftpcount > txmax) { 59775374Sbp txpcount = min(leftpcount, txmax - len); 59875374Sbp poff = len; 59975374Sbp txdcount = 0; 60075374Sbp doff = 0; 60175374Sbp } else { 60275374Sbp txpcount = leftpcount; 60375374Sbp poff = txpcount ? len : 0; 60475374Sbp len = ALIGN4(len + txpcount); 60575374Sbp txdcount = min(leftdcount, txmax - len); 60675374Sbp doff = txdcount ? len : 0; 60775374Sbp } 60875374Sbp leftpcount -= txpcount; 60975374Sbp leftdcount -= txdcount; 61075374Sbp mb_put_uint16le(mbp, txpcount); 61175374Sbp mb_put_uint16le(mbp, poff); 61275374Sbp mb_put_uint16le(mbp, txdcount); 61375374Sbp mb_put_uint16le(mbp, doff); 61475374Sbp mb_put_uint8(mbp, t2p->t2_setupcount); 61575374Sbp mb_put_uint8(mbp, 0); 61675374Sbp for (i = 0; i < t2p->t2_setupcount; i++) 61775374Sbp mb_put_uint16le(mbp, t2p->t2_setupdata[i]); 61875374Sbp smb_rq_wend(rqp); 61975374Sbp smb_rq_bstart(rqp); 62075374Sbp /* TDUNICODE */ 62175374Sbp if (t2p->t_name) 62275374Sbp mb_put_mem(mbp, t2p->t_name, nmlen, MB_MSYSTEM); 62375374Sbp mb_put_uint8(mbp, 0); /* terminating zero */ 62475374Sbp len = mb_fixhdr(mbp); 62575374Sbp if (txpcount) { 62675374Sbp mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO); 62775374Sbp error = md_get_mbuf(&mbparam, txpcount, &m); 62875374Sbp SMBSDEBUG("%d:%d:%d\n", error, txpcount, txmax); 62975374Sbp if (error) 63075374Sbp goto freerq; 63175374Sbp mb_put_mbuf(mbp, m); 63275374Sbp } 63375374Sbp len = mb_fixhdr(mbp); 63475374Sbp if (txdcount) { 63575374Sbp mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO); 63675374Sbp error = md_get_mbuf(&mbdata, txdcount, &m); 63775374Sbp if (error) 63875374Sbp goto freerq; 63975374Sbp mb_put_mbuf(mbp, m); 64075374Sbp } 64175374Sbp smb_rq_bend(rqp); /* incredible, but thats it... */ 64275374Sbp error = smb_rq_enqueue(rqp); 64375374Sbp if (error) 64475374Sbp goto freerq; 64575374Sbp if (leftpcount == 0 && leftdcount == 0) 64675374Sbp t2p->t2_flags |= SMBT2_ALLSENT; 64775374Sbp error = smb_t2_reply(t2p); 64875374Sbp if (error) 64975374Sbp goto bad; 65075374Sbp while (leftpcount || leftdcount) { 65175374Sbp error = smb_rq_new(rqp, t2p->t_name ? 65275374Sbp SMB_COM_TRANSACTION_SECONDARY : SMB_COM_TRANSACTION2_SECONDARY); 65375374Sbp if (error) 65475374Sbp goto bad; 65575374Sbp mbp = &rqp->sr_rq; 65675374Sbp smb_rq_wstart(rqp); 65775374Sbp mb_put_uint16le(mbp, totpcount); 65875374Sbp mb_put_uint16le(mbp, totdcount); 65975374Sbp len = mb_fixhdr(mbp); 66075374Sbp /* 66175374Sbp * now we have known packet size as 66275374Sbp * ALIGN4(len + 7 * 2 + 2) for T2 request, and -2 for T one, 66375374Sbp * and need to decide which parts should go into request 66475374Sbp */ 66575374Sbp len = ALIGN4(len + 6 * 2 + 2); 66675374Sbp if (t2p->t_name == NULL) 66775374Sbp len += 2; 66875374Sbp if (len + leftpcount > txmax) { 66975374Sbp txpcount = min(leftpcount, txmax - len); 67075374Sbp poff = len; 67175374Sbp txdcount = 0; 67275374Sbp doff = 0; 67375374Sbp } else { 67475374Sbp txpcount = leftpcount; 67575374Sbp poff = txpcount ? len : 0; 67675374Sbp len = ALIGN4(len + txpcount); 67775374Sbp txdcount = min(leftdcount, txmax - len); 67875374Sbp doff = txdcount ? len : 0; 67975374Sbp } 68075374Sbp mb_put_uint16le(mbp, txpcount); 68175374Sbp mb_put_uint16le(mbp, poff); 68275374Sbp mb_put_uint16le(mbp, totpcount - leftpcount); 68375374Sbp mb_put_uint16le(mbp, txdcount); 68475374Sbp mb_put_uint16le(mbp, doff); 68575374Sbp mb_put_uint16le(mbp, totdcount - leftdcount); 68675374Sbp leftpcount -= txpcount; 68775374Sbp leftdcount -= txdcount; 68875374Sbp if (t2p->t_name == NULL) 68975374Sbp mb_put_uint16le(mbp, t2p->t2_fid); 69075374Sbp smb_rq_wend(rqp); 69175374Sbp smb_rq_bstart(rqp); 69275374Sbp mb_put_uint8(mbp, 0); /* name */ 69375374Sbp len = mb_fixhdr(mbp); 69475374Sbp if (txpcount) { 69575374Sbp mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO); 69675374Sbp error = md_get_mbuf(&mbparam, txpcount, &m); 69775374Sbp if (error) 69875374Sbp goto bad; 69975374Sbp mb_put_mbuf(mbp, m); 70075374Sbp } 70175374Sbp len = mb_fixhdr(mbp); 70275374Sbp if (txdcount) { 70375374Sbp mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO); 70475374Sbp error = md_get_mbuf(&mbdata, txdcount, &m); 70575374Sbp if (error) 70675374Sbp goto bad; 70775374Sbp mb_put_mbuf(mbp, m); 70875374Sbp } 70975374Sbp smb_rq_bend(rqp); 71075374Sbp rqp->sr_state = SMBRQ_NOTSENT; 71175374Sbp error = smb_iod_request(vcp->vc_iod, SMBIOD_EV_NEWRQ, NULL); 71275374Sbp if (error) 71375374Sbp goto bad; 71475374Sbp } /* while left params or data */ 71575374Sbp t2p->t2_flags |= SMBT2_ALLSENT; 71675374Sbp mdp = &t2p->t2_rdata; 71775374Sbp if (mdp->md_top) { 71875374Sbp m_fixhdr(mdp->md_top); 71975374Sbp md_initm(mdp, mdp->md_top); 72075374Sbp } 72175374Sbp mdp = &t2p->t2_rparam; 72275374Sbp if (mdp->md_top) { 72375374Sbp m_fixhdr(mdp->md_top); 72475374Sbp md_initm(mdp, mdp->md_top); 72575374Sbp } 72675374Sbpbad: 72775374Sbp smb_iod_removerq(rqp); 72875374Sbpfreerq: 72975374Sbp smb_rq_done(rqp); 73075374Sbp if (error) { 73175374Sbp if (rqp->sr_flags & SMBR_RESTART) 73275374Sbp t2p->t2_flags |= SMBT2_RESTART; 73375374Sbp md_done(&t2p->t2_rparam); 73475374Sbp md_done(&t2p->t2_rdata); 73575374Sbp } 73675374Sbp return error; 73775374Sbp} 73875374Sbp 73975374Sbpint 74075374Sbpsmb_t2_request(struct smb_t2rq *t2p) 74175374Sbp{ 74275374Sbp int error = EINVAL, i; 74375374Sbp 74475374Sbp for (i = 0; i < SMB_MAXRCN; i++) { 74575374Sbp t2p->t2_flags &= ~SMBR_RESTART; 74675374Sbp error = smb_t2_request_int(t2p); 74775374Sbp if (error == 0) 74875374Sbp break; 74975374Sbp if ((t2p->t2_flags & (SMBT2_RESTART | SMBT2_NORESTART)) != SMBT2_RESTART) 75075374Sbp break; 75175374Sbp } 75275374Sbp return error; 75375374Sbp} 754