xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_opipe.c (revision 94047d49916b669576decf2f622a1ee718646882)
13db3f65cSamw /*
23db3f65cSamw  * CDDL HEADER START
33db3f65cSamw  *
43db3f65cSamw  * The contents of this file are subject to the terms of the
53db3f65cSamw  * Common Development and Distribution License (the "License").
63db3f65cSamw  * You may not use this file except in compliance with the License.
73db3f65cSamw  *
83db3f65cSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93db3f65cSamw  * or http://www.opensolaris.org/os/licensing.
103db3f65cSamw  * See the License for the specific language governing permissions
113db3f65cSamw  * and limitations under the License.
123db3f65cSamw  *
133db3f65cSamw  * When distributing Covered Code, include this CDDL HEADER in each
143db3f65cSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153db3f65cSamw  * If applicable, add the following below this CDDL HEADER, with the
163db3f65cSamw  * fields enclosed by brackets "[]" replaced with your own identifying
173db3f65cSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
183db3f65cSamw  *
193db3f65cSamw  * CDDL HEADER END
203db3f65cSamw  */
213db3f65cSamw /*
22148c5f43SAlan Wright  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23adee6784SGordon Ross  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
243db3f65cSamw  */
253db3f65cSamw 
263db3f65cSamw /*
273db3f65cSamw  * This module provides the interface to NDR RPC.
283db3f65cSamw  */
293db3f65cSamw 
303db3f65cSamw #include <sys/stat.h>
313db3f65cSamw #include <sys/uio.h>
323db3f65cSamw #include <sys/ksynch.h>
3368b2bbf2SGordon Ross #include <sys/stropts.h>
3468b2bbf2SGordon Ross #include <sys/socket.h>
3568b2bbf2SGordon Ross #include <sys/filio.h>
36bbf6f00cSJordan Brown #include <smbsrv/smb_kproto.h>
373db3f65cSamw #include <smbsrv/smb_xdr.h>
38adee6784SGordon Ross #include <smb/winioctl.h>
39a90cf9f2SGordon Ross 
40a90cf9f2SGordon Ross static uint32_t smb_opipe_transceive(smb_request_t *, smb_fsctl_t *);
413db3f65cSamw 
4268b2bbf2SGordon Ross /*
4368b2bbf2SGordon Ross  * Allocate a new opipe and return it, or NULL, in which case
4468b2bbf2SGordon Ross  * the caller will report "internal error".
4568b2bbf2SGordon Ross  */
4668b2bbf2SGordon Ross static smb_opipe_t *
4768b2bbf2SGordon Ross smb_opipe_alloc(smb_request_t *sr)
489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
4968b2bbf2SGordon Ross 	smb_server_t	*sv = sr->sr_server;
509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_opipe_t	*opipe;
5168b2bbf2SGordon Ross 	ksocket_t	sock;
5268b2bbf2SGordon Ross 
5368b2bbf2SGordon Ross 	if (ksocket_socket(&sock, AF_UNIX, SOCK_STREAM, 0,
5468b2bbf2SGordon Ross 	    KSOCKET_SLEEP, sr->user_cr) != 0)
5568b2bbf2SGordon Ross 		return (NULL);
569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
578622ec45SGordon Ross 	opipe = kmem_cache_alloc(smb_cache_opipe, KM_SLEEP);
589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
599fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	bzero(opipe, sizeof (smb_opipe_t));
609fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_init(&opipe->p_mutex, NULL, MUTEX_DEFAULT, NULL);
619fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	cv_init(&opipe->p_cv, NULL, CV_DEFAULT, NULL);
629fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	opipe->p_magic = SMB_OPIPE_MAGIC;
639fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	opipe->p_server = sv;
6468b2bbf2SGordon Ross 	opipe->p_refcnt = 1;
6568b2bbf2SGordon Ross 	opipe->p_socket = sock;
669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	return (opipe);
689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
7068b2bbf2SGordon Ross /*
7168b2bbf2SGordon Ross  * Destroy an opipe.  This is normally called from smb_ofile_delete
7268b2bbf2SGordon Ross  * when the ofile has no more references and is about to be free'd.
7368b2bbf2SGordon Ross  * This is also called here in error handling code paths, before
7468b2bbf2SGordon Ross  * the opipe is installed under an ofile.
7568b2bbf2SGordon Ross  */
769fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States void
779fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_opipe_dealloc(smb_opipe_t *opipe)
789fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
799fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_server_t *sv;
809fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
819fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_OPIPE_VALID(opipe);
829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	sv = opipe->p_server;
839fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_SERVER_VALID(sv);
849fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
8568b2bbf2SGordon Ross 	/*
8668b2bbf2SGordon Ross 	 * This is called in the error path when opening,
8768b2bbf2SGordon Ross 	 * in which case we close the socket here.
8868b2bbf2SGordon Ross 	 */
8968b2bbf2SGordon Ross 	if (opipe->p_socket != NULL)
9068b2bbf2SGordon Ross 		(void) ksocket_close(opipe->p_socket, zone_kcred());
919fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
929fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	opipe->p_magic = (uint32_t)~SMB_OPIPE_MAGIC;
939fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	cv_destroy(&opipe->p_cv);
949fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_destroy(&opipe->p_mutex);
959fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
968622ec45SGordon Ross 	kmem_cache_free(smb_cache_opipe, opipe);
979fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
989fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
993db3f65cSamw /*
100b210fedeSGordon Ross  * Unblock a request that might be blocked reading some
101b210fedeSGordon Ross  * pipe (AF_UNIX socket).  We don't have an easy way to
102b210fedeSGordon Ross  * interrupt just the thread servicing this request, so
103b210fedeSGordon Ross  * we shutdown(3socket) the socket, waking all readers.
104b210fedeSGordon Ross  * That's a bit heavy-handed, making the socket unusable
105b210fedeSGordon Ross  * after this, so we do this only when disconnecting a
106b210fedeSGordon Ross  * session (i.e. stopping the SMB service), and not when
107b210fedeSGordon Ross  * handling an SMB2_cancel or SMB_nt_cancel request.
108b210fedeSGordon Ross  */
109b210fedeSGordon Ross static void
110b210fedeSGordon Ross smb_opipe_cancel(smb_request_t *sr)
111b210fedeSGordon Ross {
112b210fedeSGordon Ross 	ksocket_t so;
113b210fedeSGordon Ross 
114811599a4SMatt Barden 	switch (sr->session->s_state) {
115811599a4SMatt Barden 	case SMB_SESSION_STATE_DISCONNECTED:
116811599a4SMatt Barden 	case SMB_SESSION_STATE_TERMINATED:
117811599a4SMatt Barden 		if ((so = sr->cancel_arg2) != NULL)
118b210fedeSGordon Ross 			(void) ksocket_shutdown(so, SHUT_RDWR, sr->user_cr);
119811599a4SMatt Barden 		break;
120b210fedeSGordon Ross 	}
121b210fedeSGordon Ross }
122b210fedeSGordon Ross 
123b210fedeSGordon Ross /*
12468b2bbf2SGordon Ross  * Helper for open: build pipe name and connect.
12568b2bbf2SGordon Ross  */
12668b2bbf2SGordon Ross static int
12768b2bbf2SGordon Ross smb_opipe_connect(smb_request_t *sr, smb_opipe_t *opipe)
12868b2bbf2SGordon Ross {
12968b2bbf2SGordon Ross 	struct sockaddr_un saddr;
13068b2bbf2SGordon Ross 	smb_arg_open_t	*op = &sr->sr_open;
13168b2bbf2SGordon Ross 	const char *name;
13268b2bbf2SGordon Ross 	int rc;
13368b2bbf2SGordon Ross 
13468b2bbf2SGordon Ross 	name = op->fqi.fq_path.pn_path;
13568b2bbf2SGordon Ross 	name += strspn(name, "\\");
13668b2bbf2SGordon Ross 	if (smb_strcasecmp(name, "PIPE", 4) == 0) {
13768b2bbf2SGordon Ross 		name += 4;
13868b2bbf2SGordon Ross 		name += strspn(name, "\\");
13968b2bbf2SGordon Ross 	}
14068b2bbf2SGordon Ross 	(void) strlcpy(opipe->p_name, name, SMB_OPIPE_MAXNAME);
14168b2bbf2SGordon Ross 	(void) smb_strlwr(opipe->p_name);
14268b2bbf2SGordon Ross 
14368b2bbf2SGordon Ross 	bzero(&saddr, sizeof (saddr));
14468b2bbf2SGordon Ross 	saddr.sun_family = AF_UNIX;
14568b2bbf2SGordon Ross 	(void) snprintf(saddr.sun_path, sizeof (saddr.sun_path),
14668b2bbf2SGordon Ross 	    "%s/%s", SMB_PIPE_DIR, opipe->p_name);
14768b2bbf2SGordon Ross 	rc = ksocket_connect(opipe->p_socket, (struct sockaddr *)&saddr,
14868b2bbf2SGordon Ross 	    sizeof (saddr), sr->user_cr);
14968b2bbf2SGordon Ross 
15068b2bbf2SGordon Ross 	return (rc);
15168b2bbf2SGordon Ross }
15268b2bbf2SGordon Ross 
15368b2bbf2SGordon Ross /*
15468b2bbf2SGordon Ross  * Helper for open: encode and send the user info.
15568b2bbf2SGordon Ross  *
15668b2bbf2SGordon Ross  * We send information about this client + user to the
15768b2bbf2SGordon Ross  * pipe service so it can use it for access checks.
15868b2bbf2SGordon Ross  * The service MAY deny the open based on this info,
15968b2bbf2SGordon Ross  * (i.e. anonymous session trying to open a pipe that
16068b2bbf2SGordon Ross  * requires authentication) in which case we will read
16168b2bbf2SGordon Ross  * an error status from the service and return that.
16268b2bbf2SGordon Ross  */
16368b2bbf2SGordon Ross static void
16468b2bbf2SGordon Ross smb_opipe_send_userinfo(smb_request_t *sr, smb_opipe_t *opipe,
16568b2bbf2SGordon Ross     smb_error_t *errp)
16668b2bbf2SGordon Ross {
16768b2bbf2SGordon Ross 	XDR xdrs;
16868b2bbf2SGordon Ross 	smb_netuserinfo_t nui;
16968b2bbf2SGordon Ross 	smb_pipehdr_t phdr;
17068b2bbf2SGordon Ross 	char *buf;
17168b2bbf2SGordon Ross 	uint32_t buflen;
17268b2bbf2SGordon Ross 	uint32_t status;
17368b2bbf2SGordon Ross 	size_t iocnt = 0;
17468b2bbf2SGordon Ross 	int rc;
17568b2bbf2SGordon Ross 
17668b2bbf2SGordon Ross 	/*
17768b2bbf2SGordon Ross 	 * Any errors building the XDR message etc.
17868b2bbf2SGordon Ross 	 */
17968b2bbf2SGordon Ross 	errp->status = NT_STATUS_INTERNAL_ERROR;
18068b2bbf2SGordon Ross 
18168b2bbf2SGordon Ross 	smb_user_netinfo_init(sr->uid_user, &nui);
18268b2bbf2SGordon Ross 	phdr.ph_magic = SMB_PIPE_HDR_MAGIC;
18368b2bbf2SGordon Ross 	phdr.ph_uilen = xdr_sizeof(smb_netuserinfo_xdr, &nui);
18468b2bbf2SGordon Ross 
18568b2bbf2SGordon Ross 	buflen = sizeof (phdr) + phdr.ph_uilen;
18668b2bbf2SGordon Ross 	buf = kmem_alloc(buflen, KM_SLEEP);
18768b2bbf2SGordon Ross 
18868b2bbf2SGordon Ross 	bcopy(&phdr, buf, sizeof (phdr));
18968b2bbf2SGordon Ross 	xdrmem_create(&xdrs, buf + sizeof (phdr),
19068b2bbf2SGordon Ross 	    buflen - (sizeof (phdr)), XDR_ENCODE);
19168b2bbf2SGordon Ross 	if (!smb_netuserinfo_xdr(&xdrs, &nui))
19268b2bbf2SGordon Ross 		goto out;
19368b2bbf2SGordon Ross 
194b210fedeSGordon Ross 	mutex_enter(&sr->sr_mutex);
195b210fedeSGordon Ross 	if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
196b210fedeSGordon Ross 		mutex_exit(&sr->sr_mutex);
197b210fedeSGordon Ross 		errp->status = NT_STATUS_CANCELLED;
198b210fedeSGordon Ross 		goto out;
199b210fedeSGordon Ross 	}
200b210fedeSGordon Ross 	sr->sr_state = SMB_REQ_STATE_WAITING_PIPE;
201b210fedeSGordon Ross 	sr->cancel_method = smb_opipe_cancel;
202b210fedeSGordon Ross 	sr->cancel_arg2 = opipe->p_socket;
203b210fedeSGordon Ross 	mutex_exit(&sr->sr_mutex);
20468b2bbf2SGordon Ross 
20568b2bbf2SGordon Ross 	rc = ksocket_send(opipe->p_socket, buf, buflen, 0,
20668b2bbf2SGordon Ross 	    &iocnt, sr->user_cr);
20768b2bbf2SGordon Ross 	if (rc == 0 && iocnt != buflen)
20868b2bbf2SGordon Ross 		rc = EIO;
209b210fedeSGordon Ross 	if (rc == 0)
210b210fedeSGordon Ross 		rc = ksocket_recv(opipe->p_socket, &status, sizeof (status),
211b210fedeSGordon Ross 		    0, &iocnt, sr->user_cr);
212b210fedeSGordon Ross 	if (rc == 0 && iocnt != sizeof (status))
213b210fedeSGordon Ross 		rc = EIO;
21468b2bbf2SGordon Ross 
215b210fedeSGordon Ross 	mutex_enter(&sr->sr_mutex);
216b210fedeSGordon Ross 	sr->cancel_method = NULL;
217b210fedeSGordon Ross 	sr->cancel_arg2 = NULL;
218b210fedeSGordon Ross 	switch (sr->sr_state) {
219b210fedeSGordon Ross 	case SMB_REQ_STATE_WAITING_PIPE:
220b210fedeSGordon Ross 		sr->sr_state = SMB_REQ_STATE_ACTIVE;
221b210fedeSGordon Ross 		break;
222b210fedeSGordon Ross 	case SMB_REQ_STATE_CANCEL_PENDING:
223b210fedeSGordon Ross 		sr->sr_state = SMB_REQ_STATE_CANCELLED;
224b210fedeSGordon Ross 		rc = EINTR;
225b210fedeSGordon Ross 		break;
226b210fedeSGordon Ross 	default:
227b210fedeSGordon Ross 		/* keep rc from above */
228b210fedeSGordon Ross 		break;
229b210fedeSGordon Ross 	}
230b210fedeSGordon Ross 	mutex_exit(&sr->sr_mutex);
231b210fedeSGordon Ross 
23268b2bbf2SGordon Ross 
23368b2bbf2SGordon Ross 	/*
23468b2bbf2SGordon Ross 	 * Return the status we read from the pipe service,
23568b2bbf2SGordon Ross 	 * normally NT_STATUS_SUCCESS, but could be something
23668b2bbf2SGordon Ross 	 * else like NT_STATUS_ACCESS_DENIED.
23768b2bbf2SGordon Ross 	 */
238b210fedeSGordon Ross 	switch (rc) {
239b210fedeSGordon Ross 	case 0:
24068b2bbf2SGordon Ross 		errp->status = status;
241b210fedeSGordon Ross 		break;
242b210fedeSGordon Ross 	case EINTR:
243b210fedeSGordon Ross 		errp->status = NT_STATUS_CANCELLED;
244b210fedeSGordon Ross 		break;
245b210fedeSGordon Ross 	/*
246b210fedeSGordon Ross 	 * If we fail sending the netuserinfo or recv'ing the
247b210fedeSGordon Ross 	 * status reponse, we have probably run into the limit
248b210fedeSGordon Ross 	 * on the number of open pipes.  That's this status:
249b210fedeSGordon Ross 	 */
250b210fedeSGordon Ross 	default:
251b210fedeSGordon Ross 		errp->status = NT_STATUS_PIPE_NOT_AVAILABLE;
252b210fedeSGordon Ross 		break;
253b210fedeSGordon Ross 	}
25468b2bbf2SGordon Ross 
25568b2bbf2SGordon Ross out:
25668b2bbf2SGordon Ross 	xdr_destroy(&xdrs);
25768b2bbf2SGordon Ross 	kmem_free(buf, buflen);
25868b2bbf2SGordon Ross 	smb_user_netinfo_fini(&nui);
25968b2bbf2SGordon Ross }
26068b2bbf2SGordon Ross 
26168b2bbf2SGordon Ross /*
2623db3f65cSamw  * smb_opipe_open
2633db3f65cSamw  *
26468b2bbf2SGordon Ross  * Open an RPC named pipe. This routine should be called if
2653db3f65cSamw  * a file open is requested on a share of type STYPE_IPC.
2663db3f65cSamw  * If we recognize the pipe, we setup a new ofile.
2673db3f65cSamw  *
26868b2bbf2SGordon Ross  * Returns 0 on success, Otherwise an NT status code.
2693db3f65cSamw  */
2703db3f65cSamw int
271*94047d49SGordon Ross smb_opipe_open(smb_request_t *sr, smb_ofile_t *ofile)
2723db3f65cSamw {
273148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
274c5f48fa5SGordon Ross 	smb_attr_t *ap = &op->fqi.fq_fattr;
2753db3f65cSamw 	smb_opipe_t *opipe;
2763db3f65cSamw 	smb_error_t err;
2773db3f65cSamw 
27868b2bbf2SGordon Ross 	opipe = smb_opipe_alloc(sr);
27968b2bbf2SGordon Ross 	if (opipe == NULL)
28068b2bbf2SGordon Ross 		return (NT_STATUS_INTERNAL_ERROR);
2813db3f65cSamw 
28268b2bbf2SGordon Ross 	if (smb_opipe_connect(sr, opipe) != 0) {
28368b2bbf2SGordon Ross 		smb_opipe_dealloc(opipe);
2848b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		return (NT_STATUS_OBJECT_NAME_NOT_FOUND);
2858b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
2868b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
28768b2bbf2SGordon Ross 	smb_opipe_send_userinfo(sr, opipe, &err);
28868b2bbf2SGordon Ross 	if (err.status != 0) {
28968b2bbf2SGordon Ross 		smb_opipe_dealloc(opipe);
29068b2bbf2SGordon Ross 		return (err.status);
29168b2bbf2SGordon Ross 	}
29268b2bbf2SGordon Ross 
29368b2bbf2SGordon Ross 	/*
294*94047d49SGordon Ross 	 * We might have blocked in smb_opipe_connect long enough so
295*94047d49SGordon Ross 	 * a tree disconnect might have happened.  In that case, we
296*94047d49SGordon Ross 	 * would be adding an ofile to a tree that's disconnecting,
297*94047d49SGordon Ross 	 * which would interfere with tear-down.
29868b2bbf2SGordon Ross 	 */
299*94047d49SGordon Ross 	if (!smb_tree_is_connected(sr->tid_tree)) {
30068b2bbf2SGordon Ross 		smb_opipe_dealloc(opipe);
301*94047d49SGordon Ross 		return (NT_STATUS_NETWORK_NAME_DELETED);
30268b2bbf2SGordon Ross 	}
30368b2bbf2SGordon Ross 
304*94047d49SGordon Ross 	/*
305*94047d49SGordon Ross 	 * Note: The new opipe is given to smb_ofile_open
306*94047d49SGordon Ross 	 * via op->pipe
307*94047d49SGordon Ross 	 */
308*94047d49SGordon Ross 	op->pipe = opipe;
309*94047d49SGordon Ross 	smb_ofile_open(sr, op, ofile);
310*94047d49SGordon Ross 	op->pipe = NULL;
311*94047d49SGordon Ross 
31268b2bbf2SGordon Ross 	/* An "up" pointer, for debug. */
31368b2bbf2SGordon Ross 	opipe->p_ofile = ofile;
31468b2bbf2SGordon Ross 
315c5f48fa5SGordon Ross 	/*
316c5f48fa5SGordon Ross 	 * Caller expects attributes in op->fqi
317c5f48fa5SGordon Ross 	 */
318c5f48fa5SGordon Ross 	(void) smb_opipe_getattr(ofile, &op->fqi.fq_fattr);
319c5f48fa5SGordon Ross 
320c5f48fa5SGordon Ross 	op->dsize = 0;
321c5f48fa5SGordon Ross 	op->dattr = ap->sa_dosattr;
322c5f48fa5SGordon Ross 	op->fileid = ap->sa_vattr.va_nodeid;
3233db3f65cSamw 	op->ftype = SMB_FTYPE_MESG_PIPE;
324c5f48fa5SGordon Ross 	op->action_taken = SMB_OACT_OPLOCK | SMB_OACT_OPENED;
3253db3f65cSamw 	op->devstate = SMB_PIPE_READMODE_MESSAGE
3263db3f65cSamw 	    | SMB_PIPE_TYPE_MESSAGE
3273db3f65cSamw 	    | SMB_PIPE_UNLIMITED_INSTANCES; /* 0x05ff */
3283db3f65cSamw 
32968b2bbf2SGordon Ross 	sr->smb_fid = ofile->f_fid;
33068b2bbf2SGordon Ross 	sr->fid_ofile = ofile;
3313db3f65cSamw 
3323db3f65cSamw 	return (NT_STATUS_SUCCESS);
3333db3f65cSamw }
3343db3f65cSamw 
3353db3f65cSamw /*
3363db3f65cSamw  * smb_opipe_close
3373db3f65cSamw  *
33868b2bbf2SGordon Ross  * Called by smb_ofile_close for pipes.
33968b2bbf2SGordon Ross  *
34068b2bbf2SGordon Ross  * Note: ksocket_close may block while waiting for
34168b2bbf2SGordon Ross  * any I/O threads with a hold to get out.
3423db3f65cSamw  */
3433db3f65cSamw void
3443db3f65cSamw smb_opipe_close(smb_ofile_t *of)
3453db3f65cSamw {
3463db3f65cSamw 	smb_opipe_t *opipe;
34768b2bbf2SGordon Ross 	ksocket_t sock;
3483db3f65cSamw 
34968b2bbf2SGordon Ross 	ASSERT(of->f_state == SMB_OFILE_STATE_CLOSING);
3503db3f65cSamw 	ASSERT(of->f_ftype == SMB_FTYPE_MESG_PIPE);
3513db3f65cSamw 	opipe = of->f_pipe;
3529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_OPIPE_VALID(opipe);
3539fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
35468b2bbf2SGordon Ross 	mutex_enter(&opipe->p_mutex);
35568b2bbf2SGordon Ross 	sock = opipe->p_socket;
35668b2bbf2SGordon Ross 	opipe->p_socket = NULL;
35768b2bbf2SGordon Ross 	mutex_exit(&opipe->p_mutex);
3583db3f65cSamw 
35968b2bbf2SGordon Ross 	(void) ksocket_shutdown(sock, SHUT_RDWR, of->f_cr);
36068b2bbf2SGordon Ross 	(void) ksocket_close(sock, of->f_cr);
3613db3f65cSamw }
3623db3f65cSamw 
3633db3f65cSamw /*
3643db3f65cSamw  * smb_opipe_write
3653db3f65cSamw  *
3663db3f65cSamw  * Write RPC request data to the pipe.  The client should call smb_opipe_read
3673db3f65cSamw  * to complete the exchange and obtain the RPC response.
3683db3f65cSamw  *
3693db3f65cSamw  * Returns 0 on success or an errno on failure.
3703db3f65cSamw  */
3713db3f65cSamw int
3723db3f65cSamw smb_opipe_write(smb_request_t *sr, struct uio *uio)
3733db3f65cSamw {
37468b2bbf2SGordon Ross 	struct nmsghdr msghdr;
37568b2bbf2SGordon Ross 	smb_ofile_t *ofile;
3763db3f65cSamw 	smb_opipe_t *opipe;
37768b2bbf2SGordon Ross 	ksocket_t sock;
37868b2bbf2SGordon Ross 	size_t sent = 0;
37968b2bbf2SGordon Ross 	int rc = 0;
3803db3f65cSamw 
38168b2bbf2SGordon Ross 	ofile = sr->fid_ofile;
38268b2bbf2SGordon Ross 	ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
38368b2bbf2SGordon Ross 	opipe = ofile->f_pipe;
3849fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_OPIPE_VALID(opipe);
3853db3f65cSamw 
38668b2bbf2SGordon Ross 	mutex_enter(&opipe->p_mutex);
38768b2bbf2SGordon Ross 	sock = opipe->p_socket;
38868b2bbf2SGordon Ross 	if (sock != NULL)
38968b2bbf2SGordon Ross 		ksocket_hold(sock);
39068b2bbf2SGordon Ross 	mutex_exit(&opipe->p_mutex);
39168b2bbf2SGordon Ross 	if (sock == NULL)
3923db3f65cSamw 		return (EBADF);
39368b2bbf2SGordon Ross 
39468b2bbf2SGordon Ross 	bzero(&msghdr, sizeof (msghdr));
39568b2bbf2SGordon Ross 	msghdr.msg_iov = uio->uio_iov;
39668b2bbf2SGordon Ross 	msghdr.msg_iovlen = uio->uio_iovcnt;
39768b2bbf2SGordon Ross 
39868b2bbf2SGordon Ross 	/*
39968b2bbf2SGordon Ross 	 * This should block until we've sent it all,
40068b2bbf2SGordon Ross 	 * or given up due to errors (pipe closed).
40168b2bbf2SGordon Ross 	 */
40268b2bbf2SGordon Ross 	while (uio->uio_resid > 0) {
40368b2bbf2SGordon Ross 		rc = ksocket_sendmsg(sock, &msghdr, 0, &sent, ofile->f_cr);
40468b2bbf2SGordon Ross 		if (rc != 0)
40568b2bbf2SGordon Ross 			break;
40668b2bbf2SGordon Ross 		uio->uio_resid -= sent;
4073db3f65cSamw 	}
4083db3f65cSamw 
40968b2bbf2SGordon Ross 	ksocket_rele(sock);
4103db3f65cSamw 
41168b2bbf2SGordon Ross 	return (rc);
4123db3f65cSamw }
4133db3f65cSamw 
4143db3f65cSamw /*
4153db3f65cSamw  * smb_opipe_read
4163db3f65cSamw  *
41768b2bbf2SGordon Ross  * This interface may be called from smb_opipe_transact (write, read)
41868b2bbf2SGordon Ross  * or from smb_read / smb2_read to get the rest of an RPC response.
41968b2bbf2SGordon Ross  * The response data (and length) are returned via the uio.
4203db3f65cSamw  */
4213db3f65cSamw int
4223db3f65cSamw smb_opipe_read(smb_request_t *sr, struct uio *uio)
4233db3f65cSamw {
42468b2bbf2SGordon Ross 	struct nmsghdr msghdr;
42568b2bbf2SGordon Ross 	smb_ofile_t *ofile;
4263db3f65cSamw 	smb_opipe_t *opipe;
42768b2bbf2SGordon Ross 	ksocket_t sock;
42868b2bbf2SGordon Ross 	size_t recvcnt = 0;
4293db3f65cSamw 	int rc;
4303db3f65cSamw 
43168b2bbf2SGordon Ross 	ofile = sr->fid_ofile;
43268b2bbf2SGordon Ross 	ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
43368b2bbf2SGordon Ross 	opipe = ofile->f_pipe;
4349fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_OPIPE_VALID(opipe);
4359fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
43668b2bbf2SGordon Ross 	mutex_enter(&opipe->p_mutex);
43768b2bbf2SGordon Ross 	sock = opipe->p_socket;
43868b2bbf2SGordon Ross 	if (sock != NULL)
43968b2bbf2SGordon Ross 		ksocket_hold(sock);
44068b2bbf2SGordon Ross 	mutex_exit(&opipe->p_mutex);
44168b2bbf2SGordon Ross 	if (sock == NULL)
4423db3f65cSamw 		return (EBADF);
4433db3f65cSamw 
444b210fedeSGordon Ross 	mutex_enter(&sr->sr_mutex);
445b210fedeSGordon Ross 	if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
446b210fedeSGordon Ross 		mutex_exit(&sr->sr_mutex);
447b210fedeSGordon Ross 		rc = EINTR;
448b210fedeSGordon Ross 		goto out;
449b210fedeSGordon Ross 	}
450b210fedeSGordon Ross 	sr->sr_state = SMB_REQ_STATE_WAITING_PIPE;
451b210fedeSGordon Ross 	sr->cancel_method = smb_opipe_cancel;
452b210fedeSGordon Ross 	sr->cancel_arg2 = sock;
453b210fedeSGordon Ross 	mutex_exit(&sr->sr_mutex);
4549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
4553db3f65cSamw 	/*
45668b2bbf2SGordon Ross 	 * This should block only if there's no data.
45768b2bbf2SGordon Ross 	 * A single call to recvmsg does just that.
45868b2bbf2SGordon Ross 	 * (Intentionaly no recv loop here.)
4593db3f65cSamw 	 */
460b210fedeSGordon Ross 	bzero(&msghdr, sizeof (msghdr));
461b210fedeSGordon Ross 	msghdr.msg_iov = uio->uio_iov;
462b210fedeSGordon Ross 	msghdr.msg_iovlen = uio->uio_iovcnt;
46368b2bbf2SGordon Ross 	rc = ksocket_recvmsg(sock, &msghdr, 0,
46468b2bbf2SGordon Ross 	    &recvcnt, ofile->f_cr);
465b210fedeSGordon Ross 
466b210fedeSGordon Ross 	mutex_enter(&sr->sr_mutex);
467b210fedeSGordon Ross 	sr->cancel_method = NULL;
468b210fedeSGordon Ross 	sr->cancel_arg2 = NULL;
469b210fedeSGordon Ross 	switch (sr->sr_state) {
470b210fedeSGordon Ross 	case SMB_REQ_STATE_WAITING_PIPE:
471b210fedeSGordon Ross 		sr->sr_state = SMB_REQ_STATE_ACTIVE;
472b210fedeSGordon Ross 		break;
473b210fedeSGordon Ross 	case SMB_REQ_STATE_CANCEL_PENDING:
474b210fedeSGordon Ross 		sr->sr_state = SMB_REQ_STATE_CANCELLED;
475b210fedeSGordon Ross 		rc = EINTR;
476b210fedeSGordon Ross 		break;
477b210fedeSGordon Ross 	default:
478b210fedeSGordon Ross 		/* keep rc from above */
479b210fedeSGordon Ross 		break;
480b210fedeSGordon Ross 	}
481b210fedeSGordon Ross 	mutex_exit(&sr->sr_mutex);
482b210fedeSGordon Ross 
48368b2bbf2SGordon Ross 	if (rc != 0)
48468b2bbf2SGordon Ross 		goto out;
4853db3f65cSamw 
48668b2bbf2SGordon Ross 	if (recvcnt == 0) {
48768b2bbf2SGordon Ross 		/* Other side closed. */
48868b2bbf2SGordon Ross 		rc = EPIPE;
48968b2bbf2SGordon Ross 		goto out;
4903db3f65cSamw 	}
49168b2bbf2SGordon Ross 	uio->uio_resid -= recvcnt;
4923db3f65cSamw 
493bce01b59SGordon Ross out:
494bce01b59SGordon Ross 	ksocket_rele(sock);
495bce01b59SGordon Ross 
496bce01b59SGordon Ross 	return (rc);
49768b2bbf2SGordon Ross }
49868b2bbf2SGordon Ross 
499bce01b59SGordon Ross int
500a90cf9f2SGordon Ross smb_opipe_ioctl(smb_request_t *sr, int cmd, void *arg, int *rvalp)
501bce01b59SGordon Ross {
502bce01b59SGordon Ross 	smb_ofile_t *ofile;
503bce01b59SGordon Ross 	smb_opipe_t *opipe;
504bce01b59SGordon Ross 	ksocket_t sock;
505a90cf9f2SGordon Ross 	int rc;
506bce01b59SGordon Ross 
507bce01b59SGordon Ross 	ofile = sr->fid_ofile;
508bce01b59SGordon Ross 	ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
509bce01b59SGordon Ross 	opipe = ofile->f_pipe;
510bce01b59SGordon Ross 	SMB_OPIPE_VALID(opipe);
511bce01b59SGordon Ross 
512bce01b59SGordon Ross 	mutex_enter(&opipe->p_mutex);
513bce01b59SGordon Ross 	sock = opipe->p_socket;
514bce01b59SGordon Ross 	if (sock != NULL)
515bce01b59SGordon Ross 		ksocket_hold(sock);
516bce01b59SGordon Ross 	mutex_exit(&opipe->p_mutex);
517bce01b59SGordon Ross 	if (sock == NULL)
518bce01b59SGordon Ross 		return (EBADF);
519bce01b59SGordon Ross 
520a90cf9f2SGordon Ross 	rc = ksocket_ioctl(sock, cmd, (intptr_t)arg, rvalp, ofile->f_cr);
521bce01b59SGordon Ross 
52268b2bbf2SGordon Ross 	ksocket_rele(sock);
52368b2bbf2SGordon Ross 
52468b2bbf2SGordon Ross 	return (rc);
5253db3f65cSamw }
526a90cf9f2SGordon Ross 
527a90cf9f2SGordon Ross /*
528a90cf9f2SGordon Ross  * Get the smb_attr_t for a named pipe.
529a90cf9f2SGordon Ross  * Caller has already cleared to zero.
530a90cf9f2SGordon Ross  */
531a90cf9f2SGordon Ross int
532a90cf9f2SGordon Ross smb_opipe_getattr(smb_ofile_t *of, smb_attr_t *ap)
533a90cf9f2SGordon Ross {
534a90cf9f2SGordon Ross 
535a90cf9f2SGordon Ross 	if (of->f_pipe == NULL)
536a90cf9f2SGordon Ross 		return (EINVAL);
537a90cf9f2SGordon Ross 
538a90cf9f2SGordon Ross 	ap->sa_vattr.va_type = VFIFO;
539a90cf9f2SGordon Ross 	ap->sa_vattr.va_nlink = 1;
540c5f48fa5SGordon Ross 	ap->sa_vattr.va_nodeid = (uintptr_t)of->f_pipe;
541a90cf9f2SGordon Ross 	ap->sa_dosattr = FILE_ATTRIBUTE_NORMAL;
542c5f48fa5SGordon Ross 	ap->sa_allocsz = SMB_PIPE_MAX_MSGSIZE;
543a90cf9f2SGordon Ross 
544a90cf9f2SGordon Ross 	return (0);
545a90cf9f2SGordon Ross }
546a90cf9f2SGordon Ross 
547a90cf9f2SGordon Ross int
548a90cf9f2SGordon Ross smb_opipe_getname(smb_ofile_t *of, char *buf, size_t buflen)
549a90cf9f2SGordon Ross {
550a90cf9f2SGordon Ross 	smb_opipe_t *opipe;
551a90cf9f2SGordon Ross 
552a90cf9f2SGordon Ross 	if ((opipe = of->f_pipe) == NULL)
553a90cf9f2SGordon Ross 		return (EINVAL);
554a90cf9f2SGordon Ross 
555a90cf9f2SGordon Ross 	(void) snprintf(buf, buflen, "\\%s", opipe->p_name);
556a90cf9f2SGordon Ross 	return (0);
557a90cf9f2SGordon Ross }
558a90cf9f2SGordon Ross 
559a90cf9f2SGordon Ross /*
560a90cf9f2SGordon Ross  * Handler for smb2_ioctl
561a90cf9f2SGordon Ross  */
562a90cf9f2SGordon Ross /* ARGSUSED */
563a90cf9f2SGordon Ross uint32_t
564a90cf9f2SGordon Ross smb_opipe_fsctl(smb_request_t *sr, smb_fsctl_t *fsctl)
565a90cf9f2SGordon Ross {
566a90cf9f2SGordon Ross 	uint32_t status;
567a90cf9f2SGordon Ross 
568a90cf9f2SGordon Ross 	switch (fsctl->CtlCode) {
569a90cf9f2SGordon Ross 	case FSCTL_PIPE_TRANSCEIVE:
570a90cf9f2SGordon Ross 		status = smb_opipe_transceive(sr, fsctl);
571a90cf9f2SGordon Ross 		break;
572a90cf9f2SGordon Ross 
573a90cf9f2SGordon Ross 	case FSCTL_PIPE_PEEK:
574a90cf9f2SGordon Ross 	case FSCTL_PIPE_WAIT:
575a90cf9f2SGordon Ross 		/* XXX todo */
576a90cf9f2SGordon Ross 		status = NT_STATUS_NOT_SUPPORTED;
577a90cf9f2SGordon Ross 		break;
578a90cf9f2SGordon Ross 
579a90cf9f2SGordon Ross 	default:
580a90cf9f2SGordon Ross 		ASSERT(!"CtlCode");
581a90cf9f2SGordon Ross 		status = NT_STATUS_INTERNAL_ERROR;
582a90cf9f2SGordon Ross 		break;
583a90cf9f2SGordon Ross 	}
584a90cf9f2SGordon Ross 
585a90cf9f2SGordon Ross 	return (status);
586a90cf9f2SGordon Ross }
587a90cf9f2SGordon Ross 
588a90cf9f2SGordon Ross static uint32_t
589a90cf9f2SGordon Ross smb_opipe_transceive(smb_request_t *sr, smb_fsctl_t *fsctl)
590a90cf9f2SGordon Ross {
591a90cf9f2SGordon Ross 	smb_vdb_t	vdb;
592a90cf9f2SGordon Ross 	smb_ofile_t	*ofile;
593a90cf9f2SGordon Ross 	struct mbuf	*mb;
594a90cf9f2SGordon Ross 	uint32_t	status;
595a90cf9f2SGordon Ross 	int		len, rc;
596a90cf9f2SGordon Ross 
597a90cf9f2SGordon Ross 	/*
598a90cf9f2SGordon Ross 	 * Caller checked that this is the IPC$ share,
599a90cf9f2SGordon Ross 	 * and that this call has a valid open handle.
600a90cf9f2SGordon Ross 	 * Just check the type.
601a90cf9f2SGordon Ross 	 */
602a90cf9f2SGordon Ross 	ofile = sr->fid_ofile;
603a90cf9f2SGordon Ross 	if (ofile->f_ftype != SMB_FTYPE_MESG_PIPE)
604a90cf9f2SGordon Ross 		return (NT_STATUS_INVALID_HANDLE);
605a90cf9f2SGordon Ross 
606a90cf9f2SGordon Ross 	rc = smb_mbc_decodef(fsctl->in_mbc, "#B",
607a90cf9f2SGordon Ross 	    fsctl->InputCount, &vdb);
608a90cf9f2SGordon Ross 	if (rc != 0) {
609a90cf9f2SGordon Ross 		/* Not enough data sent. */
610a90cf9f2SGordon Ross 		return (NT_STATUS_INVALID_PARAMETER);
611a90cf9f2SGordon Ross 	}
612a90cf9f2SGordon Ross 
613a90cf9f2SGordon Ross 	rc = smb_opipe_write(sr, &vdb.vdb_uio);
614a90cf9f2SGordon Ross 	if (rc != 0)
615a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
616a90cf9f2SGordon Ross 
617a90cf9f2SGordon Ross 	vdb.vdb_tag = 0;
618a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_iov = &vdb.vdb_iovec[0];
619a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_iovcnt = MAX_IOVEC;
620a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_segflg = UIO_SYSSPACE;
621a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_extflg = UIO_COPY_DEFAULT;
622a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_loffset = (offset_t)0;
623a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_resid = fsctl->MaxOutputResp;
624a90cf9f2SGordon Ross 	mb = smb_mbuf_allocate(&vdb.vdb_uio);
625a90cf9f2SGordon Ross 
626a90cf9f2SGordon Ross 	rc = smb_opipe_read(sr, &vdb.vdb_uio);
627a90cf9f2SGordon Ross 	if (rc != 0) {
628a90cf9f2SGordon Ross 		m_freem(mb);
629a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
630a90cf9f2SGordon Ross 	}
631a90cf9f2SGordon Ross 
632a90cf9f2SGordon Ross 	len = fsctl->MaxOutputResp - vdb.vdb_uio.uio_resid;
633a90cf9f2SGordon Ross 	smb_mbuf_trim(mb, len);
634a90cf9f2SGordon Ross 	MBC_ATTACH_MBUF(fsctl->out_mbc, mb);
635a90cf9f2SGordon Ross 
636a90cf9f2SGordon Ross 	/*
637a90cf9f2SGordon Ross 	 * If the output buffer holds a partial pipe message,
638a90cf9f2SGordon Ross 	 * we're supposed to return NT_STATUS_BUFFER_OVERFLOW.
639a90cf9f2SGordon Ross 	 * As we don't have message boundary markers, the best
640a90cf9f2SGordon Ross 	 * we can do is return that status when we have ALL of:
641a90cf9f2SGordon Ross 	 *	Output buffer was < SMB_PIPE_MAX_MSGSIZE
642a90cf9f2SGordon Ross 	 *	We filled the output buffer (resid==0)
643a90cf9f2SGordon Ross 	 *	There's more data (ioctl FIONREAD)
644a90cf9f2SGordon Ross 	 */
645a90cf9f2SGordon Ross 	status = NT_STATUS_SUCCESS;
646a90cf9f2SGordon Ross 	if (fsctl->MaxOutputResp < SMB_PIPE_MAX_MSGSIZE &&
647a90cf9f2SGordon Ross 	    vdb.vdb_uio.uio_resid == 0) {
648a90cf9f2SGordon Ross 		int nread = 0, trval;
649a90cf9f2SGordon Ross 		rc = smb_opipe_ioctl(sr, FIONREAD, &nread, &trval);
650a90cf9f2SGordon Ross 		if (rc == 0 && nread != 0)
651a90cf9f2SGordon Ross 			status = NT_STATUS_BUFFER_OVERFLOW;
652a90cf9f2SGordon Ross 	}
653a90cf9f2SGordon Ross 
654a90cf9f2SGordon Ross 	return (status);
655a90cf9f2SGordon Ross }
656