14bff34ethurlow/*
24bff34ethurlow * Copyright (c) 2000-2001 Boris Popov
34bff34ethurlow * All rights reserved.
44bff34ethurlow *
54bff34ethurlow * Redistribution and use in source and binary forms, with or without
64bff34ethurlow * modification, are permitted provided that the following conditions
74bff34ethurlow * are met:
84bff34ethurlow * 1. Redistributions of source code must retain the above copyright
94bff34ethurlow *    notice, this list of conditions and the following disclaimer.
104bff34ethurlow * 2. Redistributions in binary form must reproduce the above copyright
114bff34ethurlow *    notice, this list of conditions and the following disclaimer in the
124bff34ethurlow *    documentation and/or other materials provided with the distribution.
134bff34ethurlow * 3. All advertising materials mentioning features or use of this software
144bff34ethurlow *    must display the following acknowledgement:
154bff34ethurlow *    This product includes software developed by Boris Popov.
164bff34ethurlow * 4. Neither the name of the author nor the names of any co-contributors
174bff34ethurlow *    may be used to endorse or promote products derived from this software
184bff34ethurlow *    without specific prior written permission.
194bff34ethurlow *
204bff34ethurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
214bff34ethurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224bff34ethurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234bff34ethurlow * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
244bff34ethurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254bff34ethurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264bff34ethurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274bff34ethurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284bff34ethurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294bff34ethurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304bff34ethurlow * SUCH DAMAGE.
314bff34ethurlow *
324bff34ethurlow * $Id: smb_usr.c,v 1.15 2004/12/13 00:25:18 lindak Exp $
334bff34ethurlow */
344bff34ethurlow
359c9af25Gordon Ross/*
36613a2f6Gordon Ross * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
379c9af25Gordon Ross * Use is subject to license terms.
3840c0e23Gordon Ross *
39e3a966fGordon Ross * Copyright 2019 Nexenta by DDN, Inc. All rights reserved.
409c9af25Gordon Ross */
414bff34ethurlow
424bff34ethurlow#include <sys/param.h>
434bff34ethurlow#include <sys/kmem.h>
444bff34ethurlow#include <sys/systm.h>
454bff34ethurlow#include <sys/policy.h>
464bff34ethurlow#include <sys/conf.h>
474bff34ethurlow#include <sys/proc.h>
484bff34ethurlow#include <sys/fcntl.h>
49613a2f6Gordon Ross#include <sys/file.h>
504bff34ethurlow#include <sys/socket.h>
51613a2f6Gordon Ross#include <sys/sunddi.h>
524bff34ethurlow#include <sys/cmn_err.h>
534bff34ethurlow
544bff34ethurlow#include <netsmb/smb_osdep.h>
554bff34ethurlow
56adee678Gordon Ross#include <smb/winioctl.h>
574bff34ethurlow#include <netsmb/smb.h>
584bff34ethurlow#include <netsmb/smb_conn.h>
594bff34ethurlow#include <netsmb/smb_rq.h>
604bff34ethurlow#include <netsmb/smb_subr.h>
614bff34ethurlow#include <netsmb/smb_dev.h>
624bff34ethurlow
63613a2f6Gordon Rossstatic int smb_cpdatain(struct mbchain *mbp, int len, char *data, int seg);
64613a2f6Gordon Ross
654bff34ethurlow/*
66613a2f6Gordon Ross * Ioctl function for SMBIOC_GETSSNKEY
67613a2f6Gordon Ross * Size copied out is SMBIOC_HASH_SZ.
68613a2f6Gordon Ross *
69613a2f6Gordon Ross * The RPC library needs this for encrypting things
70613a2f6Gordon Ross * like "set password" requests.  This is called
71613a2f6Gordon Ross * with an active RPC binding, so the connection
72613a2f6Gordon Ross * will already be active (but this checks).
734bff34ethurlow */
74613a2f6Gordon Rossint
75613a2f6Gordon Rosssmb_usr_get_ssnkey(smb_dev_t *sdp, intptr_t arg, int flags)
764bff34ethurlow{
77613a2f6Gordon Ross	struct smb_vc *vcp = NULL;
78613a2f6Gordon Ross
79613a2f6Gordon Ross	/* This ioctl requires an active session. */
80613a2f6Gordon Ross	if ((vcp = sdp->sd_vc) == NULL)
81613a2f6Gordon Ross		return (ENOTCONN);
82613a2f6Gordon Ross	if (vcp->vc_state != SMBIOD_ST_VCACTIVE)
83613a2f6Gordon Ross		return (ENOTCONN);
844bff34ethurlow
854bff34ethurlow	/*
86613a2f6Gordon Ross	 * Return the session key.
874bff34ethurlow	 */
8840c0e23Gordon Ross	if (vcp->vc_ssnkey == NULL ||
8940c0e23Gordon Ross	    vcp->vc_ssnkeylen < SMBIOC_HASH_SZ)
9040c0e23Gordon Ross		return (EINVAL);
9140c0e23Gordon Ross	if (ddi_copyout(vcp->vc_ssnkey, (void *)arg,
92613a2f6Gordon Ross	    SMBIOC_HASH_SZ, flags))
93613a2f6Gordon Ross		return (EFAULT);
94613a2f6Gordon Ross
95613a2f6Gordon Ross	return (0);
96613a2f6Gordon Ross}
97613a2f6Gordon Ross
98613a2f6Gordon Ross/*
9940c0e23Gordon Ross * Ioctl function for SMBIOC_XACTNP (transact named pipe)
100613a2f6Gordon Ross */
101613a2f6Gordon Rossint
10240c0e23Gordon Rosssmb_usr_xnp(smb_dev_t *sdp, intptr_t arg, int flags, cred_t *cr)
1034bff34ethurlow{
104613a2f6Gordon Ross	struct smb_cred scred;
105613a2f6Gordon Ross	struct smb_share *ssp;
106adee678Gordon Ross	struct smb_fh *fhp;
10740c0e23Gordon Ross	smbioc_xnp_t *ioc = NULL;
108adee678Gordon Ross	struct mbchain send_mb;
109adee678Gordon Ross	struct mdchain recv_md;
110adee678Gordon Ross	uint32_t rdlen;
111adee678Gordon Ross	int err, mbseg;
112613a2f6Gordon Ross
113adee678Gordon Ross	/* This ioctl requires a file handle. */
114adee678Gordon Ross	if ((fhp = sdp->sd_fh) == NULL)
115adee678Gordon Ross		return (EINVAL);
116adee678Gordon Ross	ssp = FHTOSS(fhp);
117adee678Gordon Ross
118adee678Gordon Ross	/* After reconnect, force close+reopen */
119adee678Gordon Ross	if (fhp->fh_vcgenid != ssp->ss_vcgenid)
120adee678Gordon Ross		return (ESTALE);
121adee678Gordon Ross
122adee678Gordon Ross	bzero(&send_mb, sizeof (send_mb));
123adee678Gordon Ross	bzero(&recv_md, sizeof (recv_md));
1244bff34ethurlow
125613a2f6Gordon Ross	ioc = kmem_alloc(sizeof (*ioc), KM_SLEEP);
126613a2f6Gordon Ross	if (ddi_copyin((void *) arg, ioc, sizeof (*ioc), flags)) {
127613a2f6Gordon Ross		err = EFAULT;
128613a2f6Gordon Ross		goto out;
1294bff34ethurlow	}
130613a2f6Gordon Ross
131430b4c4Gordon Ross	/*
132adee678Gordon Ross	 * Copyin the send data, into an mbchain,
133adee678Gordon Ross	 * save output buffer size.
134430b4c4Gordon Ross	 */
135adee678Gordon Ross	mbseg = (flags & FKIOCTL) ? MB_MSYSTEM : MB_MUSER;
136adee678Gordon Ross	err = smb_cpdatain(&send_mb, ioc->ioc_tdlen, ioc->ioc_tdata, mbseg);
137613a2f6Gordon Ross	if (err)
138613a2f6Gordon Ross		goto out;
139adee678Gordon Ross	rdlen = ioc->ioc_rdlen;
140613a2f6Gordon Ross
141adee678Gordon Ross	/*
142adee678Gordon Ross	 * Run the SMB2 ioctl or SMB1 trans2
143adee678Gordon Ross	 */
144adee678Gordon Ross	smb_credinit(&scred, cr);
145adee678Gordon Ross	if (SSTOVC(ssp)->vc_flags & SMBV_SMB2) {
146adee678Gordon Ross		err = smb2_smb_ioctl(ssp, &fhp->fh_fid2,
147adee678Gordon Ross		    &send_mb, &recv_md, &rdlen,
148adee678Gordon Ross		    FSCTL_PIPE_TRANSCEIVE, &scred);
149adee678Gordon Ross	} else {
150adee678Gordon Ross		err = smb_t2_xnp(ssp, fhp->fh_fid1,
151adee678Gordon Ross		    &send_mb, &recv_md, &rdlen,
152adee678Gordon Ross		    &ioc->ioc_more, &scred);
153adee678Gordon Ross	}
154adee678Gordon Ross	smb_credrele(&scred);
155613a2f6Gordon Ross
156613a2f6Gordon Ross	/* Copyout returned data. */
157adee678Gordon Ross	if (err == 0 && recv_md.md_top != NULL) {
158adee678Gordon Ross		/* User's buffer large enough for copyout? */
159adee678Gordon Ross		size_t len = m_fixhdr(recv_md.md_top);
16040c0e23Gordon Ross		if (len > ioc->ioc_rdlen) {
161613a2f6Gordon Ross			err = EMSGSIZE;
162613a2f6Gordon Ross			goto out;
163613a2f6Gordon Ross		}
164adee678Gordon Ross		err = md_get_mem(&recv_md, ioc->ioc_rdata, len, mbseg);
165613a2f6Gordon Ross		if (err)
166613a2f6Gordon Ross			goto out;
167613a2f6Gordon Ross	} else
16840c0e23Gordon Ross		ioc->ioc_rdlen = 0;
169613a2f6Gordon Ross
170adee678Gordon Ross	/* Tell caller received length */
171adee678Gordon Ross	if (rdlen <= ioc->ioc_rdlen) {
172adee678Gordon Ross		/* Normal case */
173adee678Gordon Ross		ioc->ioc_rdlen = rdlen;
174adee678Gordon Ross	} else {
175adee678Gordon Ross		/* Buffer overlow. Leave ioc_rdlen */
17640c0e23Gordon Ross		ioc->ioc_more = 1;
177adee678Gordon Ross	}
178613a2f6Gordon Ross
17902d09e0Gordon Ross	(void) ddi_copyout(ioc, (void *)arg, sizeof (*ioc), flags);
180613a2f6Gordon Ross
181613a2f6Gordon Rossout:
182430b4c4Gordon Ross	kmem_free(ioc, sizeof (*ioc));
183e3a966fGordon Ross	md_done(&recv_md);
184e3a966fGordon Ross	mb_done(&send_mb);
185613a2f6Gordon Ross
186613a2f6Gordon Ross	return (err);
1874bff34ethurlow}
1884bff34ethurlow
189613a2f6Gordon Ross/* helper for _t2request */
1904bff34ethurlowstatic int
191613a2f6Gordon Rosssmb_cpdatain(struct mbchain *mbp, int len, char *data, int mbseg)
1924bff34ethurlow{
193613a2f6Gordon Ross	int error;
194613a2f6Gordon Ross
195613a2f6Gordon Ross	if (len == 0)
196613a2f6Gordon Ross		return (0);
197613a2f6Gordon Ross	error = mb_init(mbp);
198613a2f6Gordon Ross	if (error)
199613a2f6Gordon Ross		return (error);
200613a2f6Gordon Ross	return (mb_put_mem(mbp, data, len, mbseg));
2014bff34ethurlow}
2024bff34ethurlow
203613a2f6Gordon Ross/*
204613a2f6Gordon Ross * Helper for nsmb_ioctl cases
205613a2f6Gordon Ross * SMBIOC_READ, SMBIOC_WRITE
206613a2f6Gordon Ross */
2074bff34ethurlowint
208613a2f6Gordon Rosssmb_usr_rw(smb_dev_t *sdp, int cmd, intptr_t arg, int flags, cred_t *cr)
2091b34bc4bs{
210613a2f6Gordon Ross	struct smb_cred scred;
211613a2f6Gordon Ross	struct smb_share *ssp;
212adee678Gordon Ross	struct smb_fh *fhp;
213613a2f6Gordon Ross	smbioc_rw_t *ioc = NULL;
214613a2f6Gordon Ross	struct iovec aiov[1];
215613a2f6Gordon Ross	struct uio  auio;
216613a2f6Gordon Ross	int err;
217613a2f6Gordon Ross	uio_rw_t rw;
2181b34bc4bs
219adee678Gordon Ross	/* This ioctl requires a file handle. */
220adee678Gordon Ross	if ((fhp = sdp->sd_fh) == NULL)
221adee678Gordon Ross		return (EINVAL);
222adee678Gordon Ross	ssp = FHTOSS(fhp);
223613a2f6Gordon Ross
224613a2f6Gordon Ross	/* After reconnect, force close+reopen */
225adee678Gordon Ross	if (fhp->fh_vcgenid != ssp->ss_vcgenid)
226613a2f6Gordon Ross		return (ESTALE);
227613a2f6Gordon Ross
228613a2f6Gordon Ross	ioc = kmem_alloc(sizeof (*ioc), KM_SLEEP);
229613a2f6Gordon Ross	if (ddi_copyin((void *) arg, ioc, sizeof (*ioc), flags)) {
230613a2f6Gordon Ross		err = EFAULT;
2311b34bc4bs		goto out;
232613a2f6Gordon Ross	}
233613a2f6Gordon Ross
234613a2f6Gordon Ross	switch (cmd) {
235613a2f6Gordon Ross	case SMBIOC_READ:
236613a2f6Gordon Ross		rw = UIO_READ;
237613a2f6Gordon Ross		break;
238613a2f6Gordon Ross	case SMBIOC_WRITE:
239613a2f6Gordon Ross		rw = UIO_WRITE;
240613a2f6Gordon Ross		break;
241613a2f6Gordon Ross	default:
242613a2f6Gordon Ross		err = ENODEV;
243613a2f6Gordon Ross		goto out;
244613a2f6Gordon Ross	}
245613a2f6Gordon Ross
246613a2f6Gordon Ross	aiov[0].iov_base = ioc->ioc_base;
247613a2f6Gordon Ross	aiov[0].iov_len = (size_t)ioc->ioc_cnt;
248613a2f6Gordon Ross
249613a2f6Gordon Ross	auio.uio_iov = aiov;
250613a2f6Gordon Ross	auio.uio_iovcnt = 1;
251613a2f6Gordon Ross	auio.uio_loffset = ioc->ioc_offset;
252613a2f6Gordon Ross	auio.uio_segflg = (flags & FKIOCTL) ?
253613a2f6Gordon Ross	    UIO_SYSSPACE : UIO_USERSPACE;
254613a2f6Gordon Ross	auio.uio_fmode = 0;
255613a2f6Gordon Ross	auio.uio_resid = (size_t)ioc->ioc_cnt;
256613a2f6Gordon Ross
257adee678Gordon Ross	smb_credinit(&scred, cr);
258adee678Gordon Ross	err = smb_rwuio(fhp, rw, &auio, &scred, 0);
259adee678Gordon Ross	smb_credrele(&scred);
260613a2f6Gordon Ross
261613a2f6Gordon Ross	/*
262613a2f6Gordon Ross	 * On return ioc_cnt holds the
263613a2f6Gordon Ross	 * number of bytes transferred.
264613a2f6Gordon Ross	 */
265613a2f6Gordon Ross	ioc->ioc_cnt -= auio.uio_resid;
266613a2f6Gordon Ross
26702d09e0Gordon Ross	(void) ddi_copyout(ioc, (void *)arg, sizeof (*ioc), flags);
268613a2f6Gordon Ross
2691b34bc4bsout:
270430b4c4Gordon Ross	kmem_free(ioc, sizeof (*ioc));
271430b4c4Gordon Ross
272430b4c4Gordon Ross	return (err);
273430b4c4Gordon Ross}
274430b4c4Gordon Ross
275430b4c4Gordon Ross/*
276430b4c4Gordon Ross * Helper for nsmb_ioctl case
277430b4c4Gordon Ross * SMBIOC_NTCREATE
278430b4c4Gordon Ross */
279430b4c4Gordon Rossint
280430b4c4Gordon Rosssmb_usr_ntcreate(smb_dev_t *sdp, intptr_t arg, int flags, cred_t *cr)
281430b4c4Gordon Ross{
282430b4c4Gordon Ross	struct smb_cred scred;
283430b4c4Gordon Ross	struct mbchain name_mb;
284430b4c4Gordon Ross	struct smb_share *ssp;
285adee678Gordon Ross	struct smb_fh *fhp = NULL;
286430b4c4Gordon Ross	smbioc_ntcreate_t *ioc = NULL;
287430b4c4Gordon Ross	int err, nmlen;
288430b4c4Gordon Ross
289adee678Gordon Ross	mb_init(&name_mb);
290adee678Gordon Ross
291430b4c4Gordon Ross	/* This ioctl requires a share. */
292430b4c4Gordon Ross	if ((ssp = sdp->sd_share) == NULL)
293430b4c4Gordon Ross		return (ENOTCONN);
294430b4c4Gordon Ross
295adee678Gordon Ross	/* Must not already have a file handle. */
296adee678Gordon Ross	if (sdp->sd_fh != NULL)
297430b4c4Gordon Ross		return (EINVAL);
298430b4c4Gordon Ross
299430b4c4Gordon Ross	ioc = kmem_alloc(sizeof (*ioc), KM_SLEEP);
300430b4c4Gordon Ross	if (ddi_copyin((void *) arg, ioc, sizeof (*ioc), flags)) {
301430b4c4Gordon Ross		err = EFAULT;
302430b4c4Gordon Ross		goto out;
303430b4c4Gordon Ross	}
304430b4c4Gordon Ross
305430b4c4Gordon Ross	/* Build name_mb */
306430b4c4Gordon Ross	ioc->ioc_name[SMBIOC_MAX_NAME-1] = '\0';
307430b4c4Gordon Ross	nmlen = strnlen(ioc->ioc_name, SMBIOC_MAX_NAME-1);
308430b4c4Gordon Ross	err = smb_put_dmem(&name_mb, SSTOVC(ssp),
309430b4c4Gordon Ross	    ioc->ioc_name, nmlen,
310430b4c4Gordon Ross	    SMB_CS_NONE, NULL);
311430b4c4Gordon Ross	if (err != 0)
312430b4c4Gordon Ross		goto out;
313430b4c4Gordon Ross
314adee678Gordon Ross	err = smb_fh_create(ssp, &fhp);
315adee678Gordon Ross	if (err != 0)
316adee678Gordon Ross		goto out;
317adee678Gordon Ross
318adee678Gordon Ross	/*
319adee678Gordon Ross	 * Do the OtW open, save the FID.
320adee678Gordon Ross	 */
321adee678Gordon Ross	smb_credinit(&scred, cr);
322430b4c4Gordon Ross	err = smb_smb_ntcreate(ssp, &name_mb,
323430b4c4Gordon Ross	    0,	/* create flags */
324430b4c4Gordon Ross	    ioc->ioc_req_acc,
325430b4c4Gordon Ross	    ioc->ioc_efattr,
326430b4c4Gordon Ross	    ioc->ioc_share_acc,
327430b4c4Gordon Ross	    ioc->ioc_open_disp,
328430b4c4Gordon Ross	    ioc->ioc_creat_opts,
329430b4c4Gordon Ross	    NTCREATEX_IMPERSONATION_IMPERSONATION,
330430b4c4Gordon Ross	    &scred,
331adee678Gordon Ross	    fhp,
332430b4c4Gordon Ross	    NULL,
333430b4c4Gordon Ross	    NULL);
334adee678Gordon Ross	smb_credrele(&scred);
335430b4c4Gordon Ross	if (err != 0)
336430b4c4Gordon Ross		goto out;
337430b4c4Gordon Ross
338adee678Gordon Ross	fhp->fh_rights = ioc->ioc_req_acc;
339adee678Gordon Ross	smb_fh_opened(fhp);
340adee678Gordon Ross	sdp->sd_fh = fhp;
341adee678Gordon Ross	fhp = NULL;
342430b4c4Gordon Ross
343430b4c4Gordon Rossout:
344adee678Gordon Ross	if (fhp != NULL)
345adee678Gordon Ross		smb_fh_rele(fhp);
346430b4c4Gordon Ross	kmem_free(ioc, sizeof (*ioc));
347430b4c4Gordon Ross	mb_done(&name_mb);
348430b4c4Gordon Ross
349430b4c4Gordon Ross	return (err);
350430b4c4Gordon Ross}
351430b4c4Gordon Ross
352430b4c4Gordon Ross/*
353430b4c4Gordon Ross * Helper for nsmb_ioctl case
354430b4c4Gordon Ross * SMBIOC_PRINTJOB
355430b4c4Gordon Ross */
356430b4c4Gordon Rossint
357430b4c4Gordon Rosssmb_usr_printjob(smb_dev_t *sdp, intptr_t arg, int flags, cred_t *cr)
358430b4c4Gordon Ross{
359adee678Gordon Ross	static const char invalid_chars[] = SMB_FILENAME_INVALID_CHARS;
360430b4c4Gordon Ross	struct smb_cred scred;
361adee678Gordon Ross	struct mbchain name_mb;
362430b4c4Gordon Ross	struct smb_share *ssp;
363adee678Gordon Ross	struct smb_fh *fhp = NULL;
364430b4c4Gordon Ross	smbioc_printjob_t *ioc = NULL;
365adee678Gordon Ross	int err, cklen, nmlen;
366adee678Gordon Ross	uint32_t access = SA_RIGHT_FILE_WRITE_DATA |
367adee678Gordon Ross	    SA_RIGHT_FILE_READ_ATTRIBUTES;
368adee678Gordon Ross
369adee678Gordon Ross	mb_init(&name_mb);
370430b4c4Gordon Ross
371430b4c4Gordon Ross	/* This ioctl requires a share. */
372430b4c4Gordon Ross	if ((ssp = sdp->sd_share) == NULL)
373430b4c4Gordon Ross		return (ENOTCONN);
374430b4c4Gordon Ross
375430b4c4Gordon Ross	/* The share must be a print queue. */
376430b4c4Gordon Ross	if (ssp->ss_type != STYPE_PRINTQ)
377430b4c4Gordon Ross		return (EINVAL);
378430b4c4Gordon Ross
379adee678Gordon Ross	/* Must not already have a file handle. */
380adee678Gordon Ross	if (sdp->sd_fh != NULL)
381430b4c4Gordon Ross		return (EINVAL);
382430b4c4Gordon Ross
383430b4c4Gordon Ross	smb_credinit(&scred, cr);
384430b4c4Gordon Ross	ioc = kmem_alloc(sizeof (*ioc), KM_SLEEP);
385430b4c4Gordon Ross	if (ddi_copyin((void *) arg, ioc, sizeof (*ioc), flags)) {
386430b4c4Gordon Ross		err = EFAULT;
387430b4c4Gordon Ross		goto out;
388430b4c4Gordon Ross	}
389adee678Gordon Ross
390adee678Gordon Ross	/*
391adee678Gordon Ross	 * Use the print job title as the file name to open, but
392adee678Gordon Ross	 * check for invalid characters first.  See the notes in
393adee678Gordon Ross	 * libsmbfs/smb/print.c about job name sanitizing.
394adee678Gordon Ross	 */
395430b4c4Gordon Ross	ioc->ioc_title[SMBIOC_MAX_NAME-1] = '\0';
396adee678Gordon Ross	nmlen = strnlen(ioc->ioc_title, SMBIOC_MAX_NAME-1);
397adee678Gordon Ross	cklen = strcspn(ioc->ioc_title, invalid_chars);
398adee678Gordon Ross	if (cklen < nmlen) {
399adee678Gordon Ross		err = EINVAL;
400adee678Gordon Ross		goto out;
401adee678Gordon Ross	}
402430b4c4Gordon Ross
403adee678Gordon Ross	/* Build name_mb */
404adee678Gordon Ross	err = smb_put_dmem(&name_mb, SSTOVC(ssp),
405adee678Gordon Ross	    ioc->ioc_title, nmlen,
406adee678Gordon Ross	    SMB_CS_NONE, NULL);
407430b4c4Gordon Ross	if (err != 0)
408430b4c4Gordon Ross		goto out;
409430b4c4Gordon Ross
410adee678Gordon Ross	err = smb_fh_create(ssp, &fhp);
411adee678Gordon Ross	if (err != 0)
412adee678Gordon Ross		goto out;
413adee678Gordon Ross
414adee678Gordon Ross	/*
415adee678Gordon Ross	 * Do the OtW open, save the FID.
416adee678Gordon Ross	 */
417adee678Gordon Ross	smb_credinit(&scred, cr);
418adee678Gordon Ross	if (SSTOVC(ssp)->vc_flags & SMBV_SMB2) {
419adee678Gordon Ross		err = smb2_smb_ntcreate(ssp, &name_mb,
420adee678Gordon Ross		    NULL, NULL, /* cctx in, out */
421adee678Gordon Ross		    0,	/* create flags */
422adee678Gordon Ross		    access,
423adee678Gordon Ross		    SMB_EFA_NORMAL,
424adee678Gordon Ross		    NTCREATEX_SHARE_ACCESS_NONE,
425adee678Gordon Ross		    NTCREATEX_DISP_CREATE,
426adee678Gordon Ross		    NTCREATEX_OPTIONS_NON_DIRECTORY_FILE,
427adee678Gordon Ross		    NTCREATEX_IMPERSONATION_IMPERSONATION,
428adee678Gordon Ross		    &scred,
429adee678Gordon Ross		    &fhp->fh_fid2,
430adee678Gordon Ross		    NULL,
431adee678Gordon Ross		    NULL);
432adee678Gordon Ross	} else {
433adee678Gordon Ross		err = smb_smb_open_prjob(ssp, ioc->ioc_title,
434adee678Gordon Ross		    ioc->ioc_setuplen, ioc->ioc_prmode,
435adee678Gordon Ross		    &scred, &fhp->fh_fid1);
436adee678Gordon Ross	}
437adee678Gordon Ross	smb_credrele(&scred);
438adee678Gordon Ross	if (err != 0)
439adee678Gordon Ross		goto out;
440adee678Gordon Ross
441adee678Gordon Ross	fhp->fh_rights = access;
442adee678Gordon Ross	smb_fh_opened(fhp);
443adee678Gordon Ross	sdp->sd_fh = fhp;
444adee678Gordon Ross	fhp = NULL;
445430b4c4Gordon Ross
446430b4c4Gordon Rossout:
447adee678Gordon Ross	if (fhp != NULL)
448adee678Gordon Ross		smb_fh_rele(fhp);
449430b4c4Gordon Ross	kmem_free(ioc, sizeof (*ioc));
450adee678Gordon Ross	mb_done(&name_mb);
451430b4c4Gordon Ross
452430b4c4Gordon Ross	return (err);
453430b4c4Gordon Ross}
454430b4c4Gordon Ross
455430b4c4Gordon Ross/*
456430b4c4Gordon Ross * Helper for nsmb_ioctl case
457430b4c4Gordon Ross * SMBIOC_CLOSEFH
458430b4c4Gordon Ross */
459adee678Gordon Ross/*ARGSUSED*/
460430b4c4Gordon Rossint
461430b4c4Gordon Rosssmb_usr_closefh(smb_dev_t *sdp, cred_t *cr)
462430b4c4Gordon Ross{
463adee678Gordon Ross	struct smb_fh *fhp;
464430b4c4Gordon Ross
465adee678Gordon Ross	/* This ioctl requires a file handle. */
466adee678Gordon Ross	if ((fhp = sdp->sd_fh) == NULL)
467adee678Gordon Ross		return (EINVAL);
468adee678Gordon Ross	sdp->sd_fh = NULL;
469430b4c4Gordon Ross
470adee678Gordon Ross	smb_fh_close(fhp);
471adee678Gordon Ross	smb_fh_rele(fhp);
472613a2f6Gordon Ross
473adee678Gordon Ross	return (0);
4741b34bc4bs}
4751b34bc4bs
476613a2f6Gordon Ross/*
477613a2f6Gordon Ross * Ioctl functions: SMBIOC_SSN_FIND, SMBIOC_SSN_CREATE
478613a2f6Gordon Ross * Find or create a session (a.k.a. "VC" in here)
479613a2f6Gordon Ross */
4801b34bc4bsint
481613a2f6Gordon Rosssmb_usr_get_ssn(smb_dev_t *sdp, int cmd, intptr_t arg, int flags, cred_t *cr)
4824bff34ethurlow{
483613a2f6Gordon Ross	struct smb_cred scred;
484613a2f6Gordon Ross	smbioc_ossn_t *ossn = NULL;
4854bff34ethurlow	struct smb_vc *vcp = NULL;
4864bff34ethurlow	int error = 0;
487613a2f6Gordon Ross	uid_t realuid;
4884bff34ethurlow
489613a2f6Gordon Ross	/* Should be no VC */
490613a2f6Gordon Ross	if (sdp->sd_vc != NULL)
491613a2f6Gordon Ross		return (EISCONN);
492613a2f6Gordon Ross
493613a2f6Gordon Ross	smb_credinit(&scred, cr);
494613a2f6Gordon Ross	ossn = kmem_alloc(sizeof (*ossn), KM_SLEEP);
495613a2f6Gordon Ross	if (ddi_copyin((void *)arg, ossn, sizeof (*ossn), flags)) {
496613a2f6Gordon Ross		error = EFAULT;
497613a2f6Gordon Ross		goto out;
4984bff34ethurlow	}
499613a2f6Gordon Ross
500613a2f6Gordon Ross	/*
501613a2f6Gordon Ross	 * Only superuser can specify a UID or GID.
502613a2f6Gordon Ross	 */
503613a2f6Gordon Ross	realuid = crgetruid(cr);
504613a2f6Gordon Ross	if (ossn->ssn_owner == SMBM_ANY_OWNER)
505613a2f6Gordon Ross		ossn->ssn_owner = realuid;
506613a2f6Gordon Ross	else {
5074bff34ethurlow		/*
508613a2f6Gordon Ross		 * Do we have the privilege to create with the
509613a2f6Gordon Ross		 * specified uid?  (does uid == cr->cr_uid, etc.)
5104bff34ethurlow		 */
511613a2f6Gordon Ross		if (secpolicy_vnode_owner(cr, ossn->ssn_owner)) {
512613a2f6Gordon Ross			error = EPERM;
513613a2f6Gordon Ross			goto out;
5144bff34ethurlow		}
515613a2f6Gordon Ross		/* ossn->ssn_owner is OK */
5164bff34ethurlow	}
5174bff34ethurlow
518613a2f6Gordon Ross	/*
519613a2f6Gordon Ross	 * Make sure the strings are null terminated.
520613a2f6Gordon Ross	 */
521613a2f6Gordon Ross	ossn->ssn_srvname[SMBIOC_MAX_NAME-1] = '\0';
522613a2f6Gordon Ross	ossn->ssn_id.id_domain[ SMBIOC_MAX_NAME-1] = '\0';
523613a2f6Gordon Ross	ossn->ssn_id.id_user[   SMBIOC_MAX_NAME-1] = '\0';
5244bff34ethurlow
525613a2f6Gordon Ross	if (cmd == SMBIOC_SSN_CREATE)
526613a2f6Gordon Ross		ossn->ssn_vopt |= SMBVOPT_CREATE;
527613a2f6Gordon Ross	else /* FIND */
528613a2f6Gordon Ross		ossn->ssn_vopt &= ~SMBVOPT_CREATE;
5294bff34ethurlow
530613a2f6Gordon Ross	error = smb_vc_findcreate(ossn, &scred, &vcp);
5314bff34ethurlow	if (error)
5324bff34ethurlow		goto out;
533613a2f6Gordon Ross	ASSERT(vcp != NULL);
5344bff34ethurlow
5354bff34ethurlow	/*
536613a2f6Gordon Ross	 * We have a VC, held, but not locked.
537613a2f6Gordon Ross	 * If we're creating, mark this instance as
538613a2f6Gordon Ross	 * an open from IOD so close can do cleanup.
539613a2f6Gordon Ross	 *
540613a2f6Gordon Ross	 * XXX: Would be nice to have a back pointer
541613a2f6Gordon Ross	 * from the VC to this (IOD) sdp instance.
5424bff34ethurlow	 */
543613a2f6Gordon Ross	if (cmd == SMBIOC_SSN_CREATE) {
544613a2f6Gordon Ross		if (vcp->iod_thr != NULL) {
545613a2f6Gordon Ross			error = EEXIST;
546613a2f6Gordon Ross			goto out;
547613a2f6Gordon Ross		}
548613a2f6Gordon Ross		sdp->sd_flags |= NSMBFL_IOD;
549613a2f6Gordon Ross	} else {
550613a2f6Gordon Ross		/*
551613a2f6Gordon Ross		 * Wait for it to finish connecting
552613a2f6Gordon Ross		 * (or reconnect) if necessary.
553613a2f6Gordon Ross		 */
554613a2f6Gordon Ross		if (vcp->vc_state != SMBIOD_ST_VCACTIVE) {
555613a2f6Gordon Ross			error = smb_iod_reconnect(vcp);
556613a2f6Gordon Ross			if (error != 0)
557613a2f6Gordon Ross				goto out;
558613a2f6Gordon Ross		}
559613a2f6Gordon Ross	}
560613a2f6Gordon Ross
561613a2f6Gordon Ross	/*
562613a2f6Gordon Ross	 * The VC has a hold from _findvc
563613a2f6Gordon Ross	 * which we keep until _SSN_RELE
564613a2f6Gordon Ross	 * or nsmb_close().
565613a2f6Gordon Ross	 */
566613a2f6Gordon Ross	sdp->sd_level = SMBL_VC;
567613a2f6Gordon Ross	sdp->sd_vc = vcp;
568613a2f6Gordon Ross	vcp = NULL;
569613a2f6Gordon Ross	(void) ddi_copyout(ossn, (void *)arg, sizeof (*ossn), flags);
5704bff34ethurlow
5714bff34ethurlowout:
572613a2f6Gordon Ross	if (vcp) {
573613a2f6Gordon Ross		/* Error path: rele hold from _findcreate */
574613a2f6Gordon Ross		smb_vc_rele(vcp);
575613a2f6Gordon Ross	}
576430b4c4Gordon Ross	kmem_free(ossn, sizeof (*ossn));
577613a2f6Gordon Ross	smb_credrele(&scred);
578613a2f6Gordon Ross
5794bff34ethurlow	return (error);
5804bff34ethurlow}
5814bff34ethurlow
582613a2f6Gordon Ross/*
583613a2f6Gordon Ross * Ioctl functions: SMBIOC_SSN_RELE, SMBIOC_SSN_KILL
584613a2f6Gordon Ross * Release or kill the current session.
585613a2f6Gordon Ross */
5864bff34ethurlowint
587613a2f6Gordon Rosssmb_usr_drop_ssn(smb_dev_t *sdp, int cmd)
5884bff34ethurlow{
589613a2f6Gordon Ross	struct smb_vc *vcp = NULL;
5904bff34ethurlow
591613a2f6Gordon Ross	/* Must have a VC. */
592613a2f6Gordon Ross	if ((vcp = sdp->sd_vc) == NULL)
593613a2f6Gordon Ross		return (ENOTCONN);
5944bff34ethurlow
595613a2f6Gordon Ross	/* If we have a share ref, drop it too. */
596613a2f6Gordon Ross	if (sdp->sd_share) {
597613a2f6Gordon Ross		smb_share_rele(sdp->sd_share);
598613a2f6Gordon Ross		sdp->sd_share = NULL;
599613a2f6Gordon Ross		sdp->sd_level = SMBL_VC;
6004bff34ethurlow	}
6014bff34ethurlow
602613a2f6Gordon Ross	if (cmd == SMBIOC_SSN_KILL)
603613a2f6Gordon Ross		smb_vc_kill(vcp);
6044bff34ethurlow
605613a2f6Gordon Ross	/* Drop the VC ref. */
606613a2f6Gordon Ross	smb_vc_rele(vcp);
607613a2f6Gordon Ross	sdp->sd_vc = NULL;
608613a2f6Gordon Ross	sdp->sd_level = 0;
609613a2f6Gordon Ross
610613a2f6Gordon Ross	return (0);
6114bff34ethurlow}
6124bff34ethurlow
6134bff34ethurlow/*
614613a2f6Gordon Ross * Find or create a tree (connected share)
6154bff34ethurlow */
6164bff34ethurlowint
617613a2f6Gordon Rosssmb_usr_get_tree(smb_dev_t *sdp, int cmd, intptr_t arg, int flags, cred_t *cr)
6184bff34ethurlow{
619613a2f6Gordon Ross	struct smb_cred scred;
620613a2f6Gordon Ross	smbioc_tcon_t *tcon = NULL;
621613a2f6Gordon Ross	struct smb_vc *vcp = NULL;
622613a2f6Gordon Ross	struct smb_share *ssp = NULL;
623613a2f6Gordon Ross	int error = 0;
6244bff34ethurlow
625613a2f6Gordon Ross	/* Must have a VC. */
626613a2f6Gordon Ross	if ((vcp = sdp->sd_vc) == NULL)
627613a2f6Gordon Ross		return (ENOTCONN);
628613a2f6Gordon Ross	/* Should not have a share. */
629613a2f6Gordon Ross	if (sdp->sd_share != NULL)
630613a2f6Gordon Ross		return (EISCONN);
631613a2f6Gordon Ross
632613a2f6Gordon Ross	smb_credinit(&scred, cr);
633613a2f6Gordon Ross	tcon = kmem_alloc(sizeof (*tcon), KM_SLEEP);
634613a2f6Gordon Ross	if (ddi_copyin((void *)arg, tcon, sizeof (*tcon), flags)) {
635613a2f6Gordon Ross		error = EFAULT;
636613a2f6Gordon Ross		goto out;
6374bff34ethurlow	}
638613a2f6Gordon Ross
639613a2f6Gordon Ross	/*
640613a2f6Gordon Ross	 * Make sure the strings are null terminated.
641613a2f6Gordon Ross	 */
642613a2f6Gordon Ross	tcon->tc_sh.sh_name[SMBIOC_MAX_NAME-1] = '\0';
643613a2f6Gordon Ross	tcon->tc_sh.sh_pass[SMBIOC_MAX_NAME-1] = '\0';
644613a2f6Gordon Ross
645613a2f6Gordon Ross	if (cmd == SMBIOC_TREE_CONNECT)
646613a2f6Gordon Ross		tcon->tc_opt |= SMBSOPT_CREATE;
647613a2f6Gordon Ross	else /* FIND */
648613a2f6Gordon Ross		tcon->tc_opt &= ~SMBSOPT_CREATE;
649613a2f6Gordon Ross
650613a2f6Gordon Ross	error = smb_share_findcreate(tcon, vcp, &ssp, &scred);
6514bff34ethurlow	if (error)
652613a2f6Gordon Ross		goto out;
653613a2f6Gordon Ross	ASSERT(ssp != NULL);
654613a2f6Gordon Ross
655613a2f6Gordon Ross	/*
656613a2f6Gordon Ross	 * We have a share, held, but not locked.
657613a2f6Gordon Ross	 * If we're creating, do tree connect now,
658613a2f6Gordon Ross	 * otherwise let that wait for a request.
659613a2f6Gordon Ross	 */
660613a2f6Gordon Ross	if (cmd == SMBIOC_TREE_CONNECT) {
661613a2f6Gordon Ross		error = smb_share_tcon(ssp, &scred);
662613a2f6Gordon Ross		if (error)
663613a2f6Gordon Ross			goto out;
6644bff34ethurlow	}
665613a2f6Gordon Ross
666613a2f6Gordon Ross	/*
667613a2f6Gordon Ross	 * Give caller the real share type from
668613a2f6Gordon Ross	 * the tree connect response, so they can
669613a2f6Gordon Ross	 * see if they got the requested type.
670613a2f6Gordon Ross	 */
671430b4c4Gordon Ross	tcon->tc_sh.sh_type = ssp->ss_type;
672613a2f6Gordon Ross
673613a2f6Gordon Ross	/*
674613a2f6Gordon Ross	 * The share has a hold from _tcon
675613a2f6Gordon Ross	 * which we keep until nsmb_close()
676613a2f6Gordon Ross	 * or the SMBIOC_TDIS below.
677613a2f6Gordon Ross	 */
678613a2f6Gordon Ross	sdp->sd_level = SMBL_SHARE;
679613a2f6Gordon Ross	sdp->sd_share = ssp;
680613a2f6Gordon Ross	ssp = NULL;
681613a2f6Gordon Ross	(void) ddi_copyout(tcon, (void *)arg, sizeof (*tcon), flags);
682613a2f6Gordon Ross
683613a2f6Gordon Rossout:
684613a2f6Gordon Ross	if (ssp) {
685613a2f6Gordon Ross		/* Error path: rele hold from _findcreate */
686613a2f6Gordon Ross		smb_share_rele(ssp);
6874bff34ethurlow	}
688430b4c4Gordon Ross	/*
689430b4c4Gordon Ross	 * This structure may contain a
690430b4c4Gordon Ross	 * cleartext password, so zap it.
691430b4c4Gordon Ross	 */
692430b4c4Gordon Ross	bzero(tcon, sizeof (*tcon));
693430b4c4Gordon Ross	kmem_free(tcon, sizeof (*tcon));
694613a2f6Gordon Ross	smb_credrele(&scred);
6954bff34ethurlow
696613a2f6Gordon Ross	return (error);
6974bff34ethurlow}
6984bff34ethurlow
699613a2f6Gordon Ross/*
700613a2f6Gordon Ross * Ioctl functions: SMBIOC_TREE_RELE, SMBIOC_TREE_KILL
701613a2f6Gordon Ross * Release or kill the current tree
702613a2f6Gordon Ross */
703613a2f6Gordon Rossint
704613a2f6Gordon Rosssmb_usr_drop_tree(smb_dev_t *sdp, int cmd)
7054bff34ethurlow{
706613a2f6Gordon Ross	struct smb_share *ssp = NULL;
7074bff34ethurlow
708613a2f6Gordon Ross	/* Must have a VC and a share. */
709613a2f6Gordon Ross	if (sdp->sd_vc == NULL)
710613a2f6Gordon Ross		return (ENOTCONN);
711613a2f6Gordon Ross	if ((ssp = sdp->sd_share) == NULL)
712613a2f6Gordon Ross		return (ENOTCONN);
713613a2f6Gordon Ross
714613a2f6Gordon Ross	if (cmd == SMBIOC_TREE_KILL)
715613a2f6Gordon Ross		smb_share_kill(ssp);
716613a2f6Gordon Ross
717613a2f6Gordon Ross	/* Drop the share ref. */
718613a2f6Gordon Ross	smb_share_rele(sdp->sd_share);
719613a2f6Gordon Ross	sdp->sd_share = NULL;
720613a2f6Gordon Ross	sdp->sd_level = SMBL_VC;
721613a2f6Gordon Ross
722613a2f6Gordon Ross	return (0);
7234bff34ethurlow}
7244bff34ethurlow
725613a2f6Gordon Ross/*
72640c0e23Gordon Ross * Ioctl handler for all SMBIOC_IOD_...
727613a2f6Gordon Ross */
7284bff34ethurlowint
72940c0e23Gordon Rosssmb_usr_iod_ioctl(smb_dev_t *sdp, int cmd, intptr_t arg, int flags, cred_t *cr)
7304bff34ethurlow{
73140c0e23Gordon Ross	struct smb_vc *vcp;
732613a2f6Gordon Ross	int err = 0;
7334bff34ethurlow
73440c0e23Gordon Ross	/* Must be the IOD. */
73540c0e23Gordon Ross	if ((sdp->sd_flags & NSMBFL_IOD) == 0)
73640c0e23Gordon Ross		return (EINVAL);
73740c0e23Gordon Ross	/* Must have a VC and no share. */
738613a2f6Gordon Ross	if ((vcp = sdp->sd_vc) == NULL)
7394bff34ethurlow		return (EINVAL);
74040c0e23Gordon Ross	if (sdp->sd_share != NULL)
741613a2f6Gordon Ross		return (EINVAL);
742613a2f6Gordon Ross
743613a2f6Gordon Ross	/*
744613a2f6Gordon Ross	 * Is there already an IOD for this VC?
745613a2f6Gordon Ross	 * (Should never happen.)
746613a2f6Gordon Ross	 */
747613a2f6Gordon Ross	SMB_VC_LOCK(vcp);
748613a2f6Gordon Ross	if (vcp->iod_thr == NULL)
749613a2f6Gordon Ross		vcp->iod_thr = curthread;
750613a2f6Gordon Ross	else
751613a2f6Gordon Ross		err = EEXIST;
752613a2f6Gordon Ross	SMB_VC_UNLOCK(vcp);
753613a2f6Gordon Ross	if (err)
754613a2f6Gordon Ross		return (err);
755613a2f6Gordon Ross
756613a2f6Gordon Ross	/*
75740c0e23Gordon Ross	 * Copy the "work" state, etc. into the VC,
75840c0e23Gordon Ross	 * and back to the caller on the way out.
75940c0e23Gordon Ross	 * Clear the "out only" part.
760613a2f6Gordon Ross	 */
761613a2f6Gordon Ross	if (ddi_copyin((void *)arg, &vcp->vc_work,
762613a2f6Gordon Ross	    sizeof (smbioc_ssn_work_t), flags)) {
763613a2f6Gordon Ross		err = EFAULT;
764613a2f6Gordon Ross		goto out;
7654bff34ethurlow	}
76640c0e23Gordon Ross	vcp->vc_work.wk_out_state = 0;
767613a2f6Gordon Ross
76840c0e23Gordon Ross	switch (cmd) {
769613a2f6Gordon Ross
77040c0e23Gordon Ross	case SMBIOC_IOD_CONNECT:
771adee678Gordon Ross		err = nsmb_iod_connect(vcp, cr);
77240c0e23Gordon Ross		break;
773613a2f6Gordon Ross
77440c0e23Gordon Ross	case SMBIOC_IOD_NEGOTIATE:
77540c0e23Gordon Ross		err = nsmb_iod_negotiate(vcp, cr);
77640c0e23Gordon Ross		break;
777613a2f6Gordon Ross
77840c0e23Gordon Ross	case SMBIOC_IOD_SSNSETUP:
77940c0e23Gordon Ross		err = nsmb_iod_ssnsetup(vcp, cr);
78040c0e23Gordon Ross		break;
78140c0e23Gordon Ross
78240c0e23Gordon Ross	case SMBIOC_IOD_WORK:
78340c0e23Gordon Ross		err = smb_iod_vc_work(vcp, flags, cr);
78440c0e23Gordon Ross		break;
78540c0e23Gordon Ross
78640c0e23Gordon Ross	case SMBIOC_IOD_IDLE:
78740c0e23Gordon Ross		err = smb_iod_vc_idle(vcp);
78840c0e23Gordon Ross		break;
78940c0e23Gordon Ross
79040c0e23Gordon Ross	case SMBIOC_IOD_RCFAIL:
79140c0e23Gordon Ross		err = smb_iod_vc_rcfail(vcp);
79240c0e23Gordon Ross		break;
79340c0e23Gordon Ross
79440c0e23Gordon Ross	default:
79540c0e23Gordon Ross		err = ENOTTY;
79640c0e23Gordon Ross		break;
797613a2f6Gordon Ross	}
798613a2f6Gordon Ross
79940c0e23Gordon Rossout:
80040c0e23Gordon Ross	vcp->vc_work.wk_out_state = vcp->vc_state;
80140c0e23Gordon Ross	(void) ddi_copyout(&vcp->vc_work, (void *)arg,
80240c0e23Gordon Ross	    sizeof (smbioc_ssn_work_t), flags);
80340c0e23Gordon Ross
804613a2f6Gordon Ross	/*
805613a2f6Gordon Ross	 * The IOD thread is leaving the driver.  Clear iod_thr,
806613a2f6Gordon Ross	 * and wake up anybody waiting for us to quit.
807613a2f6Gordon Ross	 */
808613a2f6Gordon Ross	SMB_VC_LOCK(vcp);
809613a2f6Gordon Ross	vcp->iod_thr = NULL;
810613a2f6Gordon Ross	cv_broadcast(&vcp->vc_statechg);
811613a2f6Gordon Ross	SMB_VC_UNLOCK(vcp);
812613a2f6Gordon Ross
813613a2f6Gordon Ross	return (err);
8144bff34ethurlow}
8154bff34ethurlow
8164bff34ethurlowint
81740c0e23Gordon Rosssmb_usr_ioctl(smb_dev_t *sdp, int cmd, intptr_t arg, int flags, cred_t *cr)
8184bff34ethurlow{
81940c0e23Gordon Ross	int err;
820613a2f6Gordon Ross
821613a2f6Gordon Ross	/*
82240c0e23Gordon Ross	 * Serialize ioctl calls.  The smb_usr_... functions
82340c0e23Gordon Ross	 * don't expect concurrent calls on a given sdp.
824613a2f6Gordon Ross	 */
82540c0e23Gordon Ross	mutex_enter(&sdp->sd_lock);
82640c0e23Gordon Ross	if ((sdp->sd_flags & NSMBFL_IOCTL) != 0) {
82740c0e23Gordon Ross		mutex_exit(&sdp->sd_lock);
82840c0e23Gordon Ross		return (EBUSY);
82940c0e23Gordon Ross	}
83040c0e23Gordon Ross	sdp->sd_flags |= NSMBFL_IOCTL;
83140c0e23Gordon Ross	mutex_exit(&sdp->sd_lock);
8324bff34ethurlow
83340c0e23Gordon Ross	err = 0;
8344bff34ethurlow	switch (cmd) {
83540c0e23Gordon Ross	case SMBIOC_GETVERS:
83640c0e23Gordon Ross		(void) ddi_copyout(&nsmb_version, (void *)arg,
83740c0e23Gordon Ross		    sizeof (nsmb_version), flags);
83840c0e23Gordon Ross		break;
83940c0e23Gordon Ross
84040c0e23Gordon Ross	case SMBIOC_GETSSNKEY:
84140c0e23Gordon Ross		err = smb_usr_get_ssnkey(sdp, arg, flags);
84240c0e23Gordon Ross		break;
84340c0e23Gordon Ross
84440c0e23Gordon Ross	case SMBIOC_DUP_DEV:
84540c0e23Gordon Ross		err = smb_usr_dup_dev(sdp, arg, flags);
84640c0e23Gordon Ross		break;
84740c0e23Gordon Ross
84840c0e23Gordon Ross	case SMBIOC_XACTNP:
84940c0e23Gordon Ross		err = smb_usr_xnp(sdp, arg, flags, cr);
85040c0e23Gordon Ross		break;
85140c0e23Gordon Ross
85240c0e23Gordon Ross	case SMBIOC_READ:
85340c0e23Gordon Ross	case SMBIOC_WRITE:
85440c0e23Gordon Ross		err = smb_usr_rw(sdp, cmd, arg, flags, cr);
85540c0e23Gordon Ross		break;
85640c0e23Gordon Ross
85740c0e23Gordon Ross	case SMBIOC_NTCREATE:
85840c0e23Gordon Ross		err = smb_usr_ntcreate(sdp, arg, flags, cr);
85940c0e23Gordon Ross		break;
86040c0e23Gordon Ross
86140c0e23Gordon Ross	case SMBIOC_PRINTJOB:
86240c0e23Gordon Ross		err = smb_usr_printjob(sdp, arg, flags, cr);
8634bff34ethurlow		break;
864613a2f6Gordon Ross
86540c0e23Gordon Ross	case SMBIOC_CLOSEFH:
86640c0e23Gordon Ross		err = smb_usr_closefh(sdp, cr);
86740c0e23Gordon Ross		break;
86840c0e23Gordon Ross
86940c0e23Gordon Ross	case SMBIOC_SSN_CREATE:
87040c0e23Gordon Ross	case SMBIOC_SSN_FIND:
87140c0e23Gordon Ross		err = smb_usr_get_ssn(sdp, cmd, arg, flags, cr);
87240c0e23Gordon Ross		break;
87340c0e23Gordon Ross
87440c0e23Gordon Ross	case SMBIOC_SSN_KILL:
87540c0e23Gordon Ross	case SMBIOC_SSN_RELE:
87640c0e23Gordon Ross		err = smb_usr_drop_ssn(sdp, cmd);
87740c0e23Gordon Ross		break;
87840c0e23Gordon Ross
87940c0e23Gordon Ross	case SMBIOC_TREE_CONNECT:
88040c0e23Gordon Ross	case SMBIOC_TREE_FIND:
88140c0e23Gordon Ross		err = smb_usr_get_tree(sdp, cmd, arg, flags, cr);
88240c0e23Gordon Ross		break;
88340c0e23Gordon Ross
88440c0e23Gordon Ross	case SMBIOC_TREE_KILL:
88540c0e23Gordon Ross	case SMBIOC_TREE_RELE:
88640c0e23Gordon Ross		err = smb_usr_drop_tree(sdp, cmd);
88740c0e23Gordon Ross		break;
88840c0e23Gordon Ross
88940c0e23Gordon Ross	case SMBIOC_IOD_CONNECT:
89040c0e23Gordon Ross	case SMBIOC_IOD_NEGOTIATE:
89140c0e23Gordon Ross	case SMBIOC_IOD_SSNSETUP:
89240c0e23Gordon Ross	case SMBIOC_IOD_WORK:
89340c0e23Gordon Ross	case SMBIOC_IOD_IDLE:
894613a2f6Gordon Ross	case SMBIOC_IOD_RCFAIL:
89540c0e23Gordon Ross		err = smb_usr_iod_ioctl(sdp, cmd, arg, flags, cr);
89640c0e23Gordon Ross		break;
89740c0e23Gordon Ross
89840c0e23Gordon Ross	case SMBIOC_PK_ADD:
89940c0e23Gordon Ross	case SMBIOC_PK_DEL:
90040c0e23Gordon Ross	case SMBIOC_PK_CHK:
90140c0e23Gordon Ross	case SMBIOC_PK_DEL_OWNER:
90240c0e23Gordon Ross	case SMBIOC_PK_DEL_EVERYONE:
90340c0e23Gordon Ross		err = smb_pkey_ioctl(cmd, arg, flags, cr);
9044bff34ethurlow		break;
905613a2f6Gordon Ross
9064bff34ethurlow	default:
907613a2f6Gordon Ross		err = ENOTTY;
90840c0e23Gordon Ross		break;
9094bff34ethurlow	}
9104bff34ethurlow
91140c0e23Gordon Ross	mutex_enter(&sdp->sd_lock);
91240c0e23Gordon Ross	sdp->sd_flags &= ~NSMBFL_IOCTL;
91340c0e23Gordon Ross	mutex_exit(&sdp->sd_lock);
9144bff34ethurlow
915613a2f6Gordon Ross	return (err);
9164bff34ethurlow}
917