xref: /freebsd/sys/netsmb/smb_rq.c (revision fe267a559009cbf34f9341666fe4d88a92c02d5e)
1c398230bSWarner Losh /*-
2*fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*fe267a55SPedro F. Giffuni  *
4d122d784SJoel Dahl  * Copyright (c) 2000-2001 Boris Popov
5681a5bbeSBoris Popov  * All rights reserved.
6681a5bbeSBoris Popov  *
7681a5bbeSBoris Popov  * Redistribution and use in source and binary forms, with or without
8681a5bbeSBoris Popov  * modification, are permitted provided that the following conditions
9681a5bbeSBoris Popov  * are met:
10681a5bbeSBoris Popov  * 1. Redistributions of source code must retain the above copyright
11681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer.
12681a5bbeSBoris Popov  * 2. Redistributions in binary form must reproduce the above copyright
13681a5bbeSBoris Popov  *    notice, this list of conditions and the following disclaimer in the
14681a5bbeSBoris Popov  *    documentation and/or other materials provided with the distribution.
15681a5bbeSBoris Popov  *
16681a5bbeSBoris Popov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17681a5bbeSBoris Popov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18681a5bbeSBoris Popov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19681a5bbeSBoris Popov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20681a5bbeSBoris Popov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21681a5bbeSBoris Popov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22681a5bbeSBoris Popov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23681a5bbeSBoris Popov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24681a5bbeSBoris Popov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25681a5bbeSBoris Popov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26681a5bbeSBoris Popov  * SUCH DAMAGE.
27681a5bbeSBoris Popov  */
28ab0de15bSDavid E. O'Brien 
29ab0de15bSDavid E. O'Brien #include <sys/cdefs.h>
30ab0de15bSDavid E. O'Brien __FBSDID("$FreeBSD$");
31ab0de15bSDavid E. O'Brien 
32681a5bbeSBoris Popov #include <sys/param.h>
33681a5bbeSBoris Popov #include <sys/systm.h>
34a30d4b32SMike Barcroft #include <sys/endian.h>
35681a5bbeSBoris Popov #include <sys/kernel.h>
36681a5bbeSBoris Popov #include <sys/malloc.h>
375dba30f1SPoul-Henning Kamp #include <sys/module.h>
38681a5bbeSBoris Popov #include <sys/proc.h>
39681a5bbeSBoris Popov #include <sys/lock.h>
40681a5bbeSBoris Popov #include <sys/sysctl.h>
41681a5bbeSBoris Popov #include <sys/socket.h>
42681a5bbeSBoris Popov #include <sys/socketvar.h>
43681a5bbeSBoris Popov #include <sys/mbuf.h>
44681a5bbeSBoris Popov 
45681a5bbeSBoris Popov #include <netsmb/smb.h>
46681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
47681a5bbeSBoris Popov #include <netsmb/smb_rq.h>
48681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
49681a5bbeSBoris Popov #include <netsmb/smb_tran.h>
50681a5bbeSBoris Popov 
51d745c852SEd Schouten static MALLOC_DEFINE(M_SMBRQ, "SMBRQ", "SMB request");
52681a5bbeSBoris Popov 
53681a5bbeSBoris Popov MODULE_DEPEND(netsmb, libmchain, 1, 1, 1);
54681a5bbeSBoris Popov 
55681a5bbeSBoris Popov static int  smb_rq_reply(struct smb_rq *rqp);
56681a5bbeSBoris Popov static int  smb_rq_enqueue(struct smb_rq *rqp);
57681a5bbeSBoris Popov static int  smb_rq_getenv(struct smb_connobj *layer,
58681a5bbeSBoris Popov 		struct smb_vc **vcpp, struct smb_share **sspp);
59681a5bbeSBoris Popov static int  smb_rq_new(struct smb_rq *rqp, u_char cmd);
60681a5bbeSBoris Popov static int  smb_t2_reply(struct smb_t2rq *t2p);
61681a5bbeSBoris Popov 
62681a5bbeSBoris Popov int
63681a5bbeSBoris Popov smb_rq_alloc(struct smb_connobj *layer, u_char cmd, struct smb_cred *scred,
64681a5bbeSBoris Popov 	struct smb_rq **rqpp)
65681a5bbeSBoris Popov {
66681a5bbeSBoris Popov 	struct smb_rq *rqp;
67681a5bbeSBoris Popov 	int error;
68681a5bbeSBoris Popov 
691ede983cSDag-Erling Smørgrav 	rqp = malloc(sizeof(*rqp), M_SMBRQ, M_WAITOK);
70681a5bbeSBoris Popov 	if (rqp == NULL)
71681a5bbeSBoris Popov 		return ENOMEM;
72681a5bbeSBoris Popov 	error = smb_rq_init(rqp, layer, cmd, scred);
73681a5bbeSBoris Popov 	rqp->sr_flags |= SMBR_ALLOCED;
74681a5bbeSBoris Popov 	if (error) {
75681a5bbeSBoris Popov 		smb_rq_done(rqp);
76681a5bbeSBoris Popov 		return error;
77681a5bbeSBoris Popov 	}
78681a5bbeSBoris Popov 	*rqpp = rqp;
79681a5bbeSBoris Popov 	return 0;
80681a5bbeSBoris Popov }
81681a5bbeSBoris Popov 
82681a5bbeSBoris Popov static char tzero[12];
83681a5bbeSBoris Popov 
84681a5bbeSBoris Popov int
85681a5bbeSBoris Popov smb_rq_init(struct smb_rq *rqp, struct smb_connobj *layer, u_char cmd,
86681a5bbeSBoris Popov 	struct smb_cred *scred)
87681a5bbeSBoris Popov {
88681a5bbeSBoris Popov 	int error;
89681a5bbeSBoris Popov 
90681a5bbeSBoris Popov 	bzero(rqp, sizeof(*rqp));
91681a5bbeSBoris Popov 	smb_sl_init(&rqp->sr_slock, "srslock");
92681a5bbeSBoris Popov 	error = smb_rq_getenv(layer, &rqp->sr_vc, &rqp->sr_share);
93681a5bbeSBoris Popov 	if (error)
94681a5bbeSBoris Popov 		return error;
95681a5bbeSBoris Popov 	error = smb_vc_access(rqp->sr_vc, scred, SMBM_EXEC);
96681a5bbeSBoris Popov 	if (error)
97681a5bbeSBoris Popov 		return error;
98681a5bbeSBoris Popov 	if (rqp->sr_share) {
99681a5bbeSBoris Popov 		error = smb_share_access(rqp->sr_share, scred, SMBM_EXEC);
100681a5bbeSBoris Popov 		if (error)
101681a5bbeSBoris Popov 			return error;
102681a5bbeSBoris Popov 	}
103681a5bbeSBoris Popov 	rqp->sr_cred = scred;
104681a5bbeSBoris Popov 	rqp->sr_mid = smb_vc_nextmid(rqp->sr_vc);
105681a5bbeSBoris Popov 	return smb_rq_new(rqp, cmd);
106681a5bbeSBoris Popov }
107681a5bbeSBoris Popov 
108681a5bbeSBoris Popov static int
109681a5bbeSBoris Popov smb_rq_new(struct smb_rq *rqp, u_char cmd)
110681a5bbeSBoris Popov {
111681a5bbeSBoris Popov 	struct smb_vc *vcp = rqp->sr_vc;
112681a5bbeSBoris Popov 	struct mbchain *mbp = &rqp->sr_rq;
113681a5bbeSBoris Popov 	int error;
114190b2c4fSTim J. Robbins 	u_int16_t flags2;
115681a5bbeSBoris Popov 
116681a5bbeSBoris Popov 	rqp->sr_sendcnt = 0;
117681a5bbeSBoris Popov 	mb_done(mbp);
118681a5bbeSBoris Popov 	md_done(&rqp->sr_rp);
119681a5bbeSBoris Popov 	error = mb_init(mbp);
120681a5bbeSBoris Popov 	if (error)
121681a5bbeSBoris Popov 		return error;
122681a5bbeSBoris Popov 	mb_put_mem(mbp, SMB_SIGNATURE, SMB_SIGLEN, MB_MSYSTEM);
123681a5bbeSBoris Popov 	mb_put_uint8(mbp, cmd);
124681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);		/* DosError */
125681a5bbeSBoris Popov 	mb_put_uint8(mbp, vcp->vc_hflags);
126190b2c4fSTim J. Robbins 	flags2 = vcp->vc_hflags2;
127ab93c874SBoris Popov 	if (cmd == SMB_COM_TRANSACTION || cmd == SMB_COM_TRANSACTION_SECONDARY)
128190b2c4fSTim J. Robbins 		flags2 &= ~SMB_FLAGS2_UNICODE;
129190b2c4fSTim J. Robbins 	if (cmd == SMB_COM_NEGOTIATE)
130190b2c4fSTim J. Robbins 		flags2 &= ~SMB_FLAGS2_SECURITY_SIGNATURE;
131190b2c4fSTim J. Robbins 	mb_put_uint16le(mbp, flags2);
132190b2c4fSTim J. Robbins 	if ((flags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0) {
133681a5bbeSBoris Popov 		mb_put_mem(mbp, tzero, 12, MB_MSYSTEM);
134190b2c4fSTim J. Robbins 		rqp->sr_rqsig = NULL;
135190b2c4fSTim J. Robbins 	} else {
136190b2c4fSTim J. Robbins 		mb_put_uint16le(mbp, 0 /*scred->sc_p->p_pid >> 16*/);
137190b2c4fSTim J. Robbins 		rqp->sr_rqsig = (u_int8_t *)mb_reserve(mbp, 8);
138190b2c4fSTim J. Robbins 		mb_put_uint16le(mbp, 0);
139190b2c4fSTim J. Robbins 	}
140a6a4232fSMarcel Moolenaar 	rqp->sr_rqtid = mb_reserve(mbp, sizeof(u_int16_t));
141681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 1 /*scred->sc_p->p_pid & 0xffff*/);
142a6a4232fSMarcel Moolenaar 	rqp->sr_rquid = mb_reserve(mbp, sizeof(u_int16_t));
143681a5bbeSBoris Popov 	mb_put_uint16le(mbp, rqp->sr_mid);
144681a5bbeSBoris Popov 	return 0;
145681a5bbeSBoris Popov }
146681a5bbeSBoris Popov 
147681a5bbeSBoris Popov void
148681a5bbeSBoris Popov smb_rq_done(struct smb_rq *rqp)
149681a5bbeSBoris Popov {
150681a5bbeSBoris Popov 	mb_done(&rqp->sr_rq);
151681a5bbeSBoris Popov 	md_done(&rqp->sr_rp);
152681a5bbeSBoris Popov 	smb_sl_destroy(&rqp->sr_slock);
153681a5bbeSBoris Popov 	if (rqp->sr_flags & SMBR_ALLOCED)
154681a5bbeSBoris Popov 		free(rqp, M_SMBRQ);
155681a5bbeSBoris Popov }
156681a5bbeSBoris Popov 
157681a5bbeSBoris Popov /*
158681a5bbeSBoris Popov  * Simple request-reply exchange
159681a5bbeSBoris Popov  */
160681a5bbeSBoris Popov int
161681a5bbeSBoris Popov smb_rq_simple(struct smb_rq *rqp)
162681a5bbeSBoris Popov {
163681a5bbeSBoris Popov 	struct smb_vc *vcp = rqp->sr_vc;
164681a5bbeSBoris Popov 	int error = EINVAL, i;
165681a5bbeSBoris Popov 
166681a5bbeSBoris Popov 	for (i = 0; i < SMB_MAXRCN; i++) {
167681a5bbeSBoris Popov 		rqp->sr_flags &= ~SMBR_RESTART;
168681a5bbeSBoris Popov 		rqp->sr_timo = vcp->vc_timo;
169681a5bbeSBoris Popov 		rqp->sr_state = SMBRQ_NOTSENT;
170681a5bbeSBoris Popov 		error = smb_rq_enqueue(rqp);
171681a5bbeSBoris Popov 		if (error)
172681a5bbeSBoris Popov 			return error;
173681a5bbeSBoris Popov 		error = smb_rq_reply(rqp);
174681a5bbeSBoris Popov 		if (error == 0)
175681a5bbeSBoris Popov 			break;
176681a5bbeSBoris Popov 		if ((rqp->sr_flags & (SMBR_RESTART | SMBR_NORESTART)) != SMBR_RESTART)
177681a5bbeSBoris Popov 			break;
178681a5bbeSBoris Popov 	}
179681a5bbeSBoris Popov 	return error;
180681a5bbeSBoris Popov }
181681a5bbeSBoris Popov 
182681a5bbeSBoris Popov static int
183681a5bbeSBoris Popov smb_rq_enqueue(struct smb_rq *rqp)
184681a5bbeSBoris Popov {
185681a5bbeSBoris Popov 	struct smb_share *ssp = rqp->sr_share;
186681a5bbeSBoris Popov 	int error;
187681a5bbeSBoris Popov 
188681a5bbeSBoris Popov 	if (ssp == NULL || rqp->sr_cred == &rqp->sr_vc->vc_iod->iod_scred) {
189681a5bbeSBoris Popov 		return smb_iod_addrq(rqp);
190681a5bbeSBoris Popov 	}
191681a5bbeSBoris Popov 	for (;;) {
192681a5bbeSBoris Popov 		SMBS_ST_LOCK(ssp);
193681a5bbeSBoris Popov 		if (ssp->ss_flags & SMBS_RECONNECTING) {
194681a5bbeSBoris Popov 			msleep(&ssp->ss_vcgenid, SMBS_ST_LOCKPTR(ssp),
195681a5bbeSBoris Popov 			    PWAIT | PDROP, "90trcn", hz);
1964093529dSJeff Roberson 			if (smb_td_intr(rqp->sr_cred->scr_td))
197681a5bbeSBoris Popov 				return EINTR;
198681a5bbeSBoris Popov 			continue;
199681a5bbeSBoris Popov 		}
200681a5bbeSBoris Popov 		if (smb_share_valid(ssp) || (ssp->ss_flags & SMBS_CONNECTED) == 0) {
201681a5bbeSBoris Popov 			SMBS_ST_UNLOCK(ssp);
202681a5bbeSBoris Popov 		} else {
203681a5bbeSBoris Popov 			SMBS_ST_UNLOCK(ssp);
204681a5bbeSBoris Popov 			error = smb_iod_request(rqp->sr_vc->vc_iod,
205681a5bbeSBoris Popov 			    SMBIOD_EV_TREECONNECT | SMBIOD_EV_SYNC, ssp);
206681a5bbeSBoris Popov 			if (error)
207681a5bbeSBoris Popov 				return error;
208681a5bbeSBoris Popov 		}
209681a5bbeSBoris Popov 		error = smb_iod_addrq(rqp);
210681a5bbeSBoris Popov 		if (error != EXDEV)
211681a5bbeSBoris Popov 			break;
212681a5bbeSBoris Popov 	}
213681a5bbeSBoris Popov 	return error;
214681a5bbeSBoris Popov }
215681a5bbeSBoris Popov 
216681a5bbeSBoris Popov void
217681a5bbeSBoris Popov smb_rq_wstart(struct smb_rq *rqp)
218681a5bbeSBoris Popov {
219681a5bbeSBoris Popov 	rqp->sr_wcount = mb_reserve(&rqp->sr_rq, sizeof(u_int8_t));
220681a5bbeSBoris Popov 	rqp->sr_rq.mb_count = 0;
221681a5bbeSBoris Popov }
222681a5bbeSBoris Popov 
223681a5bbeSBoris Popov void
224681a5bbeSBoris Popov smb_rq_wend(struct smb_rq *rqp)
225681a5bbeSBoris Popov {
226681a5bbeSBoris Popov 	if (rqp->sr_wcount == NULL) {
227681a5bbeSBoris Popov 		SMBERROR("no wcount\n");	/* actually panic */
228681a5bbeSBoris Popov 		return;
229681a5bbeSBoris Popov 	}
230681a5bbeSBoris Popov 	if (rqp->sr_rq.mb_count & 1)
231681a5bbeSBoris Popov 		SMBERROR("odd word count\n");
232681a5bbeSBoris Popov 	*rqp->sr_wcount = rqp->sr_rq.mb_count / 2;
233681a5bbeSBoris Popov }
234681a5bbeSBoris Popov 
235681a5bbeSBoris Popov void
236681a5bbeSBoris Popov smb_rq_bstart(struct smb_rq *rqp)
237681a5bbeSBoris Popov {
238a6a4232fSMarcel Moolenaar 	rqp->sr_bcount = mb_reserve(&rqp->sr_rq, sizeof(u_short));
239681a5bbeSBoris Popov 	rqp->sr_rq.mb_count = 0;
240681a5bbeSBoris Popov }
241681a5bbeSBoris Popov 
242681a5bbeSBoris Popov void
243681a5bbeSBoris Popov smb_rq_bend(struct smb_rq *rqp)
244681a5bbeSBoris Popov {
245681a5bbeSBoris Popov 	int bcnt;
246681a5bbeSBoris Popov 
247681a5bbeSBoris Popov 	if (rqp->sr_bcount == NULL) {
248681a5bbeSBoris Popov 		SMBERROR("no bcount\n");	/* actually panic */
249681a5bbeSBoris Popov 		return;
250681a5bbeSBoris Popov 	}
251681a5bbeSBoris Popov 	bcnt = rqp->sr_rq.mb_count;
252681a5bbeSBoris Popov 	if (bcnt > 0xffff)
253681a5bbeSBoris Popov 		SMBERROR("byte count too large (%d)\n", bcnt);
254a6a4232fSMarcel Moolenaar 	le16enc(rqp->sr_bcount, bcnt);
255681a5bbeSBoris Popov }
256681a5bbeSBoris Popov 
257681a5bbeSBoris Popov int
258681a5bbeSBoris Popov smb_rq_intr(struct smb_rq *rqp)
259681a5bbeSBoris Popov {
260681a5bbeSBoris Popov 	if (rqp->sr_flags & SMBR_INTR)
261681a5bbeSBoris Popov 		return EINTR;
2624093529dSJeff Roberson 	return smb_td_intr(rqp->sr_cred->scr_td);
263681a5bbeSBoris Popov }
264681a5bbeSBoris Popov 
265681a5bbeSBoris Popov int
266681a5bbeSBoris Popov smb_rq_getrequest(struct smb_rq *rqp, struct mbchain **mbpp)
267681a5bbeSBoris Popov {
268681a5bbeSBoris Popov 	*mbpp = &rqp->sr_rq;
269681a5bbeSBoris Popov 	return 0;
270681a5bbeSBoris Popov }
271681a5bbeSBoris Popov 
272681a5bbeSBoris Popov int
273681a5bbeSBoris Popov smb_rq_getreply(struct smb_rq *rqp, struct mdchain **mbpp)
274681a5bbeSBoris Popov {
275681a5bbeSBoris Popov 	*mbpp = &rqp->sr_rp;
276681a5bbeSBoris Popov 	return 0;
277681a5bbeSBoris Popov }
278681a5bbeSBoris Popov 
279681a5bbeSBoris Popov static int
280681a5bbeSBoris Popov smb_rq_getenv(struct smb_connobj *layer,
281681a5bbeSBoris Popov 	struct smb_vc **vcpp, struct smb_share **sspp)
282681a5bbeSBoris Popov {
283681a5bbeSBoris Popov 	struct smb_vc *vcp = NULL;
284681a5bbeSBoris Popov 	struct smb_share *ssp = NULL;
285681a5bbeSBoris Popov 	struct smb_connobj *cp;
286681a5bbeSBoris Popov 	int error = 0;
287681a5bbeSBoris Popov 
288681a5bbeSBoris Popov 	switch (layer->co_level) {
289681a5bbeSBoris Popov 	    case SMBL_VC:
290681a5bbeSBoris Popov 		vcp = CPTOVC(layer);
291681a5bbeSBoris Popov 		if (layer->co_parent == NULL) {
292681a5bbeSBoris Popov 			SMBERROR("zombie VC %s\n", vcp->vc_srvname);
293681a5bbeSBoris Popov 			error = EINVAL;
294681a5bbeSBoris Popov 			break;
295681a5bbeSBoris Popov 		}
296681a5bbeSBoris Popov 		break;
297681a5bbeSBoris Popov 	    case SMBL_SHARE:
298681a5bbeSBoris Popov 		ssp = CPTOSS(layer);
299681a5bbeSBoris Popov 		cp = layer->co_parent;
300681a5bbeSBoris Popov 		if (cp == NULL) {
301681a5bbeSBoris Popov 			SMBERROR("zombie share %s\n", ssp->ss_name);
302681a5bbeSBoris Popov 			error = EINVAL;
303681a5bbeSBoris Popov 			break;
304681a5bbeSBoris Popov 		}
305681a5bbeSBoris Popov 		error = smb_rq_getenv(cp, &vcp, NULL);
306681a5bbeSBoris Popov 		if (error)
307681a5bbeSBoris Popov 			break;
308681a5bbeSBoris Popov 		break;
309681a5bbeSBoris Popov 	    default:
310681a5bbeSBoris Popov 		SMBERROR("invalid layer %d passed\n", layer->co_level);
311681a5bbeSBoris Popov 		error = EINVAL;
312681a5bbeSBoris Popov 	}
313681a5bbeSBoris Popov 	if (vcpp)
314681a5bbeSBoris Popov 		*vcpp = vcp;
315681a5bbeSBoris Popov 	if (sspp)
316681a5bbeSBoris Popov 		*sspp = ssp;
317681a5bbeSBoris Popov 	return error;
318681a5bbeSBoris Popov }
319681a5bbeSBoris Popov 
320681a5bbeSBoris Popov /*
321681a5bbeSBoris Popov  * Wait for reply on the request
322681a5bbeSBoris Popov  */
323681a5bbeSBoris Popov static int
324681a5bbeSBoris Popov smb_rq_reply(struct smb_rq *rqp)
325681a5bbeSBoris Popov {
326681a5bbeSBoris Popov 	struct mdchain *mdp = &rqp->sr_rp;
327681a5bbeSBoris Popov 	u_int32_t tdw;
328681a5bbeSBoris Popov 	u_int8_t tb;
329681a5bbeSBoris Popov 	int error, rperror = 0;
330681a5bbeSBoris Popov 
331681a5bbeSBoris Popov 	error = smb_iod_waitrq(rqp);
332681a5bbeSBoris Popov 	if (error)
333681a5bbeSBoris Popov 		return error;
334681a5bbeSBoris Popov 	error = md_get_uint32(mdp, &tdw);
335681a5bbeSBoris Popov 	if (error)
336681a5bbeSBoris Popov 		return error;
337681a5bbeSBoris Popov 	error = md_get_uint8(mdp, &tb);
338681a5bbeSBoris Popov 	if (rqp->sr_vc->vc_hflags2 & SMB_FLAGS2_ERR_STATUS) {
339681a5bbeSBoris Popov 		error = md_get_uint32le(mdp, &rqp->sr_error);
340681a5bbeSBoris Popov 	} else {
341681a5bbeSBoris Popov 		error = md_get_uint8(mdp, &rqp->sr_errclass);
342681a5bbeSBoris Popov 		error = md_get_uint8(mdp, &tb);
343681a5bbeSBoris Popov 		error = md_get_uint16le(mdp, &rqp->sr_serror);
344681a5bbeSBoris Popov 		if (!error)
345681a5bbeSBoris Popov 			rperror = smb_maperror(rqp->sr_errclass, rqp->sr_serror);
346681a5bbeSBoris Popov 	}
347681a5bbeSBoris Popov 	error = md_get_uint8(mdp, &rqp->sr_rpflags);
348681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rpflags2);
349681a5bbeSBoris Popov 
350681a5bbeSBoris Popov 	error = md_get_uint32(mdp, &tdw);
351681a5bbeSBoris Popov 	error = md_get_uint32(mdp, &tdw);
352681a5bbeSBoris Popov 	error = md_get_uint32(mdp, &tdw);
353681a5bbeSBoris Popov 
354681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rptid);
355681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rppid);
356681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rpuid);
357681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rpmid);
358681a5bbeSBoris Popov 
359190b2c4fSTim J. Robbins 	if (error == 0 &&
360190b2c4fSTim J. Robbins 	    (rqp->sr_vc->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE))
361190b2c4fSTim J. Robbins 		error = smb_rq_verify(rqp);
362190b2c4fSTim J. Robbins 
363681a5bbeSBoris Popov 	SMBSDEBUG("M:%04x, P:%04x, U:%04x, T:%04x, E: %d:%d\n",
364681a5bbeSBoris Popov 	    rqp->sr_rpmid, rqp->sr_rppid, rqp->sr_rpuid, rqp->sr_rptid,
365681a5bbeSBoris Popov 	    rqp->sr_errclass, rqp->sr_serror);
366681a5bbeSBoris Popov 	return error ? error : rperror;
367681a5bbeSBoris Popov }
368681a5bbeSBoris Popov 
369681a5bbeSBoris Popov 
370681a5bbeSBoris Popov #define ALIGN4(a)	(((a) + 3) & ~3)
371681a5bbeSBoris Popov 
372681a5bbeSBoris Popov /*
373681a5bbeSBoris Popov  * TRANS2 request implementation
374681a5bbeSBoris Popov  */
375681a5bbeSBoris Popov int
376681a5bbeSBoris Popov smb_t2_alloc(struct smb_connobj *layer, u_short setup, struct smb_cred *scred,
377681a5bbeSBoris Popov 	struct smb_t2rq **t2pp)
378681a5bbeSBoris Popov {
379681a5bbeSBoris Popov 	struct smb_t2rq *t2p;
380681a5bbeSBoris Popov 	int error;
381681a5bbeSBoris Popov 
3821ede983cSDag-Erling Smørgrav 	t2p = malloc(sizeof(*t2p), M_SMBRQ, M_WAITOK);
383681a5bbeSBoris Popov 	if (t2p == NULL)
384681a5bbeSBoris Popov 		return ENOMEM;
385681a5bbeSBoris Popov 	error = smb_t2_init(t2p, layer, setup, scred);
386681a5bbeSBoris Popov 	t2p->t2_flags |= SMBT2_ALLOCED;
387681a5bbeSBoris Popov 	if (error) {
388681a5bbeSBoris Popov 		smb_t2_done(t2p);
389681a5bbeSBoris Popov 		return error;
390681a5bbeSBoris Popov 	}
391681a5bbeSBoris Popov 	*t2pp = t2p;
392681a5bbeSBoris Popov 	return 0;
393681a5bbeSBoris Popov }
394681a5bbeSBoris Popov 
395681a5bbeSBoris Popov int
396681a5bbeSBoris Popov smb_t2_init(struct smb_t2rq *t2p, struct smb_connobj *source, u_short setup,
397681a5bbeSBoris Popov 	struct smb_cred *scred)
398681a5bbeSBoris Popov {
399681a5bbeSBoris Popov 	int error;
400681a5bbeSBoris Popov 
401681a5bbeSBoris Popov 	bzero(t2p, sizeof(*t2p));
402681a5bbeSBoris Popov 	t2p->t2_source = source;
403681a5bbeSBoris Popov 	t2p->t2_setupcount = 1;
404681a5bbeSBoris Popov 	t2p->t2_setupdata = t2p->t2_setup;
405681a5bbeSBoris Popov 	t2p->t2_setup[0] = setup;
406681a5bbeSBoris Popov 	t2p->t2_fid = 0xffff;
407681a5bbeSBoris Popov 	t2p->t2_cred = scred;
408681a5bbeSBoris Popov 	error = smb_rq_getenv(source, &t2p->t2_vc, NULL);
409681a5bbeSBoris Popov 	if (error)
410681a5bbeSBoris Popov 		return error;
411681a5bbeSBoris Popov 	return 0;
412681a5bbeSBoris Popov }
413681a5bbeSBoris Popov 
414681a5bbeSBoris Popov void
415681a5bbeSBoris Popov smb_t2_done(struct smb_t2rq *t2p)
416681a5bbeSBoris Popov {
417681a5bbeSBoris Popov 	mb_done(&t2p->t2_tparam);
418681a5bbeSBoris Popov 	mb_done(&t2p->t2_tdata);
419681a5bbeSBoris Popov 	md_done(&t2p->t2_rparam);
420681a5bbeSBoris Popov 	md_done(&t2p->t2_rdata);
421681a5bbeSBoris Popov 	if (t2p->t2_flags & SMBT2_ALLOCED)
422681a5bbeSBoris Popov 		free(t2p, M_SMBRQ);
423681a5bbeSBoris Popov }
424681a5bbeSBoris Popov 
425681a5bbeSBoris Popov static int
426681a5bbeSBoris Popov smb_t2_placedata(struct mbuf *mtop, u_int16_t offset, u_int16_t count,
427681a5bbeSBoris Popov 	struct mdchain *mdp)
428681a5bbeSBoris Popov {
429681a5bbeSBoris Popov 	struct mbuf *m, *m0;
430681a5bbeSBoris Popov 	int len;
431681a5bbeSBoris Popov 
432eb1b1807SGleb Smirnoff 	m0 = m_split(mtop, offset, M_WAITOK);
4337ed60de8SPoul-Henning Kamp 	len = m_length(m0, &m);
434681a5bbeSBoris Popov 	m->m_len -= len - count;
435681a5bbeSBoris Popov 	if (mdp->md_top == NULL) {
436681a5bbeSBoris Popov 		md_initm(mdp, m0);
437681a5bbeSBoris Popov 	} else
438681a5bbeSBoris Popov 		m_cat(mdp->md_top, m0);
439681a5bbeSBoris Popov 	return 0;
440681a5bbeSBoris Popov }
441681a5bbeSBoris Popov 
442681a5bbeSBoris Popov static int
443681a5bbeSBoris Popov smb_t2_reply(struct smb_t2rq *t2p)
444681a5bbeSBoris Popov {
445681a5bbeSBoris Popov 	struct mdchain *mdp;
446681a5bbeSBoris Popov 	struct smb_rq *rqp = t2p->t2_rq;
447681a5bbeSBoris Popov 	int error, totpgot, totdgot;
448681a5bbeSBoris Popov 	u_int16_t totpcount, totdcount, pcount, poff, doff, pdisp, ddisp;
449681a5bbeSBoris Popov 	u_int16_t tmp, bc, dcount;
450681a5bbeSBoris Popov 	u_int8_t wc;
451681a5bbeSBoris Popov 
452681a5bbeSBoris Popov 	error = smb_rq_reply(rqp);
453681a5bbeSBoris Popov 	if (error)
454681a5bbeSBoris Popov 		return error;
455681a5bbeSBoris Popov 	if ((t2p->t2_flags & SMBT2_ALLSENT) == 0) {
456681a5bbeSBoris Popov 		/*
457681a5bbeSBoris Popov 		 * this is an interim response, ignore it.
458681a5bbeSBoris Popov 		 */
459681a5bbeSBoris Popov 		SMBRQ_SLOCK(rqp);
460681a5bbeSBoris Popov 		md_next_record(&rqp->sr_rp);
461681a5bbeSBoris Popov 		SMBRQ_SUNLOCK(rqp);
462681a5bbeSBoris Popov 		return 0;
463681a5bbeSBoris Popov 	}
464681a5bbeSBoris Popov 	/*
465fc75194cSBoris Popov 	 * Now we have to get all subsequent responses. The CIFS specification
466fc75194cSBoris Popov 	 * says that they can be disordered which is weird.
467681a5bbeSBoris Popov 	 * TODO: timo
468681a5bbeSBoris Popov 	 */
469681a5bbeSBoris Popov 	totpgot = totdgot = 0;
470681a5bbeSBoris Popov 	totpcount = totdcount = 0xffff;
471681a5bbeSBoris Popov 	mdp = &rqp->sr_rp;
472681a5bbeSBoris Popov 	for (;;) {
473681a5bbeSBoris Popov 		m_dumpm(mdp->md_top);
474681a5bbeSBoris Popov 		if ((error = md_get_uint8(mdp, &wc)) != 0)
475681a5bbeSBoris Popov 			break;
476681a5bbeSBoris Popov 		if (wc < 10) {
477681a5bbeSBoris Popov 			error = ENOENT;
478681a5bbeSBoris Popov 			break;
479681a5bbeSBoris Popov 		}
480681a5bbeSBoris Popov 		if ((error = md_get_uint16le(mdp, &tmp)) != 0)
481681a5bbeSBoris Popov 			break;
482681a5bbeSBoris Popov 		if (totpcount > tmp)
483681a5bbeSBoris Popov 			totpcount = tmp;
484681a5bbeSBoris Popov 		md_get_uint16le(mdp, &tmp);
485681a5bbeSBoris Popov 		if (totdcount > tmp)
486681a5bbeSBoris Popov 			totdcount = tmp;
487681a5bbeSBoris Popov 		if ((error = md_get_uint16le(mdp, &tmp)) != 0 || /* reserved */
488681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &pcount)) != 0 ||
489681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &poff)) != 0 ||
490681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &pdisp)) != 0)
491681a5bbeSBoris Popov 			break;
492681a5bbeSBoris Popov 		if (pcount != 0 && pdisp != totpgot) {
493fc75194cSBoris Popov 			SMBERROR("Can't handle disordered parameters %d:%d\n",
494681a5bbeSBoris Popov 			    pdisp, totpgot);
495681a5bbeSBoris Popov 			error = EINVAL;
496681a5bbeSBoris Popov 			break;
497681a5bbeSBoris Popov 		}
498681a5bbeSBoris Popov 		if ((error = md_get_uint16le(mdp, &dcount)) != 0 ||
499681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &doff)) != 0 ||
500681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &ddisp)) != 0)
501681a5bbeSBoris Popov 			break;
502681a5bbeSBoris Popov 		if (dcount != 0 && ddisp != totdgot) {
503fc75194cSBoris Popov 			SMBERROR("Can't handle disordered data\n");
504681a5bbeSBoris Popov 			error = EINVAL;
505681a5bbeSBoris Popov 			break;
506681a5bbeSBoris Popov 		}
507681a5bbeSBoris Popov 		md_get_uint8(mdp, &wc);
508681a5bbeSBoris Popov 		md_get_uint8(mdp, NULL);
509681a5bbeSBoris Popov 		tmp = wc;
510681a5bbeSBoris Popov 		while (tmp--)
511681a5bbeSBoris Popov 			md_get_uint16(mdp, NULL);
512681a5bbeSBoris Popov 		if ((error = md_get_uint16le(mdp, &bc)) != 0)
513681a5bbeSBoris Popov 			break;
514681a5bbeSBoris Popov /*		tmp = SMB_HDRLEN + 1 + 10 * 2 + 2 * wc + 2;*/
515681a5bbeSBoris Popov 		if (dcount) {
516681a5bbeSBoris Popov 			error = smb_t2_placedata(mdp->md_top, doff, dcount,
517681a5bbeSBoris Popov 			    &t2p->t2_rdata);
518681a5bbeSBoris Popov 			if (error)
519681a5bbeSBoris Popov 				break;
520681a5bbeSBoris Popov 		}
521681a5bbeSBoris Popov 		if (pcount) {
522681a5bbeSBoris Popov 			error = smb_t2_placedata(mdp->md_top, poff, pcount,
523681a5bbeSBoris Popov 			    &t2p->t2_rparam);
524681a5bbeSBoris Popov 			if (error)
525681a5bbeSBoris Popov 				break;
526681a5bbeSBoris Popov 		}
527681a5bbeSBoris Popov 		totpgot += pcount;
528681a5bbeSBoris Popov 		totdgot += dcount;
529681a5bbeSBoris Popov 		if (totpgot >= totpcount && totdgot >= totdcount) {
530681a5bbeSBoris Popov 			error = 0;
531681a5bbeSBoris Popov 			t2p->t2_flags |= SMBT2_ALLRECV;
532681a5bbeSBoris Popov 			break;
533681a5bbeSBoris Popov 		}
534681a5bbeSBoris Popov 		/*
535681a5bbeSBoris Popov 		 * We're done with this reply, look for the next one.
536681a5bbeSBoris Popov 		 */
537681a5bbeSBoris Popov 		SMBRQ_SLOCK(rqp);
538681a5bbeSBoris Popov 		md_next_record(&rqp->sr_rp);
539681a5bbeSBoris Popov 		SMBRQ_SUNLOCK(rqp);
540681a5bbeSBoris Popov 		error = smb_rq_reply(rqp);
541681a5bbeSBoris Popov 		if (error)
542681a5bbeSBoris Popov 			break;
543681a5bbeSBoris Popov 	}
544681a5bbeSBoris Popov 	return error;
545681a5bbeSBoris Popov }
546681a5bbeSBoris Popov 
547681a5bbeSBoris Popov /*
548681a5bbeSBoris Popov  * Perform a full round of TRANS2 request
549681a5bbeSBoris Popov  */
550681a5bbeSBoris Popov static int
551681a5bbeSBoris Popov smb_t2_request_int(struct smb_t2rq *t2p)
552681a5bbeSBoris Popov {
553681a5bbeSBoris Popov 	struct smb_vc *vcp = t2p->t2_vc;
554681a5bbeSBoris Popov 	struct smb_cred *scred = t2p->t2_cred;
555681a5bbeSBoris Popov 	struct mbchain *mbp;
556681a5bbeSBoris Popov 	struct mdchain *mdp, mbparam, mbdata;
557681a5bbeSBoris Popov 	struct mbuf *m;
558681a5bbeSBoris Popov 	struct smb_rq *rqp;
559681a5bbeSBoris Popov 	int totpcount, leftpcount, totdcount, leftdcount, len, txmax, i;
560681a5bbeSBoris Popov 	int error, doff, poff, txdcount, txpcount, nmlen;
561681a5bbeSBoris Popov 
562681a5bbeSBoris Popov 	m = t2p->t2_tparam.mb_top;
563681a5bbeSBoris Popov 	if (m) {
564681a5bbeSBoris Popov 		md_initm(&mbparam, m);	/* do not free it! */
565681a5bbeSBoris Popov 		totpcount = m_fixhdr(m);
566681a5bbeSBoris Popov 		if (totpcount > 0xffff)		/* maxvalue for u_short */
567681a5bbeSBoris Popov 			return EINVAL;
568681a5bbeSBoris Popov 	} else
569681a5bbeSBoris Popov 		totpcount = 0;
570681a5bbeSBoris Popov 	m = t2p->t2_tdata.mb_top;
571681a5bbeSBoris Popov 	if (m) {
572681a5bbeSBoris Popov 		md_initm(&mbdata, m);	/* do not free it! */
573681a5bbeSBoris Popov 		totdcount =  m_fixhdr(m);
574681a5bbeSBoris Popov 		if (totdcount > 0xffff)
575681a5bbeSBoris Popov 			return EINVAL;
576681a5bbeSBoris Popov 	} else
577681a5bbeSBoris Popov 		totdcount = 0;
578681a5bbeSBoris Popov 	leftdcount = totdcount;
579681a5bbeSBoris Popov 	leftpcount = totpcount;
580681a5bbeSBoris Popov 	txmax = vcp->vc_txmax;
581681a5bbeSBoris Popov 	error = smb_rq_alloc(t2p->t2_source, t2p->t_name ?
582681a5bbeSBoris Popov 	    SMB_COM_TRANSACTION : SMB_COM_TRANSACTION2, scred, &rqp);
583681a5bbeSBoris Popov 	if (error)
584681a5bbeSBoris Popov 		return error;
585681a5bbeSBoris Popov 	rqp->sr_flags |= SMBR_MULTIPACKET;
586681a5bbeSBoris Popov 	t2p->t2_rq = rqp;
587190b2c4fSTim J. Robbins 	rqp->sr_t2 = t2p;
588681a5bbeSBoris Popov 	mbp = &rqp->sr_rq;
589681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
590681a5bbeSBoris Popov 	mb_put_uint16le(mbp, totpcount);
591681a5bbeSBoris Popov 	mb_put_uint16le(mbp, totdcount);
592681a5bbeSBoris Popov 	mb_put_uint16le(mbp, t2p->t2_maxpcount);
593681a5bbeSBoris Popov 	mb_put_uint16le(mbp, t2p->t2_maxdcount);
594681a5bbeSBoris Popov 	mb_put_uint8(mbp, t2p->t2_maxscount);
595681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);			/* reserved */
596681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);			/* flags */
597681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);			/* Timeout */
598681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);			/* reserved 2 */
599681a5bbeSBoris Popov 	len = mb_fixhdr(mbp);
600681a5bbeSBoris Popov 	/*
601681a5bbeSBoris Popov 	 * now we have known packet size as
602681a5bbeSBoris Popov 	 * ALIGN4(len + 5 * 2 + setupcount * 2 + 2 + strlen(name) + 1),
603681a5bbeSBoris Popov 	 * and need to decide which parts should go into the first request
604681a5bbeSBoris Popov 	 */
605681a5bbeSBoris Popov 	nmlen = t2p->t_name ? strlen(t2p->t_name) : 0;
606681a5bbeSBoris Popov 	len = ALIGN4(len + 5 * 2 + t2p->t2_setupcount * 2 + 2 + nmlen + 1);
607681a5bbeSBoris Popov 	if (len + leftpcount > txmax) {
608681a5bbeSBoris Popov 		txpcount = min(leftpcount, txmax - len);
609681a5bbeSBoris Popov 		poff = len;
610681a5bbeSBoris Popov 		txdcount = 0;
611681a5bbeSBoris Popov 		doff = 0;
612681a5bbeSBoris Popov 	} else {
613681a5bbeSBoris Popov 		txpcount = leftpcount;
614681a5bbeSBoris Popov 		poff = txpcount ? len : 0;
615681a5bbeSBoris Popov 		len = ALIGN4(len + txpcount);
616681a5bbeSBoris Popov 		txdcount = min(leftdcount, txmax - len);
617681a5bbeSBoris Popov 		doff = txdcount ? len : 0;
618681a5bbeSBoris Popov 	}
619681a5bbeSBoris Popov 	leftpcount -= txpcount;
620681a5bbeSBoris Popov 	leftdcount -= txdcount;
621681a5bbeSBoris Popov 	mb_put_uint16le(mbp, txpcount);
622681a5bbeSBoris Popov 	mb_put_uint16le(mbp, poff);
623681a5bbeSBoris Popov 	mb_put_uint16le(mbp, txdcount);
624681a5bbeSBoris Popov 	mb_put_uint16le(mbp, doff);
625681a5bbeSBoris Popov 	mb_put_uint8(mbp, t2p->t2_setupcount);
626681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);
627681a5bbeSBoris Popov 	for (i = 0; i < t2p->t2_setupcount; i++)
628681a5bbeSBoris Popov 		mb_put_uint16le(mbp, t2p->t2_setupdata[i]);
629681a5bbeSBoris Popov 	smb_rq_wend(rqp);
630681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
631681a5bbeSBoris Popov 	/* TDUNICODE */
632681a5bbeSBoris Popov 	if (t2p->t_name)
633681a5bbeSBoris Popov 		mb_put_mem(mbp, t2p->t_name, nmlen, MB_MSYSTEM);
634681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);	/* terminating zero */
635681a5bbeSBoris Popov 	len = mb_fixhdr(mbp);
636681a5bbeSBoris Popov 	if (txpcount) {
637681a5bbeSBoris Popov 		mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
638681a5bbeSBoris Popov 		error = md_get_mbuf(&mbparam, txpcount, &m);
639681a5bbeSBoris Popov 		SMBSDEBUG("%d:%d:%d\n", error, txpcount, txmax);
640681a5bbeSBoris Popov 		if (error)
641681a5bbeSBoris Popov 			goto freerq;
642681a5bbeSBoris Popov 		mb_put_mbuf(mbp, m);
643681a5bbeSBoris Popov 	}
644681a5bbeSBoris Popov 	len = mb_fixhdr(mbp);
645681a5bbeSBoris Popov 	if (txdcount) {
646681a5bbeSBoris Popov 		mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
647681a5bbeSBoris Popov 		error = md_get_mbuf(&mbdata, txdcount, &m);
648681a5bbeSBoris Popov 		if (error)
649681a5bbeSBoris Popov 			goto freerq;
650681a5bbeSBoris Popov 		mb_put_mbuf(mbp, m);
651681a5bbeSBoris Popov 	}
652681a5bbeSBoris Popov 	smb_rq_bend(rqp);	/* incredible, but thats it... */
653681a5bbeSBoris Popov 	error = smb_rq_enqueue(rqp);
654681a5bbeSBoris Popov 	if (error)
655681a5bbeSBoris Popov 		goto freerq;
656681a5bbeSBoris Popov 	if (leftpcount == 0 && leftdcount == 0)
657681a5bbeSBoris Popov 		t2p->t2_flags |= SMBT2_ALLSENT;
658681a5bbeSBoris Popov 	error = smb_t2_reply(t2p);
659681a5bbeSBoris Popov 	if (error)
660681a5bbeSBoris Popov 		goto bad;
661681a5bbeSBoris Popov 	while (leftpcount || leftdcount) {
662190b2c4fSTim J. Robbins 		t2p->t2_flags |= SMBT2_SECONDARY;
663681a5bbeSBoris Popov 		error = smb_rq_new(rqp, t2p->t_name ?
664681a5bbeSBoris Popov 		    SMB_COM_TRANSACTION_SECONDARY : SMB_COM_TRANSACTION2_SECONDARY);
665681a5bbeSBoris Popov 		if (error)
666681a5bbeSBoris Popov 			goto bad;
667681a5bbeSBoris Popov 		mbp = &rqp->sr_rq;
668681a5bbeSBoris Popov 		smb_rq_wstart(rqp);
669681a5bbeSBoris Popov 		mb_put_uint16le(mbp, totpcount);
670681a5bbeSBoris Popov 		mb_put_uint16le(mbp, totdcount);
671681a5bbeSBoris Popov 		len = mb_fixhdr(mbp);
672681a5bbeSBoris Popov 		/*
673681a5bbeSBoris Popov 		 * now we have known packet size as
674681a5bbeSBoris Popov 		 * ALIGN4(len + 7 * 2 + 2) for T2 request, and -2 for T one,
675681a5bbeSBoris Popov 		 * and need to decide which parts should go into request
676681a5bbeSBoris Popov 		 */
677681a5bbeSBoris Popov 		len = ALIGN4(len + 6 * 2 + 2);
678681a5bbeSBoris Popov 		if (t2p->t_name == NULL)
679681a5bbeSBoris Popov 			len += 2;
680681a5bbeSBoris Popov 		if (len + leftpcount > txmax) {
681681a5bbeSBoris Popov 			txpcount = min(leftpcount, txmax - len);
682681a5bbeSBoris Popov 			poff = len;
683681a5bbeSBoris Popov 			txdcount = 0;
684681a5bbeSBoris Popov 			doff = 0;
685681a5bbeSBoris Popov 		} else {
686681a5bbeSBoris Popov 			txpcount = leftpcount;
687681a5bbeSBoris Popov 			poff = txpcount ? len : 0;
688681a5bbeSBoris Popov 			len = ALIGN4(len + txpcount);
689681a5bbeSBoris Popov 			txdcount = min(leftdcount, txmax - len);
690681a5bbeSBoris Popov 			doff = txdcount ? len : 0;
691681a5bbeSBoris Popov 		}
692681a5bbeSBoris Popov 		mb_put_uint16le(mbp, txpcount);
693681a5bbeSBoris Popov 		mb_put_uint16le(mbp, poff);
694681a5bbeSBoris Popov 		mb_put_uint16le(mbp, totpcount - leftpcount);
695681a5bbeSBoris Popov 		mb_put_uint16le(mbp, txdcount);
696681a5bbeSBoris Popov 		mb_put_uint16le(mbp, doff);
697681a5bbeSBoris Popov 		mb_put_uint16le(mbp, totdcount - leftdcount);
698681a5bbeSBoris Popov 		leftpcount -= txpcount;
699681a5bbeSBoris Popov 		leftdcount -= txdcount;
700681a5bbeSBoris Popov 		if (t2p->t_name == NULL)
701681a5bbeSBoris Popov 			mb_put_uint16le(mbp, t2p->t2_fid);
702681a5bbeSBoris Popov 		smb_rq_wend(rqp);
703681a5bbeSBoris Popov 		smb_rq_bstart(rqp);
704681a5bbeSBoris Popov 		mb_put_uint8(mbp, 0);	/* name */
705681a5bbeSBoris Popov 		len = mb_fixhdr(mbp);
706681a5bbeSBoris Popov 		if (txpcount) {
707681a5bbeSBoris Popov 			mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
708681a5bbeSBoris Popov 			error = md_get_mbuf(&mbparam, txpcount, &m);
709681a5bbeSBoris Popov 			if (error)
710681a5bbeSBoris Popov 				goto bad;
711681a5bbeSBoris Popov 			mb_put_mbuf(mbp, m);
712681a5bbeSBoris Popov 		}
713681a5bbeSBoris Popov 		len = mb_fixhdr(mbp);
714681a5bbeSBoris Popov 		if (txdcount) {
715681a5bbeSBoris Popov 			mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
716681a5bbeSBoris Popov 			error = md_get_mbuf(&mbdata, txdcount, &m);
717681a5bbeSBoris Popov 			if (error)
718681a5bbeSBoris Popov 				goto bad;
719681a5bbeSBoris Popov 			mb_put_mbuf(mbp, m);
720681a5bbeSBoris Popov 		}
721681a5bbeSBoris Popov 		smb_rq_bend(rqp);
722681a5bbeSBoris Popov 		rqp->sr_state = SMBRQ_NOTSENT;
723681a5bbeSBoris Popov 		error = smb_iod_request(vcp->vc_iod, SMBIOD_EV_NEWRQ, NULL);
724681a5bbeSBoris Popov 		if (error)
725681a5bbeSBoris Popov 			goto bad;
726681a5bbeSBoris Popov 	}	/* while left params or data */
727681a5bbeSBoris Popov 	t2p->t2_flags |= SMBT2_ALLSENT;
728681a5bbeSBoris Popov 	mdp = &t2p->t2_rdata;
729681a5bbeSBoris Popov 	if (mdp->md_top) {
730681a5bbeSBoris Popov 		m_fixhdr(mdp->md_top);
731681a5bbeSBoris Popov 		md_initm(mdp, mdp->md_top);
732681a5bbeSBoris Popov 	}
733681a5bbeSBoris Popov 	mdp = &t2p->t2_rparam;
734681a5bbeSBoris Popov 	if (mdp->md_top) {
735681a5bbeSBoris Popov 		m_fixhdr(mdp->md_top);
736681a5bbeSBoris Popov 		md_initm(mdp, mdp->md_top);
737681a5bbeSBoris Popov 	}
738681a5bbeSBoris Popov bad:
739681a5bbeSBoris Popov 	smb_iod_removerq(rqp);
740681a5bbeSBoris Popov freerq:
741681a5bbeSBoris Popov 	smb_rq_done(rqp);
742681a5bbeSBoris Popov 	if (error) {
743681a5bbeSBoris Popov 		if (rqp->sr_flags & SMBR_RESTART)
744681a5bbeSBoris Popov 			t2p->t2_flags |= SMBT2_RESTART;
745681a5bbeSBoris Popov 		md_done(&t2p->t2_rparam);
746681a5bbeSBoris Popov 		md_done(&t2p->t2_rdata);
747681a5bbeSBoris Popov 	}
748681a5bbeSBoris Popov 	return error;
749681a5bbeSBoris Popov }
750681a5bbeSBoris Popov 
751681a5bbeSBoris Popov int
752681a5bbeSBoris Popov smb_t2_request(struct smb_t2rq *t2p)
753681a5bbeSBoris Popov {
754681a5bbeSBoris Popov 	int error = EINVAL, i;
755681a5bbeSBoris Popov 
756681a5bbeSBoris Popov 	for (i = 0; i < SMB_MAXRCN; i++) {
757681a5bbeSBoris Popov 		t2p->t2_flags &= ~SMBR_RESTART;
758681a5bbeSBoris Popov 		error = smb_t2_request_int(t2p);
759681a5bbeSBoris Popov 		if (error == 0)
760681a5bbeSBoris Popov 			break;
761681a5bbeSBoris Popov 		if ((t2p->t2_flags & (SMBT2_RESTART | SMBT2_NORESTART)) != SMBT2_RESTART)
762681a5bbeSBoris Popov 			break;
763681a5bbeSBoris Popov 	}
764681a5bbeSBoris Popov 	return error;
765681a5bbeSBoris Popov }
766