xref: /freebsd/sys/netsmb/smb_rq.c (revision 6e50988cf822f87a524b8da2fdc35b234078cc2f)
1c398230bSWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro 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 
29681a5bbeSBoris Popov #include <sys/param.h>
30681a5bbeSBoris Popov #include <sys/systm.h>
31a30d4b32SMike Barcroft #include <sys/endian.h>
32681a5bbeSBoris Popov #include <sys/kernel.h>
33681a5bbeSBoris Popov #include <sys/malloc.h>
345dba30f1SPoul-Henning Kamp #include <sys/module.h>
35681a5bbeSBoris Popov #include <sys/proc.h>
36681a5bbeSBoris Popov #include <sys/lock.h>
37681a5bbeSBoris Popov #include <sys/sysctl.h>
38681a5bbeSBoris Popov #include <sys/socket.h>
39681a5bbeSBoris Popov #include <sys/socketvar.h>
40681a5bbeSBoris Popov #include <sys/mbuf.h>
41681a5bbeSBoris Popov 
42681a5bbeSBoris Popov #include <netsmb/smb.h>
43681a5bbeSBoris Popov #include <netsmb/smb_conn.h>
44681a5bbeSBoris Popov #include <netsmb/smb_rq.h>
45681a5bbeSBoris Popov #include <netsmb/smb_subr.h>
46681a5bbeSBoris Popov #include <netsmb/smb_tran.h>
47681a5bbeSBoris Popov 
48d745c852SEd Schouten static MALLOC_DEFINE(M_SMBRQ, "SMBRQ", "SMB request");
49681a5bbeSBoris Popov 
50681a5bbeSBoris Popov MODULE_DEPEND(netsmb, libmchain, 1, 1, 1);
51681a5bbeSBoris Popov 
52681a5bbeSBoris Popov static int  smb_rq_reply(struct smb_rq *rqp);
53681a5bbeSBoris Popov static int  smb_rq_enqueue(struct smb_rq *rqp);
54681a5bbeSBoris Popov static int  smb_rq_getenv(struct smb_connobj *layer,
55681a5bbeSBoris Popov 		struct smb_vc **vcpp, struct smb_share **sspp);
56681a5bbeSBoris Popov static int  smb_rq_new(struct smb_rq *rqp, u_char cmd);
57681a5bbeSBoris Popov static int  smb_t2_reply(struct smb_t2rq *t2p);
58681a5bbeSBoris Popov 
59681a5bbeSBoris Popov int
smb_rq_alloc(struct smb_connobj * layer,u_char cmd,struct smb_cred * scred,struct smb_rq ** rqpp)60681a5bbeSBoris Popov smb_rq_alloc(struct smb_connobj *layer, u_char cmd, struct smb_cred *scred,
61681a5bbeSBoris Popov 	struct smb_rq **rqpp)
62681a5bbeSBoris Popov {
63681a5bbeSBoris Popov 	struct smb_rq *rqp;
64681a5bbeSBoris Popov 	int error;
65681a5bbeSBoris Popov 
661ede983cSDag-Erling Smørgrav 	rqp = malloc(sizeof(*rqp), M_SMBRQ, M_WAITOK);
67681a5bbeSBoris Popov 	error = smb_rq_init(rqp, layer, cmd, scred);
68681a5bbeSBoris Popov 	rqp->sr_flags |= SMBR_ALLOCED;
69681a5bbeSBoris Popov 	if (error) {
70681a5bbeSBoris Popov 		smb_rq_done(rqp);
71681a5bbeSBoris Popov 		return error;
72681a5bbeSBoris Popov 	}
73681a5bbeSBoris Popov 	*rqpp = rqp;
74681a5bbeSBoris Popov 	return 0;
75681a5bbeSBoris Popov }
76681a5bbeSBoris Popov 
77681a5bbeSBoris Popov static char tzero[12];
78681a5bbeSBoris Popov 
79681a5bbeSBoris Popov int
smb_rq_init(struct smb_rq * rqp,struct smb_connobj * layer,u_char cmd,struct smb_cred * scred)80681a5bbeSBoris Popov smb_rq_init(struct smb_rq *rqp, struct smb_connobj *layer, u_char cmd,
81681a5bbeSBoris Popov 	struct smb_cred *scred)
82681a5bbeSBoris Popov {
83681a5bbeSBoris Popov 	int error;
84681a5bbeSBoris Popov 
85681a5bbeSBoris Popov 	bzero(rqp, sizeof(*rqp));
86681a5bbeSBoris Popov 	smb_sl_init(&rqp->sr_slock, "srslock");
87681a5bbeSBoris Popov 	error = smb_rq_getenv(layer, &rqp->sr_vc, &rqp->sr_share);
88681a5bbeSBoris Popov 	if (error)
89681a5bbeSBoris Popov 		return error;
90681a5bbeSBoris Popov 	error = smb_vc_access(rqp->sr_vc, scred, SMBM_EXEC);
91681a5bbeSBoris Popov 	if (error)
92681a5bbeSBoris Popov 		return error;
93681a5bbeSBoris Popov 	if (rqp->sr_share) {
94681a5bbeSBoris Popov 		error = smb_share_access(rqp->sr_share, scred, SMBM_EXEC);
95681a5bbeSBoris Popov 		if (error)
96681a5bbeSBoris Popov 			return error;
97681a5bbeSBoris Popov 	}
98681a5bbeSBoris Popov 	rqp->sr_cred = scred;
99681a5bbeSBoris Popov 	rqp->sr_mid = smb_vc_nextmid(rqp->sr_vc);
100681a5bbeSBoris Popov 	return smb_rq_new(rqp, cmd);
101681a5bbeSBoris Popov }
102681a5bbeSBoris Popov 
103681a5bbeSBoris Popov static int
smb_rq_new(struct smb_rq * rqp,u_char cmd)104681a5bbeSBoris Popov smb_rq_new(struct smb_rq *rqp, u_char cmd)
105681a5bbeSBoris Popov {
106681a5bbeSBoris Popov 	struct smb_vc *vcp = rqp->sr_vc;
107681a5bbeSBoris Popov 	struct mbchain *mbp = &rqp->sr_rq;
108681a5bbeSBoris Popov 	int error;
109190b2c4fSTim J. Robbins 	u_int16_t flags2;
110681a5bbeSBoris Popov 
111681a5bbeSBoris Popov 	rqp->sr_sendcnt = 0;
112681a5bbeSBoris Popov 	mb_done(mbp);
113681a5bbeSBoris Popov 	md_done(&rqp->sr_rp);
114681a5bbeSBoris Popov 	error = mb_init(mbp);
115681a5bbeSBoris Popov 	if (error)
116681a5bbeSBoris Popov 		return error;
117681a5bbeSBoris Popov 	mb_put_mem(mbp, SMB_SIGNATURE, SMB_SIGLEN, MB_MSYSTEM);
118681a5bbeSBoris Popov 	mb_put_uint8(mbp, cmd);
119681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);		/* DosError */
120681a5bbeSBoris Popov 	mb_put_uint8(mbp, vcp->vc_hflags);
121190b2c4fSTim J. Robbins 	flags2 = vcp->vc_hflags2;
122ab93c874SBoris Popov 	if (cmd == SMB_COM_TRANSACTION || cmd == SMB_COM_TRANSACTION_SECONDARY)
123190b2c4fSTim J. Robbins 		flags2 &= ~SMB_FLAGS2_UNICODE;
124190b2c4fSTim J. Robbins 	if (cmd == SMB_COM_NEGOTIATE)
125190b2c4fSTim J. Robbins 		flags2 &= ~SMB_FLAGS2_SECURITY_SIGNATURE;
126190b2c4fSTim J. Robbins 	mb_put_uint16le(mbp, flags2);
127190b2c4fSTim J. Robbins 	if ((flags2 & SMB_FLAGS2_SECURITY_SIGNATURE) == 0) {
128681a5bbeSBoris Popov 		mb_put_mem(mbp, tzero, 12, MB_MSYSTEM);
129190b2c4fSTim J. Robbins 		rqp->sr_rqsig = NULL;
130190b2c4fSTim J. Robbins 	} else {
131190b2c4fSTim J. Robbins 		mb_put_uint16le(mbp, 0 /*scred->sc_p->p_pid >> 16*/);
132190b2c4fSTim J. Robbins 		rqp->sr_rqsig = (u_int8_t *)mb_reserve(mbp, 8);
133190b2c4fSTim J. Robbins 		mb_put_uint16le(mbp, 0);
134190b2c4fSTim J. Robbins 	}
135a6a4232fSMarcel Moolenaar 	rqp->sr_rqtid = mb_reserve(mbp, sizeof(u_int16_t));
136681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 1 /*scred->sc_p->p_pid & 0xffff*/);
137a6a4232fSMarcel Moolenaar 	rqp->sr_rquid = mb_reserve(mbp, sizeof(u_int16_t));
138681a5bbeSBoris Popov 	mb_put_uint16le(mbp, rqp->sr_mid);
139681a5bbeSBoris Popov 	return 0;
140681a5bbeSBoris Popov }
141681a5bbeSBoris Popov 
142681a5bbeSBoris Popov void
smb_rq_done(struct smb_rq * rqp)143681a5bbeSBoris Popov smb_rq_done(struct smb_rq *rqp)
144681a5bbeSBoris Popov {
145681a5bbeSBoris Popov 	mb_done(&rqp->sr_rq);
146681a5bbeSBoris Popov 	md_done(&rqp->sr_rp);
147681a5bbeSBoris Popov 	smb_sl_destroy(&rqp->sr_slock);
148681a5bbeSBoris Popov 	if (rqp->sr_flags & SMBR_ALLOCED)
149681a5bbeSBoris Popov 		free(rqp, M_SMBRQ);
150681a5bbeSBoris Popov }
151681a5bbeSBoris Popov 
152681a5bbeSBoris Popov /*
153681a5bbeSBoris Popov  * Simple request-reply exchange
154681a5bbeSBoris Popov  */
155681a5bbeSBoris Popov int
smb_rq_simple(struct smb_rq * rqp)156681a5bbeSBoris Popov smb_rq_simple(struct smb_rq *rqp)
157681a5bbeSBoris Popov {
158681a5bbeSBoris Popov 	struct smb_vc *vcp = rqp->sr_vc;
159681a5bbeSBoris Popov 	int error = EINVAL, i;
160681a5bbeSBoris Popov 
161681a5bbeSBoris Popov 	for (i = 0; i < SMB_MAXRCN; i++) {
162681a5bbeSBoris Popov 		rqp->sr_flags &= ~SMBR_RESTART;
163681a5bbeSBoris Popov 		rqp->sr_timo = vcp->vc_timo;
164681a5bbeSBoris Popov 		rqp->sr_state = SMBRQ_NOTSENT;
165681a5bbeSBoris Popov 		error = smb_rq_enqueue(rqp);
166681a5bbeSBoris Popov 		if (error)
167681a5bbeSBoris Popov 			return error;
168681a5bbeSBoris Popov 		error = smb_rq_reply(rqp);
169681a5bbeSBoris Popov 		if (error == 0)
170681a5bbeSBoris Popov 			break;
171681a5bbeSBoris Popov 		if ((rqp->sr_flags & (SMBR_RESTART | SMBR_NORESTART)) != SMBR_RESTART)
172681a5bbeSBoris Popov 			break;
173681a5bbeSBoris Popov 	}
174681a5bbeSBoris Popov 	return error;
175681a5bbeSBoris Popov }
176681a5bbeSBoris Popov 
177681a5bbeSBoris Popov static int
smb_rq_enqueue(struct smb_rq * rqp)178681a5bbeSBoris Popov smb_rq_enqueue(struct smb_rq *rqp)
179681a5bbeSBoris Popov {
180681a5bbeSBoris Popov 	struct smb_share *ssp = rqp->sr_share;
181681a5bbeSBoris Popov 	int error;
182681a5bbeSBoris Popov 
183681a5bbeSBoris Popov 	if (ssp == NULL || rqp->sr_cred == &rqp->sr_vc->vc_iod->iod_scred) {
184681a5bbeSBoris Popov 		return smb_iod_addrq(rqp);
185681a5bbeSBoris Popov 	}
186681a5bbeSBoris Popov 	for (;;) {
187681a5bbeSBoris Popov 		SMBS_ST_LOCK(ssp);
188681a5bbeSBoris Popov 		if (ssp->ss_flags & SMBS_RECONNECTING) {
189681a5bbeSBoris Popov 			msleep(&ssp->ss_vcgenid, SMBS_ST_LOCKPTR(ssp),
190681a5bbeSBoris Popov 			    PWAIT | PDROP, "90trcn", hz);
1914093529dSJeff Roberson 			if (smb_td_intr(rqp->sr_cred->scr_td))
192681a5bbeSBoris Popov 				return EINTR;
193681a5bbeSBoris Popov 			continue;
194681a5bbeSBoris Popov 		}
195681a5bbeSBoris Popov 		if (smb_share_valid(ssp) || (ssp->ss_flags & SMBS_CONNECTED) == 0) {
196681a5bbeSBoris Popov 			SMBS_ST_UNLOCK(ssp);
197681a5bbeSBoris Popov 		} else {
198681a5bbeSBoris Popov 			SMBS_ST_UNLOCK(ssp);
199681a5bbeSBoris Popov 			error = smb_iod_request(rqp->sr_vc->vc_iod,
200681a5bbeSBoris Popov 			    SMBIOD_EV_TREECONNECT | SMBIOD_EV_SYNC, ssp);
201681a5bbeSBoris Popov 			if (error)
202681a5bbeSBoris Popov 				return error;
203681a5bbeSBoris Popov 		}
204681a5bbeSBoris Popov 		error = smb_iod_addrq(rqp);
205681a5bbeSBoris Popov 		if (error != EXDEV)
206681a5bbeSBoris Popov 			break;
207681a5bbeSBoris Popov 	}
208681a5bbeSBoris Popov 	return error;
209681a5bbeSBoris Popov }
210681a5bbeSBoris Popov 
211681a5bbeSBoris Popov void
smb_rq_wstart(struct smb_rq * rqp)212681a5bbeSBoris Popov smb_rq_wstart(struct smb_rq *rqp)
213681a5bbeSBoris Popov {
214681a5bbeSBoris Popov 	rqp->sr_wcount = mb_reserve(&rqp->sr_rq, sizeof(u_int8_t));
215681a5bbeSBoris Popov 	rqp->sr_rq.mb_count = 0;
216681a5bbeSBoris Popov }
217681a5bbeSBoris Popov 
218681a5bbeSBoris Popov void
smb_rq_wend(struct smb_rq * rqp)219681a5bbeSBoris Popov smb_rq_wend(struct smb_rq *rqp)
220681a5bbeSBoris Popov {
221681a5bbeSBoris Popov 	if (rqp->sr_wcount == NULL) {
222681a5bbeSBoris Popov 		SMBERROR("no wcount\n");	/* actually panic */
223681a5bbeSBoris Popov 		return;
224681a5bbeSBoris Popov 	}
225681a5bbeSBoris Popov 	if (rqp->sr_rq.mb_count & 1)
226681a5bbeSBoris Popov 		SMBERROR("odd word count\n");
227681a5bbeSBoris Popov 	*rqp->sr_wcount = rqp->sr_rq.mb_count / 2;
228681a5bbeSBoris Popov }
229681a5bbeSBoris Popov 
230681a5bbeSBoris Popov void
smb_rq_bstart(struct smb_rq * rqp)231681a5bbeSBoris Popov smb_rq_bstart(struct smb_rq *rqp)
232681a5bbeSBoris Popov {
233a6a4232fSMarcel Moolenaar 	rqp->sr_bcount = mb_reserve(&rqp->sr_rq, sizeof(u_short));
234681a5bbeSBoris Popov 	rqp->sr_rq.mb_count = 0;
235681a5bbeSBoris Popov }
236681a5bbeSBoris Popov 
237681a5bbeSBoris Popov void
smb_rq_bend(struct smb_rq * rqp)238681a5bbeSBoris Popov smb_rq_bend(struct smb_rq *rqp)
239681a5bbeSBoris Popov {
240681a5bbeSBoris Popov 	int bcnt;
241681a5bbeSBoris Popov 
242681a5bbeSBoris Popov 	if (rqp->sr_bcount == NULL) {
243681a5bbeSBoris Popov 		SMBERROR("no bcount\n");	/* actually panic */
244681a5bbeSBoris Popov 		return;
245681a5bbeSBoris Popov 	}
246681a5bbeSBoris Popov 	bcnt = rqp->sr_rq.mb_count;
247681a5bbeSBoris Popov 	if (bcnt > 0xffff)
248681a5bbeSBoris Popov 		SMBERROR("byte count too large (%d)\n", bcnt);
249a6a4232fSMarcel Moolenaar 	le16enc(rqp->sr_bcount, bcnt);
250681a5bbeSBoris Popov }
251681a5bbeSBoris Popov 
252681a5bbeSBoris Popov int
smb_rq_intr(struct smb_rq * rqp)253681a5bbeSBoris Popov smb_rq_intr(struct smb_rq *rqp)
254681a5bbeSBoris Popov {
255681a5bbeSBoris Popov 	if (rqp->sr_flags & SMBR_INTR)
256681a5bbeSBoris Popov 		return EINTR;
2574093529dSJeff Roberson 	return smb_td_intr(rqp->sr_cred->scr_td);
258681a5bbeSBoris Popov }
259681a5bbeSBoris Popov 
260681a5bbeSBoris Popov int
smb_rq_getrequest(struct smb_rq * rqp,struct mbchain ** mbpp)261681a5bbeSBoris Popov smb_rq_getrequest(struct smb_rq *rqp, struct mbchain **mbpp)
262681a5bbeSBoris Popov {
263681a5bbeSBoris Popov 	*mbpp = &rqp->sr_rq;
264681a5bbeSBoris Popov 	return 0;
265681a5bbeSBoris Popov }
266681a5bbeSBoris Popov 
267681a5bbeSBoris Popov int
smb_rq_getreply(struct smb_rq * rqp,struct mdchain ** mbpp)268681a5bbeSBoris Popov smb_rq_getreply(struct smb_rq *rqp, struct mdchain **mbpp)
269681a5bbeSBoris Popov {
270681a5bbeSBoris Popov 	*mbpp = &rqp->sr_rp;
271681a5bbeSBoris Popov 	return 0;
272681a5bbeSBoris Popov }
273681a5bbeSBoris Popov 
274681a5bbeSBoris Popov static int
smb_rq_getenv(struct smb_connobj * layer,struct smb_vc ** vcpp,struct smb_share ** sspp)275681a5bbeSBoris Popov smb_rq_getenv(struct smb_connobj *layer,
276681a5bbeSBoris Popov 	struct smb_vc **vcpp, struct smb_share **sspp)
277681a5bbeSBoris Popov {
278681a5bbeSBoris Popov 	struct smb_vc *vcp = NULL;
279681a5bbeSBoris Popov 	struct smb_share *ssp = NULL;
280681a5bbeSBoris Popov 	struct smb_connobj *cp;
281681a5bbeSBoris Popov 	int error = 0;
282681a5bbeSBoris Popov 
283681a5bbeSBoris Popov 	switch (layer->co_level) {
284681a5bbeSBoris Popov 	    case SMBL_VC:
285681a5bbeSBoris Popov 		vcp = CPTOVC(layer);
286681a5bbeSBoris Popov 		if (layer->co_parent == NULL) {
287681a5bbeSBoris Popov 			SMBERROR("zombie VC %s\n", vcp->vc_srvname);
288681a5bbeSBoris Popov 			error = EINVAL;
289681a5bbeSBoris Popov 			break;
290681a5bbeSBoris Popov 		}
291681a5bbeSBoris Popov 		break;
292681a5bbeSBoris Popov 	    case SMBL_SHARE:
293681a5bbeSBoris Popov 		ssp = CPTOSS(layer);
294681a5bbeSBoris Popov 		cp = layer->co_parent;
295681a5bbeSBoris Popov 		if (cp == NULL) {
296681a5bbeSBoris Popov 			SMBERROR("zombie share %s\n", ssp->ss_name);
297681a5bbeSBoris Popov 			error = EINVAL;
298681a5bbeSBoris Popov 			break;
299681a5bbeSBoris Popov 		}
300681a5bbeSBoris Popov 		error = smb_rq_getenv(cp, &vcp, NULL);
301681a5bbeSBoris Popov 		if (error)
302681a5bbeSBoris Popov 			break;
303681a5bbeSBoris Popov 		break;
304681a5bbeSBoris Popov 	    default:
305681a5bbeSBoris Popov 		SMBERROR("invalid layer %d passed\n", layer->co_level);
306681a5bbeSBoris Popov 		error = EINVAL;
307681a5bbeSBoris Popov 	}
308681a5bbeSBoris Popov 	if (vcpp)
309681a5bbeSBoris Popov 		*vcpp = vcp;
310681a5bbeSBoris Popov 	if (sspp)
311681a5bbeSBoris Popov 		*sspp = ssp;
312681a5bbeSBoris Popov 	return error;
313681a5bbeSBoris Popov }
314681a5bbeSBoris Popov 
315681a5bbeSBoris Popov /*
316681a5bbeSBoris Popov  * Wait for reply on the request
317681a5bbeSBoris Popov  */
318681a5bbeSBoris Popov static int
smb_rq_reply(struct smb_rq * rqp)319681a5bbeSBoris Popov smb_rq_reply(struct smb_rq *rqp)
320681a5bbeSBoris Popov {
321681a5bbeSBoris Popov 	struct mdchain *mdp = &rqp->sr_rp;
322681a5bbeSBoris Popov 	u_int32_t tdw;
323681a5bbeSBoris Popov 	u_int8_t tb;
324681a5bbeSBoris Popov 	int error, rperror = 0;
325681a5bbeSBoris Popov 
326681a5bbeSBoris Popov 	error = smb_iod_waitrq(rqp);
327681a5bbeSBoris Popov 	if (error)
328681a5bbeSBoris Popov 		return error;
329681a5bbeSBoris Popov 	error = md_get_uint32(mdp, &tdw);
330681a5bbeSBoris Popov 	if (error)
331681a5bbeSBoris Popov 		return error;
332681a5bbeSBoris Popov 	error = md_get_uint8(mdp, &tb);
333681a5bbeSBoris Popov 	if (rqp->sr_vc->vc_hflags2 & SMB_FLAGS2_ERR_STATUS) {
334681a5bbeSBoris Popov 		error = md_get_uint32le(mdp, &rqp->sr_error);
335681a5bbeSBoris Popov 	} else {
336681a5bbeSBoris Popov 		error = md_get_uint8(mdp, &rqp->sr_errclass);
337681a5bbeSBoris Popov 		error = md_get_uint8(mdp, &tb);
338681a5bbeSBoris Popov 		error = md_get_uint16le(mdp, &rqp->sr_serror);
339681a5bbeSBoris Popov 		if (!error)
340681a5bbeSBoris Popov 			rperror = smb_maperror(rqp->sr_errclass, rqp->sr_serror);
341681a5bbeSBoris Popov 	}
342681a5bbeSBoris Popov 	error = md_get_uint8(mdp, &rqp->sr_rpflags);
343681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rpflags2);
344681a5bbeSBoris Popov 
345681a5bbeSBoris Popov 	error = md_get_uint32(mdp, &tdw);
346681a5bbeSBoris Popov 	error = md_get_uint32(mdp, &tdw);
347681a5bbeSBoris Popov 	error = md_get_uint32(mdp, &tdw);
348681a5bbeSBoris Popov 
349681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rptid);
350681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rppid);
351681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rpuid);
352681a5bbeSBoris Popov 	error = md_get_uint16le(mdp, &rqp->sr_rpmid);
353681a5bbeSBoris Popov 
354190b2c4fSTim J. Robbins 	if (error == 0 &&
355190b2c4fSTim J. Robbins 	    (rqp->sr_vc->vc_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE))
356190b2c4fSTim J. Robbins 		error = smb_rq_verify(rqp);
357190b2c4fSTim J. Robbins 
358681a5bbeSBoris Popov 	SMBSDEBUG("M:%04x, P:%04x, U:%04x, T:%04x, E: %d:%d\n",
359681a5bbeSBoris Popov 	    rqp->sr_rpmid, rqp->sr_rppid, rqp->sr_rpuid, rqp->sr_rptid,
360681a5bbeSBoris Popov 	    rqp->sr_errclass, rqp->sr_serror);
361681a5bbeSBoris Popov 	return error ? error : rperror;
362681a5bbeSBoris Popov }
363681a5bbeSBoris Popov 
364681a5bbeSBoris Popov #define ALIGN4(a)	(((a) + 3) & ~3)
365681a5bbeSBoris Popov 
366681a5bbeSBoris Popov /*
367681a5bbeSBoris Popov  * TRANS2 request implementation
368681a5bbeSBoris Popov  */
369681a5bbeSBoris Popov int
smb_t2_alloc(struct smb_connobj * layer,u_short setup,struct smb_cred * scred,struct smb_t2rq ** t2pp)370681a5bbeSBoris Popov smb_t2_alloc(struct smb_connobj *layer, u_short setup, struct smb_cred *scred,
371681a5bbeSBoris Popov 	struct smb_t2rq **t2pp)
372681a5bbeSBoris Popov {
373681a5bbeSBoris Popov 	struct smb_t2rq *t2p;
374681a5bbeSBoris Popov 	int error;
375681a5bbeSBoris Popov 
3761ede983cSDag-Erling Smørgrav 	t2p = malloc(sizeof(*t2p), M_SMBRQ, M_WAITOK);
377681a5bbeSBoris Popov 	error = smb_t2_init(t2p, layer, setup, scred);
378681a5bbeSBoris Popov 	t2p->t2_flags |= SMBT2_ALLOCED;
379681a5bbeSBoris Popov 	if (error) {
380681a5bbeSBoris Popov 		smb_t2_done(t2p);
381681a5bbeSBoris Popov 		return error;
382681a5bbeSBoris Popov 	}
383681a5bbeSBoris Popov 	*t2pp = t2p;
384681a5bbeSBoris Popov 	return 0;
385681a5bbeSBoris Popov }
386681a5bbeSBoris Popov 
387681a5bbeSBoris Popov int
smb_t2_init(struct smb_t2rq * t2p,struct smb_connobj * source,u_short setup,struct smb_cred * scred)388681a5bbeSBoris Popov smb_t2_init(struct smb_t2rq *t2p, struct smb_connobj *source, u_short setup,
389681a5bbeSBoris Popov 	struct smb_cred *scred)
390681a5bbeSBoris Popov {
391681a5bbeSBoris Popov 	int error;
392681a5bbeSBoris Popov 
393681a5bbeSBoris Popov 	bzero(t2p, sizeof(*t2p));
394681a5bbeSBoris Popov 	t2p->t2_source = source;
395681a5bbeSBoris Popov 	t2p->t2_setupcount = 1;
396681a5bbeSBoris Popov 	t2p->t2_setupdata = t2p->t2_setup;
397681a5bbeSBoris Popov 	t2p->t2_setup[0] = setup;
398681a5bbeSBoris Popov 	t2p->t2_fid = 0xffff;
399681a5bbeSBoris Popov 	t2p->t2_cred = scred;
400681a5bbeSBoris Popov 	error = smb_rq_getenv(source, &t2p->t2_vc, NULL);
401681a5bbeSBoris Popov 	if (error)
402681a5bbeSBoris Popov 		return error;
403681a5bbeSBoris Popov 	return 0;
404681a5bbeSBoris Popov }
405681a5bbeSBoris Popov 
406681a5bbeSBoris Popov void
smb_t2_done(struct smb_t2rq * t2p)407681a5bbeSBoris Popov smb_t2_done(struct smb_t2rq *t2p)
408681a5bbeSBoris Popov {
409681a5bbeSBoris Popov 	mb_done(&t2p->t2_tparam);
410681a5bbeSBoris Popov 	mb_done(&t2p->t2_tdata);
411681a5bbeSBoris Popov 	md_done(&t2p->t2_rparam);
412681a5bbeSBoris Popov 	md_done(&t2p->t2_rdata);
413681a5bbeSBoris Popov 	if (t2p->t2_flags & SMBT2_ALLOCED)
414681a5bbeSBoris Popov 		free(t2p, M_SMBRQ);
415681a5bbeSBoris Popov }
416681a5bbeSBoris Popov 
417681a5bbeSBoris Popov static int
smb_t2_placedata(struct mbuf * mtop,u_int16_t offset,u_int16_t count,struct mdchain * mdp)418681a5bbeSBoris Popov smb_t2_placedata(struct mbuf *mtop, u_int16_t offset, u_int16_t count,
419681a5bbeSBoris Popov 	struct mdchain *mdp)
420681a5bbeSBoris Popov {
421*aca3d65fSJohn Baldwin 	struct mbuf *m0;
422681a5bbeSBoris Popov 	int len;
423681a5bbeSBoris Popov 
424*aca3d65fSJohn Baldwin 	len = m_length(mtop, NULL);
425*aca3d65fSJohn Baldwin 	if (offset + count > len)
426*aca3d65fSJohn Baldwin 		return (EPROTO);
427*aca3d65fSJohn Baldwin 
428eb1b1807SGleb Smirnoff 	m0 = m_split(mtop, offset, M_WAITOK);
429*aca3d65fSJohn Baldwin 	if (len != offset + count) {
430*aca3d65fSJohn Baldwin 		len -= offset + count;
431*aca3d65fSJohn Baldwin 		m_adj(m0, -len);
432*aca3d65fSJohn Baldwin 	}
433681a5bbeSBoris Popov 	if (mdp->md_top == NULL) {
434681a5bbeSBoris Popov 		md_initm(mdp, m0);
435681a5bbeSBoris Popov 	} else
436681a5bbeSBoris Popov 		m_cat(mdp->md_top, m0);
437681a5bbeSBoris Popov 	return 0;
438681a5bbeSBoris Popov }
439681a5bbeSBoris Popov 
440681a5bbeSBoris Popov static int
smb_t2_reply(struct smb_t2rq * t2p)441681a5bbeSBoris Popov smb_t2_reply(struct smb_t2rq *t2p)
442681a5bbeSBoris Popov {
443681a5bbeSBoris Popov 	struct mdchain *mdp;
444681a5bbeSBoris Popov 	struct smb_rq *rqp = t2p->t2_rq;
445681a5bbeSBoris Popov 	int error, totpgot, totdgot;
446681a5bbeSBoris Popov 	u_int16_t totpcount, totdcount, pcount, poff, doff, pdisp, ddisp;
447681a5bbeSBoris Popov 	u_int16_t tmp, bc, dcount;
448681a5bbeSBoris Popov 	u_int8_t wc;
449681a5bbeSBoris Popov 
450681a5bbeSBoris Popov 	error = smb_rq_reply(rqp);
451681a5bbeSBoris Popov 	if (error)
452681a5bbeSBoris Popov 		return error;
453681a5bbeSBoris Popov 	if ((t2p->t2_flags & SMBT2_ALLSENT) == 0) {
454681a5bbeSBoris Popov 		/*
455681a5bbeSBoris Popov 		 * this is an interim response, ignore it.
456681a5bbeSBoris Popov 		 */
457681a5bbeSBoris Popov 		SMBRQ_SLOCK(rqp);
458681a5bbeSBoris Popov 		md_next_record(&rqp->sr_rp);
459681a5bbeSBoris Popov 		SMBRQ_SUNLOCK(rqp);
460681a5bbeSBoris Popov 		return 0;
461681a5bbeSBoris Popov 	}
462681a5bbeSBoris Popov 	/*
463fc75194cSBoris Popov 	 * Now we have to get all subsequent responses. The CIFS specification
464fc75194cSBoris Popov 	 * says that they can be disordered which is weird.
465681a5bbeSBoris Popov 	 * TODO: timo
466681a5bbeSBoris Popov 	 */
467681a5bbeSBoris Popov 	totpgot = totdgot = 0;
468681a5bbeSBoris Popov 	totpcount = totdcount = 0xffff;
469681a5bbeSBoris Popov 	mdp = &rqp->sr_rp;
470681a5bbeSBoris Popov 	for (;;) {
471681a5bbeSBoris Popov 		m_dumpm(mdp->md_top);
472681a5bbeSBoris Popov 		if ((error = md_get_uint8(mdp, &wc)) != 0)
473681a5bbeSBoris Popov 			break;
474681a5bbeSBoris Popov 		if (wc < 10) {
475681a5bbeSBoris Popov 			error = ENOENT;
476681a5bbeSBoris Popov 			break;
477681a5bbeSBoris Popov 		}
478681a5bbeSBoris Popov 		if ((error = md_get_uint16le(mdp, &tmp)) != 0)
479681a5bbeSBoris Popov 			break;
480681a5bbeSBoris Popov 		if (totpcount > tmp)
481681a5bbeSBoris Popov 			totpcount = tmp;
482681a5bbeSBoris Popov 		md_get_uint16le(mdp, &tmp);
483681a5bbeSBoris Popov 		if (totdcount > tmp)
484681a5bbeSBoris Popov 			totdcount = tmp;
485681a5bbeSBoris Popov 		if ((error = md_get_uint16le(mdp, &tmp)) != 0 || /* reserved */
486681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &pcount)) != 0 ||
487681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &poff)) != 0 ||
488681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &pdisp)) != 0)
489681a5bbeSBoris Popov 			break;
490681a5bbeSBoris Popov 		if (pcount != 0 && pdisp != totpgot) {
491fc75194cSBoris Popov 			SMBERROR("Can't handle disordered parameters %d:%d\n",
492681a5bbeSBoris Popov 			    pdisp, totpgot);
493681a5bbeSBoris Popov 			error = EINVAL;
494681a5bbeSBoris Popov 			break;
495681a5bbeSBoris Popov 		}
496681a5bbeSBoris Popov 		if ((error = md_get_uint16le(mdp, &dcount)) != 0 ||
497681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &doff)) != 0 ||
498681a5bbeSBoris Popov 		    (error = md_get_uint16le(mdp, &ddisp)) != 0)
499681a5bbeSBoris Popov 			break;
500681a5bbeSBoris Popov 		if (dcount != 0 && ddisp != totdgot) {
501fc75194cSBoris Popov 			SMBERROR("Can't handle disordered data\n");
502681a5bbeSBoris Popov 			error = EINVAL;
503681a5bbeSBoris Popov 			break;
504681a5bbeSBoris Popov 		}
505681a5bbeSBoris Popov 		md_get_uint8(mdp, &wc);
506681a5bbeSBoris Popov 		md_get_uint8(mdp, NULL);
507681a5bbeSBoris Popov 		tmp = wc;
508681a5bbeSBoris Popov 		while (tmp--)
509681a5bbeSBoris Popov 			md_get_uint16(mdp, NULL);
510681a5bbeSBoris Popov 		if ((error = md_get_uint16le(mdp, &bc)) != 0)
511681a5bbeSBoris Popov 			break;
512681a5bbeSBoris Popov /*		tmp = SMB_HDRLEN + 1 + 10 * 2 + 2 * wc + 2;*/
513681a5bbeSBoris Popov 		if (dcount) {
514681a5bbeSBoris Popov 			error = smb_t2_placedata(mdp->md_top, doff, dcount,
515681a5bbeSBoris Popov 			    &t2p->t2_rdata);
516681a5bbeSBoris Popov 			if (error)
517681a5bbeSBoris Popov 				break;
518681a5bbeSBoris Popov 		}
519681a5bbeSBoris Popov 		if (pcount) {
520681a5bbeSBoris Popov 			error = smb_t2_placedata(mdp->md_top, poff, pcount,
521681a5bbeSBoris Popov 			    &t2p->t2_rparam);
522681a5bbeSBoris Popov 			if (error)
523681a5bbeSBoris Popov 				break;
524681a5bbeSBoris Popov 		}
525681a5bbeSBoris Popov 		totpgot += pcount;
526681a5bbeSBoris Popov 		totdgot += dcount;
527681a5bbeSBoris Popov 		if (totpgot >= totpcount && totdgot >= totdcount) {
528681a5bbeSBoris Popov 			error = 0;
529681a5bbeSBoris Popov 			t2p->t2_flags |= SMBT2_ALLRECV;
530681a5bbeSBoris Popov 			break;
531681a5bbeSBoris Popov 		}
532681a5bbeSBoris Popov 		/*
533681a5bbeSBoris Popov 		 * We're done with this reply, look for the next one.
534681a5bbeSBoris Popov 		 */
535681a5bbeSBoris Popov 		SMBRQ_SLOCK(rqp);
536681a5bbeSBoris Popov 		md_next_record(&rqp->sr_rp);
537681a5bbeSBoris Popov 		SMBRQ_SUNLOCK(rqp);
538681a5bbeSBoris Popov 		error = smb_rq_reply(rqp);
539681a5bbeSBoris Popov 		if (error)
540681a5bbeSBoris Popov 			break;
541681a5bbeSBoris Popov 	}
542681a5bbeSBoris Popov 	return error;
543681a5bbeSBoris Popov }
544681a5bbeSBoris Popov 
545681a5bbeSBoris Popov /*
546681a5bbeSBoris Popov  * Perform a full round of TRANS2 request
547681a5bbeSBoris Popov  */
548681a5bbeSBoris Popov static int
smb_t2_request_int(struct smb_t2rq * t2p)549681a5bbeSBoris Popov smb_t2_request_int(struct smb_t2rq *t2p)
550681a5bbeSBoris Popov {
551681a5bbeSBoris Popov 	struct smb_vc *vcp = t2p->t2_vc;
552681a5bbeSBoris Popov 	struct smb_cred *scred = t2p->t2_cred;
553681a5bbeSBoris Popov 	struct mbchain *mbp;
554681a5bbeSBoris Popov 	struct mdchain *mdp, mbparam, mbdata;
555681a5bbeSBoris Popov 	struct mbuf *m;
556681a5bbeSBoris Popov 	struct smb_rq *rqp;
557681a5bbeSBoris Popov 	int totpcount, leftpcount, totdcount, leftdcount, len, txmax, i;
558681a5bbeSBoris Popov 	int error, doff, poff, txdcount, txpcount, nmlen;
559681a5bbeSBoris Popov 
560681a5bbeSBoris Popov 	m = t2p->t2_tparam.mb_top;
561681a5bbeSBoris Popov 	if (m) {
562681a5bbeSBoris Popov 		md_initm(&mbparam, m);	/* do not free it! */
563681a5bbeSBoris Popov 		totpcount = m_fixhdr(m);
564681a5bbeSBoris Popov 		if (totpcount > 0xffff)		/* maxvalue for u_short */
565681a5bbeSBoris Popov 			return EINVAL;
566681a5bbeSBoris Popov 	} else
567681a5bbeSBoris Popov 		totpcount = 0;
568681a5bbeSBoris Popov 	m = t2p->t2_tdata.mb_top;
569681a5bbeSBoris Popov 	if (m) {
570681a5bbeSBoris Popov 		md_initm(&mbdata, m);	/* do not free it! */
571681a5bbeSBoris Popov 		totdcount =  m_fixhdr(m);
572681a5bbeSBoris Popov 		if (totdcount > 0xffff)
573681a5bbeSBoris Popov 			return EINVAL;
574681a5bbeSBoris Popov 	} else
575681a5bbeSBoris Popov 		totdcount = 0;
576681a5bbeSBoris Popov 	leftdcount = totdcount;
577681a5bbeSBoris Popov 	leftpcount = totpcount;
578681a5bbeSBoris Popov 	txmax = vcp->vc_txmax;
579681a5bbeSBoris Popov 	error = smb_rq_alloc(t2p->t2_source, t2p->t_name ?
580681a5bbeSBoris Popov 	    SMB_COM_TRANSACTION : SMB_COM_TRANSACTION2, scred, &rqp);
581681a5bbeSBoris Popov 	if (error)
582681a5bbeSBoris Popov 		return error;
583681a5bbeSBoris Popov 	rqp->sr_flags |= SMBR_MULTIPACKET;
584681a5bbeSBoris Popov 	t2p->t2_rq = rqp;
585190b2c4fSTim J. Robbins 	rqp->sr_t2 = t2p;
586681a5bbeSBoris Popov 	mbp = &rqp->sr_rq;
587681a5bbeSBoris Popov 	smb_rq_wstart(rqp);
588681a5bbeSBoris Popov 	mb_put_uint16le(mbp, totpcount);
589681a5bbeSBoris Popov 	mb_put_uint16le(mbp, totdcount);
590681a5bbeSBoris Popov 	mb_put_uint16le(mbp, t2p->t2_maxpcount);
591681a5bbeSBoris Popov 	mb_put_uint16le(mbp, t2p->t2_maxdcount);
592681a5bbeSBoris Popov 	mb_put_uint8(mbp, t2p->t2_maxscount);
593681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);			/* reserved */
594681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);			/* flags */
595681a5bbeSBoris Popov 	mb_put_uint32le(mbp, 0);			/* Timeout */
596681a5bbeSBoris Popov 	mb_put_uint16le(mbp, 0);			/* reserved 2 */
597681a5bbeSBoris Popov 	len = mb_fixhdr(mbp);
598681a5bbeSBoris Popov 	/*
599681a5bbeSBoris Popov 	 * now we have known packet size as
600681a5bbeSBoris Popov 	 * ALIGN4(len + 5 * 2 + setupcount * 2 + 2 + strlen(name) + 1),
601681a5bbeSBoris Popov 	 * and need to decide which parts should go into the first request
602681a5bbeSBoris Popov 	 */
603681a5bbeSBoris Popov 	nmlen = t2p->t_name ? strlen(t2p->t_name) : 0;
604681a5bbeSBoris Popov 	len = ALIGN4(len + 5 * 2 + t2p->t2_setupcount * 2 + 2 + nmlen + 1);
605681a5bbeSBoris Popov 	if (len + leftpcount > txmax) {
606681a5bbeSBoris Popov 		txpcount = min(leftpcount, txmax - len);
607681a5bbeSBoris Popov 		poff = len;
608681a5bbeSBoris Popov 		txdcount = 0;
609681a5bbeSBoris Popov 		doff = 0;
610681a5bbeSBoris Popov 	} else {
611681a5bbeSBoris Popov 		txpcount = leftpcount;
612681a5bbeSBoris Popov 		poff = txpcount ? len : 0;
613681a5bbeSBoris Popov 		len = ALIGN4(len + txpcount);
614681a5bbeSBoris Popov 		txdcount = min(leftdcount, txmax - len);
615681a5bbeSBoris Popov 		doff = txdcount ? len : 0;
616681a5bbeSBoris Popov 	}
617681a5bbeSBoris Popov 	leftpcount -= txpcount;
618681a5bbeSBoris Popov 	leftdcount -= txdcount;
619681a5bbeSBoris Popov 	mb_put_uint16le(mbp, txpcount);
620681a5bbeSBoris Popov 	mb_put_uint16le(mbp, poff);
621681a5bbeSBoris Popov 	mb_put_uint16le(mbp, txdcount);
622681a5bbeSBoris Popov 	mb_put_uint16le(mbp, doff);
623681a5bbeSBoris Popov 	mb_put_uint8(mbp, t2p->t2_setupcount);
624681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);
625681a5bbeSBoris Popov 	for (i = 0; i < t2p->t2_setupcount; i++)
626681a5bbeSBoris Popov 		mb_put_uint16le(mbp, t2p->t2_setupdata[i]);
627681a5bbeSBoris Popov 	smb_rq_wend(rqp);
628681a5bbeSBoris Popov 	smb_rq_bstart(rqp);
629681a5bbeSBoris Popov 	/* TDUNICODE */
630681a5bbeSBoris Popov 	if (t2p->t_name)
631681a5bbeSBoris Popov 		mb_put_mem(mbp, t2p->t_name, nmlen, MB_MSYSTEM);
632681a5bbeSBoris Popov 	mb_put_uint8(mbp, 0);	/* terminating zero */
633681a5bbeSBoris Popov 	len = mb_fixhdr(mbp);
634681a5bbeSBoris Popov 	if (txpcount) {
635681a5bbeSBoris Popov 		mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
636681a5bbeSBoris Popov 		error = md_get_mbuf(&mbparam, txpcount, &m);
637681a5bbeSBoris Popov 		SMBSDEBUG("%d:%d:%d\n", error, txpcount, txmax);
638681a5bbeSBoris Popov 		if (error)
639681a5bbeSBoris Popov 			goto freerq;
640681a5bbeSBoris Popov 		mb_put_mbuf(mbp, m);
641681a5bbeSBoris Popov 	}
642681a5bbeSBoris Popov 	len = mb_fixhdr(mbp);
643681a5bbeSBoris Popov 	if (txdcount) {
644681a5bbeSBoris Popov 		mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
645681a5bbeSBoris Popov 		error = md_get_mbuf(&mbdata, txdcount, &m);
646681a5bbeSBoris Popov 		if (error)
647681a5bbeSBoris Popov 			goto freerq;
648681a5bbeSBoris Popov 		mb_put_mbuf(mbp, m);
649681a5bbeSBoris Popov 	}
650681a5bbeSBoris Popov 	smb_rq_bend(rqp);	/* incredible, but thats it... */
651681a5bbeSBoris Popov 	error = smb_rq_enqueue(rqp);
652681a5bbeSBoris Popov 	if (error)
653681a5bbeSBoris Popov 		goto freerq;
654681a5bbeSBoris Popov 	if (leftpcount == 0 && leftdcount == 0)
655681a5bbeSBoris Popov 		t2p->t2_flags |= SMBT2_ALLSENT;
656681a5bbeSBoris Popov 	error = smb_t2_reply(t2p);
657681a5bbeSBoris Popov 	if (error)
658681a5bbeSBoris Popov 		goto bad;
659681a5bbeSBoris Popov 	while (leftpcount || leftdcount) {
660190b2c4fSTim J. Robbins 		t2p->t2_flags |= SMBT2_SECONDARY;
661681a5bbeSBoris Popov 		error = smb_rq_new(rqp, t2p->t_name ?
662681a5bbeSBoris Popov 		    SMB_COM_TRANSACTION_SECONDARY : SMB_COM_TRANSACTION2_SECONDARY);
663681a5bbeSBoris Popov 		if (error)
664681a5bbeSBoris Popov 			goto bad;
665681a5bbeSBoris Popov 		mbp = &rqp->sr_rq;
666681a5bbeSBoris Popov 		smb_rq_wstart(rqp);
667681a5bbeSBoris Popov 		mb_put_uint16le(mbp, totpcount);
668681a5bbeSBoris Popov 		mb_put_uint16le(mbp, totdcount);
669681a5bbeSBoris Popov 		len = mb_fixhdr(mbp);
670681a5bbeSBoris Popov 		/*
671681a5bbeSBoris Popov 		 * now we have known packet size as
672681a5bbeSBoris Popov 		 * ALIGN4(len + 7 * 2 + 2) for T2 request, and -2 for T one,
673681a5bbeSBoris Popov 		 * and need to decide which parts should go into request
674681a5bbeSBoris Popov 		 */
675681a5bbeSBoris Popov 		len = ALIGN4(len + 6 * 2 + 2);
676681a5bbeSBoris Popov 		if (t2p->t_name == NULL)
677681a5bbeSBoris Popov 			len += 2;
678681a5bbeSBoris Popov 		if (len + leftpcount > txmax) {
679681a5bbeSBoris Popov 			txpcount = min(leftpcount, txmax - len);
680681a5bbeSBoris Popov 			poff = len;
681681a5bbeSBoris Popov 			txdcount = 0;
682681a5bbeSBoris Popov 			doff = 0;
683681a5bbeSBoris Popov 		} else {
684681a5bbeSBoris Popov 			txpcount = leftpcount;
685681a5bbeSBoris Popov 			poff = txpcount ? len : 0;
686681a5bbeSBoris Popov 			len = ALIGN4(len + txpcount);
687681a5bbeSBoris Popov 			txdcount = min(leftdcount, txmax - len);
688681a5bbeSBoris Popov 			doff = txdcount ? len : 0;
689681a5bbeSBoris Popov 		}
690681a5bbeSBoris Popov 		mb_put_uint16le(mbp, txpcount);
691681a5bbeSBoris Popov 		mb_put_uint16le(mbp, poff);
692681a5bbeSBoris Popov 		mb_put_uint16le(mbp, totpcount - leftpcount);
693681a5bbeSBoris Popov 		mb_put_uint16le(mbp, txdcount);
694681a5bbeSBoris Popov 		mb_put_uint16le(mbp, doff);
695681a5bbeSBoris Popov 		mb_put_uint16le(mbp, totdcount - leftdcount);
696681a5bbeSBoris Popov 		leftpcount -= txpcount;
697681a5bbeSBoris Popov 		leftdcount -= txdcount;
698681a5bbeSBoris Popov 		if (t2p->t_name == NULL)
699681a5bbeSBoris Popov 			mb_put_uint16le(mbp, t2p->t2_fid);
700681a5bbeSBoris Popov 		smb_rq_wend(rqp);
701681a5bbeSBoris Popov 		smb_rq_bstart(rqp);
702681a5bbeSBoris Popov 		mb_put_uint8(mbp, 0);	/* name */
703681a5bbeSBoris Popov 		len = mb_fixhdr(mbp);
704681a5bbeSBoris Popov 		if (txpcount) {
705681a5bbeSBoris Popov 			mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
706681a5bbeSBoris Popov 			error = md_get_mbuf(&mbparam, txpcount, &m);
707681a5bbeSBoris Popov 			if (error)
708681a5bbeSBoris Popov 				goto bad;
709681a5bbeSBoris Popov 			mb_put_mbuf(mbp, m);
710681a5bbeSBoris Popov 		}
711681a5bbeSBoris Popov 		len = mb_fixhdr(mbp);
712681a5bbeSBoris Popov 		if (txdcount) {
713681a5bbeSBoris Popov 			mb_put_mem(mbp, NULL, ALIGN4(len) - len, MB_MZERO);
714681a5bbeSBoris Popov 			error = md_get_mbuf(&mbdata, txdcount, &m);
715681a5bbeSBoris Popov 			if (error)
716681a5bbeSBoris Popov 				goto bad;
717681a5bbeSBoris Popov 			mb_put_mbuf(mbp, m);
718681a5bbeSBoris Popov 		}
719681a5bbeSBoris Popov 		smb_rq_bend(rqp);
720681a5bbeSBoris Popov 		rqp->sr_state = SMBRQ_NOTSENT;
721681a5bbeSBoris Popov 		error = smb_iod_request(vcp->vc_iod, SMBIOD_EV_NEWRQ, NULL);
722681a5bbeSBoris Popov 		if (error)
723681a5bbeSBoris Popov 			goto bad;
724681a5bbeSBoris Popov 	}	/* while left params or data */
725681a5bbeSBoris Popov 	t2p->t2_flags |= SMBT2_ALLSENT;
726681a5bbeSBoris Popov 	mdp = &t2p->t2_rdata;
727681a5bbeSBoris Popov 	if (mdp->md_top) {
728681a5bbeSBoris Popov 		m_fixhdr(mdp->md_top);
729681a5bbeSBoris Popov 		md_initm(mdp, mdp->md_top);
730681a5bbeSBoris Popov 	}
731681a5bbeSBoris Popov 	mdp = &t2p->t2_rparam;
732681a5bbeSBoris Popov 	if (mdp->md_top) {
733681a5bbeSBoris Popov 		m_fixhdr(mdp->md_top);
734681a5bbeSBoris Popov 		md_initm(mdp, mdp->md_top);
735681a5bbeSBoris Popov 	}
736681a5bbeSBoris Popov bad:
737681a5bbeSBoris Popov 	smb_iod_removerq(rqp);
738681a5bbeSBoris Popov freerq:
739681a5bbeSBoris Popov 	if (error) {
740681a5bbeSBoris Popov 		if (rqp->sr_flags & SMBR_RESTART)
741681a5bbeSBoris Popov 			t2p->t2_flags |= SMBT2_RESTART;
742681a5bbeSBoris Popov 		md_done(&t2p->t2_rparam);
743681a5bbeSBoris Popov 		md_done(&t2p->t2_rdata);
744681a5bbeSBoris Popov 	}
745771e95d2SMark Johnston 	smb_rq_done(rqp);
746681a5bbeSBoris Popov 	return error;
747681a5bbeSBoris Popov }
748681a5bbeSBoris Popov 
749681a5bbeSBoris Popov int
smb_t2_request(struct smb_t2rq * t2p)750681a5bbeSBoris Popov smb_t2_request(struct smb_t2rq *t2p)
751681a5bbeSBoris Popov {
752681a5bbeSBoris Popov 	int error = EINVAL, i;
753681a5bbeSBoris Popov 
754681a5bbeSBoris Popov 	for (i = 0; i < SMB_MAXRCN; i++) {
755681a5bbeSBoris Popov 		t2p->t2_flags &= ~SMBR_RESTART;
756681a5bbeSBoris Popov 		error = smb_t2_request_int(t2p);
757681a5bbeSBoris Popov 		if (error == 0)
758681a5bbeSBoris Popov 			break;
759681a5bbeSBoris Popov 		if ((t2p->t2_flags & (SMBT2_RESTART | SMBT2_NORESTART)) != SMBT2_RESTART)
760681a5bbeSBoris Popov 			break;
761681a5bbeSBoris Popov 	}
762681a5bbeSBoris Popov 	return error;
763681a5bbeSBoris Popov }
764