xref: /titanic_53/usr/src/lib/libsmbfs/smb/rq.c (revision 02d09e03eb27f3a2dc299de704e45dae5173f43f)
14bff34e3Sthurlow /*
24bff34e3Sthurlow  * Copyright (c) 2000, Boris Popov
34bff34e3Sthurlow  * All rights reserved.
44bff34e3Sthurlow  *
54bff34e3Sthurlow  * Redistribution and use in source and binary forms, with or without
64bff34e3Sthurlow  * modification, are permitted provided that the following conditions
74bff34e3Sthurlow  * are met:
84bff34e3Sthurlow  * 1. Redistributions of source code must retain the above copyright
94bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer.
104bff34e3Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
114bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer in the
124bff34e3Sthurlow  *    documentation and/or other materials provided with the distribution.
134bff34e3Sthurlow  * 3. All advertising materials mentioning features or use of this software
144bff34e3Sthurlow  *    must display the following acknowledgement:
154bff34e3Sthurlow  *    This product includes software developed by Boris Popov.
164bff34e3Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
174bff34e3Sthurlow  *    may be used to endorse or promote products derived from this software
184bff34e3Sthurlow  *    without specific prior written permission.
194bff34e3Sthurlow  *
204bff34e3Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
214bff34e3Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224bff34e3Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234bff34e3Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
244bff34e3Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254bff34e3Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264bff34e3Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274bff34e3Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284bff34e3Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294bff34e3Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304bff34e3Sthurlow  * SUCH DAMAGE.
314bff34e3Sthurlow  *
324bff34e3Sthurlow  * $Id: rq.c,v 1.4 2004/12/13 00:25:23 lindak Exp $
334bff34e3Sthurlow  */
344bff34e3Sthurlow 
354bff34e3Sthurlow #include <sys/types.h>
364bff34e3Sthurlow #include <sys/param.h>
374bff34e3Sthurlow #include <sys/ioctl.h>
384bff34e3Sthurlow #include <sys/errno.h>
394bff34e3Sthurlow #include <sys/stat.h>
404bff34e3Sthurlow 
414bff34e3Sthurlow #include <ctype.h>
424bff34e3Sthurlow #include <errno.h>
434bff34e3Sthurlow #include <stdio.h>
444bff34e3Sthurlow #include <unistd.h>
454bff34e3Sthurlow #include <strings.h>
464bff34e3Sthurlow #include <stdlib.h>
474bff34e3Sthurlow #include <sysexits.h>
484bff34e3Sthurlow #include <libintl.h>
494bff34e3Sthurlow 
50613a2f6bSGordon Ross #include <netsmb/smb.h>
514bff34e3Sthurlow #include <netsmb/smb_lib.h>
529c9af259SGordon Ross #include "private.h"
534bff34e3Sthurlow 
54*02d09e03SGordon Ross #define	MIN_REPLY_SIZE 4096
55*02d09e03SGordon Ross 
56613a2f6bSGordon Ross static uint32_t smb_map_doserr(uint8_t, uint16_t);
574bff34e3Sthurlow 
58613a2f6bSGordon Ross /*
59613a2f6bSGordon Ross  * Create and initialize a request structure, for either an
60613a2f6bSGordon Ross  * "internal" request (one that does not use the driver) or
61613a2f6bSGordon Ross  * a regular "driver" request, that uses driver ioctls.
62613a2f6bSGordon Ross  *
63613a2f6bSGordon Ross  * The two kinds are built a little differently:
64613a2f6bSGordon Ross  * Driver requests are composed starting with the
65613a2f6bSGordon Ross  * first word of the "variable word vector" section.
66613a2f6bSGordon Ross  * The driver prepends the SMB header and word count.
67613a2f6bSGordon Ross  * The driver also needs an output buffer to receive
68613a2f6bSGordon Ross  * the response, filled in via copyout in the ioctl.
69613a2f6bSGordon Ross  *
70613a2f6bSGordon Ross  * Internal requests are composed entirely in this library.
71613a2f6bSGordon Ross  * Space for the SMB header is reserved here, and later
72613a2f6bSGordon Ross  * filled in by smb_rq_internal before the send/receive.
73613a2f6bSGordon Ross  */
744bff34e3Sthurlow int
75613a2f6bSGordon Ross smb_rq_init(struct smb_ctx *ctx, uchar_t cmd, struct smb_rq **rqpp)
764bff34e3Sthurlow {
774bff34e3Sthurlow 	struct smb_rq *rqp;
784bff34e3Sthurlow 
794bff34e3Sthurlow 	rqp = malloc(sizeof (*rqp));
804bff34e3Sthurlow 	if (rqp == NULL)
81613a2f6bSGordon Ross 		goto errout;
824bff34e3Sthurlow 	bzero(rqp, sizeof (*rqp));
834bff34e3Sthurlow 	rqp->rq_cmd = cmd;
844bff34e3Sthurlow 	rqp->rq_ctx = ctx;
85613a2f6bSGordon Ross 
86613a2f6bSGordon Ross 	/*
87613a2f6bSGordon Ross 	 * Setup the request buffer.
88613a2f6bSGordon Ross 	 * Do the reply buffer later.
89613a2f6bSGordon Ross 	 */
90*02d09e03SGordon Ross 	if (mb_init(&rqp->rq_rq))
91613a2f6bSGordon Ross 		goto errout;
92613a2f6bSGordon Ross 
93613a2f6bSGordon Ross 	/* Space for the SMB header. (filled in later) */
94*02d09e03SGordon Ross 	mb_put_mem(&rqp->rq_rq, NULL, SMB_HDRLEN, MB_MSYSTEM);
95613a2f6bSGordon Ross 
96613a2f6bSGordon Ross 	/*
97613a2f6bSGordon Ross 	 * Copy the ctx flags here, so the caller can
98613a2f6bSGordon Ross 	 * update the req flags before the OTW call.
99613a2f6bSGordon Ross 	 */
100613a2f6bSGordon Ross 	rqp->rq_hflags = ctx->ct_hflags;
101613a2f6bSGordon Ross 	rqp->rq_hflags2 = ctx->ct_hflags2;
102613a2f6bSGordon Ross 
1034bff34e3Sthurlow 	*rqpp = rqp;
1044bff34e3Sthurlow 	return (0);
105613a2f6bSGordon Ross 
106613a2f6bSGordon Ross errout:
107613a2f6bSGordon Ross 	if (rqp) {
108613a2f6bSGordon Ross 		smb_rq_done(rqp);
109613a2f6bSGordon Ross 		free(rqp);
110613a2f6bSGordon Ross 	}
111613a2f6bSGordon Ross 	return (ENOMEM);
1124bff34e3Sthurlow }
1134bff34e3Sthurlow 
1144bff34e3Sthurlow void
1154bff34e3Sthurlow smb_rq_done(struct smb_rq *rqp)
1164bff34e3Sthurlow {
1174bff34e3Sthurlow 	mb_done(&rqp->rq_rp);
1184bff34e3Sthurlow 	mb_done(&rqp->rq_rq);
1194bff34e3Sthurlow 	free(rqp);
1204bff34e3Sthurlow }
1214bff34e3Sthurlow 
122613a2f6bSGordon Ross /*
123613a2f6bSGordon Ross  * Reserve space for the word count, which is filled in later by
124613a2f6bSGordon Ross  * smb_rq_wend().  Also initialize the counter that it uses
125613a2f6bSGordon Ross  * to figure out what value to fill in.
126613a2f6bSGordon Ross  *
127613a2f6bSGordon Ross  * Note that the word count happens to be 8-bits,
128613a2f6bSGordon Ross  * which can lead to confusion.
129613a2f6bSGordon Ross  */
130613a2f6bSGordon Ross void
131613a2f6bSGordon Ross smb_rq_wstart(struct smb_rq *rqp)
132613a2f6bSGordon Ross {
133613a2f6bSGordon Ross 	struct mbdata *mbp = &rqp->rq_rq;
134613a2f6bSGordon Ross 
135*02d09e03SGordon Ross 	(void) mb_fit(mbp, 1, &rqp->rq_wcntp);
136613a2f6bSGordon Ross 	rqp->rq_wcbase = mbp->mb_count;
137613a2f6bSGordon Ross }
138613a2f6bSGordon Ross 
139613a2f6bSGordon Ross /*
140613a2f6bSGordon Ross  * Fill in the word count, in the space reserved by
141613a2f6bSGordon Ross  * smb_rq_wstart().
142613a2f6bSGordon Ross  */
1434bff34e3Sthurlow void
1444bff34e3Sthurlow smb_rq_wend(struct smb_rq *rqp)
1454bff34e3Sthurlow {
146613a2f6bSGordon Ross 	struct mbdata *mbp = &rqp->rq_rq;
147613a2f6bSGordon Ross 	int wcnt;
148613a2f6bSGordon Ross 
149613a2f6bSGordon Ross 	if (rqp->rq_wcntp == NULL) {
150613a2f6bSGordon Ross 		DPRINT("no wcount ptr\n");
151613a2f6bSGordon Ross 		return;
152613a2f6bSGordon Ross 	}
153613a2f6bSGordon Ross 	wcnt = mbp->mb_count - rqp->rq_wcbase;
154613a2f6bSGordon Ross 	if (wcnt > 0x1ff)
155613a2f6bSGordon Ross 		DPRINT("word count too large (%d)\n", wcnt);
156613a2f6bSGordon Ross 	if (wcnt & 1)
157613a2f6bSGordon Ross 		DPRINT("odd word count\n");
158613a2f6bSGordon Ross 	wcnt >>= 1;
159613a2f6bSGordon Ross 
160613a2f6bSGordon Ross 	/*
161613a2f6bSGordon Ross 	 * Fill in the word count (8-bits).
162613a2f6bSGordon Ross 	 * Also store it in the rq, in case
163613a2f6bSGordon Ross 	 * we're using the ioctl path.
164613a2f6bSGordon Ross 	 */
165613a2f6bSGordon Ross 	*rqp->rq_wcntp = (char)wcnt;
1664bff34e3Sthurlow }
1674bff34e3Sthurlow 
168613a2f6bSGordon Ross /*
169613a2f6bSGordon Ross  * Reserve space for the byte count, which is filled in later by
170613a2f6bSGordon Ross  * smb_rq_bend().  Also initialize the counter that it uses
171613a2f6bSGordon Ross  * to figure out what value to fill in.
172613a2f6bSGordon Ross  *
173613a2f6bSGordon Ross  * Note that the byte count happens to be 16-bits,
174613a2f6bSGordon Ross  * which can lead to confusion.
175613a2f6bSGordon Ross  */
176613a2f6bSGordon Ross void
177613a2f6bSGordon Ross smb_rq_bstart(struct smb_rq *rqp)
1784bff34e3Sthurlow {
179613a2f6bSGordon Ross 	struct mbdata *mbp = &rqp->rq_rq;
1804bff34e3Sthurlow 
181*02d09e03SGordon Ross 	(void) mb_fit(mbp, 2, &rqp->rq_bcntp);
182613a2f6bSGordon Ross 	rqp->rq_bcbase = mbp->mb_count;
1834bff34e3Sthurlow }
1844bff34e3Sthurlow 
185613a2f6bSGordon Ross /*
186613a2f6bSGordon Ross  * Fill in the byte count, in the space reserved by
187613a2f6bSGordon Ross  * smb_rq_bstart().
188613a2f6bSGordon Ross  */
189613a2f6bSGordon Ross void
190613a2f6bSGordon Ross smb_rq_bend(struct smb_rq *rqp)
1914bff34e3Sthurlow {
192613a2f6bSGordon Ross 	struct mbdata *mbp = &rqp->rq_rq;
193613a2f6bSGordon Ross 	int bcnt;
194613a2f6bSGordon Ross 
195613a2f6bSGordon Ross 	if (rqp->rq_bcntp == NULL) {
196613a2f6bSGordon Ross 		DPRINT("no bcount ptr\n");
197613a2f6bSGordon Ross 		return;
1984bff34e3Sthurlow 	}
199613a2f6bSGordon Ross 	bcnt = mbp->mb_count - rqp->rq_bcbase;
200613a2f6bSGordon Ross 	if (bcnt > 0xffff)
201613a2f6bSGordon Ross 		DPRINT("byte count too large (%d)\n", bcnt);
202613a2f6bSGordon Ross 	/*
203613a2f6bSGordon Ross 	 * Fill in the byte count (16-bits).
204613a2f6bSGordon Ross 	 * Also store it in the rq, in case
205613a2f6bSGordon Ross 	 * we're using the ioctl path.
206613a2f6bSGordon Ross 	 *
207613a2f6bSGordon Ross 	 * The pointer is char * type due to
208613a2f6bSGordon Ross 	 * typical off-by-one alignment.
209613a2f6bSGordon Ross 	 */
210613a2f6bSGordon Ross 	rqp->rq_bcntp[0] = bcnt & 0xFF;
211613a2f6bSGordon Ross 	rqp->rq_bcntp[1] = (bcnt >> 8);
212613a2f6bSGordon Ross }
213613a2f6bSGordon Ross 
2144bff34e3Sthurlow int
2154bff34e3Sthurlow smb_rq_simple(struct smb_rq *rqp)
2164bff34e3Sthurlow {
2174bff34e3Sthurlow 	struct smbioc_rq krq;
2184bff34e3Sthurlow 	struct mbdata *mbp;
219*02d09e03SGordon Ross 	mbuf_t *m;
2204bff34e3Sthurlow 	char *data;
221613a2f6bSGordon Ross 	uint32_t len;
222613a2f6bSGordon Ross 	size_t rpbufsz;
223*02d09e03SGordon Ross 	int error;
2244bff34e3Sthurlow 
225613a2f6bSGordon Ross 	bzero(&krq, sizeof (krq));
226613a2f6bSGordon Ross 	krq.ioc_cmd = rqp->rq_cmd;
227613a2f6bSGordon Ross 
228613a2f6bSGordon Ross 	/*
229613a2f6bSGordon Ross 	 * Make the SMB request body contiguous,
230613a2f6bSGordon Ross 	 * and fill in the ioctl request.
231613a2f6bSGordon Ross 	 */
2324bff34e3Sthurlow 	mbp = smb_rq_getrequest(rqp);
233*02d09e03SGordon Ross 	error = m_lineup(mbp->mb_top, &mbp->mb_top);
234*02d09e03SGordon Ross 	if (error)
235*02d09e03SGordon Ross 		return (error);
236*02d09e03SGordon Ross 
2374bff34e3Sthurlow 	data = mtod(mbp->mb_top, char *);
238613a2f6bSGordon Ross 	len = m_totlen(mbp->mb_top);
2394bff34e3Sthurlow 
240613a2f6bSGordon Ross 	/*
241613a2f6bSGordon Ross 	 * _rq_init left space for the SMB header,
242613a2f6bSGordon Ross 	 * which makes mb_count the offset from
243613a2f6bSGordon Ross 	 * the beginning of the header (useful).
244613a2f6bSGordon Ross 	 * However, in this code path the driver
245613a2f6bSGordon Ross 	 * prepends the header, so we skip it.
246613a2f6bSGordon Ross 	 */
247613a2f6bSGordon Ross 	krq.ioc_tbufsz = len - SMB_HDRLEN;
248613a2f6bSGordon Ross 	krq.ioc_tbuf  = data + SMB_HDRLEN;
249613a2f6bSGordon Ross 
250613a2f6bSGordon Ross 	/*
251*02d09e03SGordon Ross 	 * Setup a buffer to hold the reply,
252*02d09e03SGordon Ross 	 * at least MIN_REPLY_SIZE, or larger
253*02d09e03SGordon Ross 	 * if the caller increased rq_rpbufsz.
254613a2f6bSGordon Ross 	 */
2554bff34e3Sthurlow 	mbp = smb_rq_getreply(rqp);
256613a2f6bSGordon Ross 	rpbufsz = rqp->rq_rpbufsz;
257*02d09e03SGordon Ross 	if (rpbufsz < MIN_REPLY_SIZE)
258*02d09e03SGordon Ross 		rpbufsz = MIN_REPLY_SIZE;
259*02d09e03SGordon Ross 	if ((error = m_get(rpbufsz, &m)) != 0)
260*02d09e03SGordon Ross 		return (error);
261*02d09e03SGordon Ross 	mb_initm(mbp, m);
262613a2f6bSGordon Ross 	krq.ioc_rbufsz = rpbufsz;
263*02d09e03SGordon Ross 	krq.ioc_rbuf = mtod(m, char *);
264613a2f6bSGordon Ross 
265613a2f6bSGordon Ross 	/*
266613a2f6bSGordon Ross 	 * Call the driver
267613a2f6bSGordon Ross 	 */
268613a2f6bSGordon Ross 	if (ioctl(rqp->rq_ctx->ct_dev_fd, SMBIOC_REQUEST, &krq) == -1)
2694bff34e3Sthurlow 		return (errno);
270613a2f6bSGordon Ross 
271613a2f6bSGordon Ross 	/*
272613a2f6bSGordon Ross 	 * Initialize returned mbdata.
273613a2f6bSGordon Ross 	 * SMB header already parsed.
274613a2f6bSGordon Ross 	 */
275*02d09e03SGordon Ross 	m->m_len = krq.ioc_rbufsz;
276613a2f6bSGordon Ross 
2774bff34e3Sthurlow 	return (0);
2784bff34e3Sthurlow }
2794bff34e3Sthurlow 
2804bff34e3Sthurlow 
2814bff34e3Sthurlow int
2824bff34e3Sthurlow smb_t2_request(struct smb_ctx *ctx, int setupcount, uint16_t *setup,
2834bff34e3Sthurlow 	const char *name,
2844bff34e3Sthurlow 	int tparamcnt, void *tparam,
2854bff34e3Sthurlow 	int tdatacnt, void *tdata,
2864bff34e3Sthurlow 	int *rparamcnt, void *rparam,
2874bff34e3Sthurlow 	int *rdatacnt, void *rdata,
2884bff34e3Sthurlow 	int *buffer_oflow)
2894bff34e3Sthurlow {
2904bff34e3Sthurlow 	smbioc_t2rq_t *krq;
2914bff34e3Sthurlow 	int i;
2924bff34e3Sthurlow 
2934bff34e3Sthurlow 	krq = (smbioc_t2rq_t *)malloc(sizeof (smbioc_t2rq_t));
2944bff34e3Sthurlow 	bzero(krq, sizeof (*krq));
2954bff34e3Sthurlow 
296613a2f6bSGordon Ross 	if (setupcount < 0 || setupcount >= SMBIOC_T2RQ_MAXSETUP) {
2974bff34e3Sthurlow 		/* Bogus setup count, or too many setup words */
2984bff34e3Sthurlow 		return (EINVAL);
2994bff34e3Sthurlow 	}
3004bff34e3Sthurlow 	for (i = 0; i < setupcount; i++)
3014bff34e3Sthurlow 		krq->ioc_setup[i] = setup[i];
3024bff34e3Sthurlow 	krq->ioc_setupcnt = setupcount;
3034bff34e3Sthurlow 	strcpy(krq->ioc_name, name);
3044bff34e3Sthurlow 	krq->ioc_tparamcnt = tparamcnt;
3054bff34e3Sthurlow 	krq->ioc_tparam = tparam;
3064bff34e3Sthurlow 	krq->ioc_tdatacnt = tdatacnt;
3074bff34e3Sthurlow 	krq->ioc_tdata = tdata;
3084bff34e3Sthurlow 
3094bff34e3Sthurlow 	krq->ioc_rparamcnt = *rparamcnt;
3104bff34e3Sthurlow 	krq->ioc_rdatacnt = *rdatacnt;
3114bff34e3Sthurlow 	krq->ioc_rparam = rparam;
3124bff34e3Sthurlow 	krq->ioc_rdata  = rdata;
3134bff34e3Sthurlow 
314613a2f6bSGordon Ross 	if (ioctl(ctx->ct_dev_fd, SMBIOC_T2RQ, krq) == -1) {
3154bff34e3Sthurlow 		return (errno);
3164bff34e3Sthurlow 	}
3174bff34e3Sthurlow 
3184bff34e3Sthurlow 	*rparamcnt = krq->ioc_rparamcnt;
3194bff34e3Sthurlow 	*rdatacnt = krq->ioc_rdatacnt;
3204bff34e3Sthurlow 	*buffer_oflow = (krq->ioc_rpflags2 & SMB_FLAGS2_ERR_STATUS) &&
3214bff34e3Sthurlow 	    (krq->ioc_error == NT_STATUS_BUFFER_OVERFLOW);
3224bff34e3Sthurlow 	free(krq);
323613a2f6bSGordon Ross 
3244bff34e3Sthurlow 	return (0);
3254bff34e3Sthurlow }
326613a2f6bSGordon Ross 
327613a2f6bSGordon Ross 
328613a2f6bSGordon Ross /*
329613a2f6bSGordon Ross  * Do an over-the-wire call without using the nsmb driver.
330613a2f6bSGordon Ross  * This is all "internal" to this library, and used only
331613a2f6bSGordon Ross  * for connection setup (negotiate protocol, etc.)
332613a2f6bSGordon Ross  */
333613a2f6bSGordon Ross int
334613a2f6bSGordon Ross smb_rq_internal(struct smb_ctx *ctx, struct smb_rq *rqp)
335613a2f6bSGordon Ross {
336613a2f6bSGordon Ross 	static const uint8_t ffsmb[4] = SMB_SIGNATURE;
337613a2f6bSGordon Ross 	struct smb_iods *is = &ctx->ct_iods;
338613a2f6bSGordon Ross 	uint32_t sigbuf[2];
339613a2f6bSGordon Ross 	struct mbdata mbtmp, *mbp;
340613a2f6bSGordon Ross 	int err, save_mlen;
341613a2f6bSGordon Ross 	uint8_t ctmp;
342613a2f6bSGordon Ross 
343613a2f6bSGordon Ross 	rqp->rq_uid = is->is_smbuid;
344613a2f6bSGordon Ross 	rqp->rq_tid = SMB_TID_UNKNOWN;
345613a2f6bSGordon Ross 	rqp->rq_mid = is->is_next_mid++;
346613a2f6bSGordon Ross 
347613a2f6bSGordon Ross 	/*
348613a2f6bSGordon Ross 	 * Fill in the NBT and SMB headers
349613a2f6bSGordon Ross 	 * Using mbtmp so we can rewind without
350613a2f6bSGordon Ross 	 * affecting the passed request mbdata.
351613a2f6bSGordon Ross 	 */
352613a2f6bSGordon Ross 	bcopy(&rqp->rq_rq, &mbtmp, sizeof (mbtmp));
353613a2f6bSGordon Ross 	mbp = &mbtmp;
354613a2f6bSGordon Ross 	mbp->mb_cur = mbp->mb_top;
355613a2f6bSGordon Ross 	mbp->mb_pos = mbp->mb_cur->m_data;
356613a2f6bSGordon Ross 	mbp->mb_count = 0;
357613a2f6bSGordon Ross 	/* Have to save and restore m_len */
358613a2f6bSGordon Ross 	save_mlen = mbp->mb_cur->m_len;
359613a2f6bSGordon Ross 	mbp->mb_cur->m_len = 0;
360613a2f6bSGordon Ross 
361613a2f6bSGordon Ross 	/*
362613a2f6bSGordon Ross 	 * rewind done; fill it in
363613a2f6bSGordon Ross 	 */
364*02d09e03SGordon Ross 	mb_put_mem(mbp, ffsmb, SMB_SIGLEN, MB_MSYSTEM);
365613a2f6bSGordon Ross 	mb_put_uint8(mbp, rqp->rq_cmd);
366*02d09e03SGordon Ross 	mb_put_uint32le(mbp, 0);	/* status */
367613a2f6bSGordon Ross 	mb_put_uint8(mbp, rqp->rq_hflags);
368613a2f6bSGordon Ross 	mb_put_uint16le(mbp, rqp->rq_hflags2);
369*02d09e03SGordon Ross 	/* pid_hi(2), signature(8), reserved(2) */
370*02d09e03SGordon Ross 	mb_put_mem(mbp, NULL, 12, MB_MZERO);
371613a2f6bSGordon Ross 	mb_put_uint16le(mbp, rqp->rq_tid);
372613a2f6bSGordon Ross 	mb_put_uint16le(mbp, 0);	/* pid_lo */
373613a2f6bSGordon Ross 	mb_put_uint16le(mbp, rqp->rq_uid);
374613a2f6bSGordon Ross 	mb_put_uint16le(mbp, rqp->rq_mid);
375613a2f6bSGordon Ross 
376613a2f6bSGordon Ross 	/* Restore original m_len */
377613a2f6bSGordon Ross 	mbp->mb_cur->m_len = save_mlen;
378613a2f6bSGordon Ross 
379613a2f6bSGordon Ross 	/*
380613a2f6bSGordon Ross 	 * Sign the message, if flags2 indicates.
381613a2f6bSGordon Ross 	 */
382613a2f6bSGordon Ross 	if (rqp->rq_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) {
383613a2f6bSGordon Ross 		smb_rq_sign(rqp);
384613a2f6bSGordon Ross 	}
385613a2f6bSGordon Ross 
386613a2f6bSGordon Ross 	/*
387613a2f6bSGordon Ross 	 * Send it, wait for the reply.
388613a2f6bSGordon Ross 	 */
389613a2f6bSGordon Ross 	if ((err = smb_ssn_send(ctx, &rqp->rq_rq)) != 0)
390613a2f6bSGordon Ross 		return (err);
391613a2f6bSGordon Ross 
392613a2f6bSGordon Ross 	if ((err = smb_ssn_recv(ctx, &rqp->rq_rp)) != 0)
393613a2f6bSGordon Ross 		return (err);
394613a2f6bSGordon Ross 
395613a2f6bSGordon Ross 	/*
396613a2f6bSGordon Ross 	 * Should have an SMB header, at least.
397613a2f6bSGordon Ross 	 */
398613a2f6bSGordon Ross 	mbp = &rqp->rq_rp;
399613a2f6bSGordon Ross 	if (mbp->mb_cur->m_len < SMB_HDRLEN) {
400613a2f6bSGordon Ross 		DPRINT("len < 32");
401613a2f6bSGordon Ross 		return (EBADRPC);
402613a2f6bSGordon Ross 	}
403613a2f6bSGordon Ross 
404613a2f6bSGordon Ross 	/*
405613a2f6bSGordon Ross 	 * If the request was signed, validate the
406613a2f6bSGordon Ross 	 * signature on the response.
407613a2f6bSGordon Ross 	 */
408613a2f6bSGordon Ross 	if (rqp->rq_hflags2 & SMB_FLAGS2_SECURITY_SIGNATURE) {
409613a2f6bSGordon Ross 		err = smb_rq_verify(rqp);
410613a2f6bSGordon Ross 		if (err) {
411613a2f6bSGordon Ross 			DPRINT("bad signature");
412613a2f6bSGordon Ross 			return (err);
413613a2f6bSGordon Ross 		}
414613a2f6bSGordon Ross 	}
415613a2f6bSGordon Ross 
416613a2f6bSGordon Ross 	/*
417613a2f6bSGordon Ross 	 * Decode the SMB header.
418613a2f6bSGordon Ross 	 */
419*02d09e03SGordon Ross 	md_get_mem(mbp, (char *)sigbuf, 4, MB_MSYSTEM);
420613a2f6bSGordon Ross 	if (0 != bcmp(sigbuf, ffsmb, 4)) {
421613a2f6bSGordon Ross 		DPRINT("not SMB");
422613a2f6bSGordon Ross 		return (EBADRPC);
423613a2f6bSGordon Ross 	}
424*02d09e03SGordon Ross 	md_get_uint8(mbp, &ctmp);	/* SMB cmd */
425*02d09e03SGordon Ross 	md_get_uint32le(mbp, &rqp->rq_status);
426*02d09e03SGordon Ross 	md_get_uint8(mbp, &rqp->rq_hflags);
427*02d09e03SGordon Ross 	md_get_uint16le(mbp, &rqp->rq_hflags2);
428*02d09e03SGordon Ross 	/* pid_hi(2), signature(8), reserved(2) */
429*02d09e03SGordon Ross 	md_get_mem(mbp, NULL, 12, MB_MSYSTEM);
430*02d09e03SGordon Ross 	md_get_uint16le(mbp, &rqp->rq_tid);
431*02d09e03SGordon Ross 	md_get_uint16le(mbp, NULL);	/* pid_lo */
432*02d09e03SGordon Ross 	md_get_uint16le(mbp, &rqp->rq_uid);
433*02d09e03SGordon Ross 	md_get_uint16le(mbp, &rqp->rq_mid);
434613a2f6bSGordon Ross 
435613a2f6bSGordon Ross 	/*
436613a2f6bSGordon Ross 	 * Figure out the status return.
437613a2f6bSGordon Ross 	 * Caller looks at rq_status.
438613a2f6bSGordon Ross 	 */
439613a2f6bSGordon Ross 	if ((rqp->rq_hflags2 & SMB_FLAGS2_ERR_STATUS) == 0) {
440613a2f6bSGordon Ross 		uint16_t	serr;
441613a2f6bSGordon Ross 		uint8_t		class;
442613a2f6bSGordon Ross 
443613a2f6bSGordon Ross 		class = rqp->rq_status & 0xff;
444613a2f6bSGordon Ross 		serr  = rqp->rq_status >> 16;
445613a2f6bSGordon Ross 		rqp->rq_status = smb_map_doserr(class, serr);
446613a2f6bSGordon Ross 	}
447613a2f6bSGordon Ross 
448613a2f6bSGordon Ross 	return (0);
449613a2f6bSGordon Ross }
450613a2f6bSGordon Ross 
451613a2f6bSGordon Ross /*
452613a2f6bSGordon Ross  * Map old DOS errors (etc.) to NT status codes.
453613a2f6bSGordon Ross  * We probably don't need this anymore, since
454613a2f6bSGordon Ross  * the oldest server we talk to is NT.  But if
455613a2f6bSGordon Ross  * later find we do need this, add support here
456613a2f6bSGordon Ross  * for the DOS errors we care about.
457613a2f6bSGordon Ross  */
458613a2f6bSGordon Ross static uint32_t
459613a2f6bSGordon Ross smb_map_doserr(uint8_t class, uint16_t serr)
460613a2f6bSGordon Ross {
461613a2f6bSGordon Ross 	if (class == 0 && serr == 0)
462613a2f6bSGordon Ross 		return (0);
463613a2f6bSGordon Ross 
464613a2f6bSGordon Ross 	DPRINT("class 0x%x serr 0x%x", (int)class, (int)serr);
465613a2f6bSGordon Ross 	return (NT_STATUS_UNSUCCESSFUL);
466613a2f6bSGordon Ross }
467