xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_opipe.c (revision 55f0a249fd3511728b02627190771a4ce4ddf20e)
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 
4068b2bbf2SGordon Ross /*
4168b2bbf2SGordon Ross  * Allocate a new opipe and return it, or NULL, in which case
4268b2bbf2SGordon Ross  * the caller will report "internal error".
4368b2bbf2SGordon Ross  */
4468b2bbf2SGordon Ross static smb_opipe_t *
4568b2bbf2SGordon Ross smb_opipe_alloc(smb_request_t *sr)
469fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
4768b2bbf2SGordon Ross 	smb_server_t	*sv = sr->sr_server;
489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_opipe_t	*opipe;
4968b2bbf2SGordon Ross 	ksocket_t	sock;
5068b2bbf2SGordon Ross 
5168b2bbf2SGordon Ross 	if (ksocket_socket(&sock, AF_UNIX, SOCK_STREAM, 0,
5268b2bbf2SGordon Ross 	    KSOCKET_SLEEP, sr->user_cr) != 0)
5368b2bbf2SGordon Ross 		return (NULL);
549fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
558622ec45SGordon Ross 	opipe = kmem_cache_alloc(smb_cache_opipe, KM_SLEEP);
569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
579fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	bzero(opipe, sizeof (smb_opipe_t));
589fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_init(&opipe->p_mutex, NULL, MUTEX_DEFAULT, NULL);
599fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	cv_init(&opipe->p_cv, NULL, CV_DEFAULT, NULL);
609fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	opipe->p_magic = SMB_OPIPE_MAGIC;
619fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	opipe->p_server = sv;
6268b2bbf2SGordon Ross 	opipe->p_refcnt = 1;
6368b2bbf2SGordon Ross 	opipe->p_socket = sock;
649fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
659fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	return (opipe);
669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
6868b2bbf2SGordon Ross /*
6968b2bbf2SGordon Ross  * Destroy an opipe.  This is normally called from smb_ofile_delete
7068b2bbf2SGordon Ross  * when the ofile has no more references and is about to be free'd.
7168b2bbf2SGordon Ross  * This is also called here in error handling code paths, before
7268b2bbf2SGordon Ross  * the opipe is installed under an ofile.
7368b2bbf2SGordon Ross  */
749fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States void
759fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States smb_opipe_dealloc(smb_opipe_t *opipe)
769fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States {
779fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_server_t *sv;
789fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
799fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_OPIPE_VALID(opipe);
809fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	sv = opipe->p_server;
819fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_SERVER_VALID(sv);
829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
8368b2bbf2SGordon Ross 	/*
8468b2bbf2SGordon Ross 	 * This is called in the error path when opening,
8568b2bbf2SGordon Ross 	 * in which case we close the socket here.
8668b2bbf2SGordon Ross 	 */
8768b2bbf2SGordon Ross 	if (opipe->p_socket != NULL)
8868b2bbf2SGordon Ross 		(void) ksocket_close(opipe->p_socket, zone_kcred());
899fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
909fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	opipe->p_magic = (uint32_t)~SMB_OPIPE_MAGIC;
919fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	cv_destroy(&opipe->p_cv);
929fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	mutex_destroy(&opipe->p_mutex);
939fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
948622ec45SGordon Ross 	kmem_cache_free(smb_cache_opipe, opipe);
959fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States }
969fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
973db3f65cSamw /*
98b210fedeSGordon Ross  * Unblock a request that might be blocked reading some
99b210fedeSGordon Ross  * pipe (AF_UNIX socket).  We don't have an easy way to
100b210fedeSGordon Ross  * interrupt just the thread servicing this request, so
101b210fedeSGordon Ross  * we shutdown(3socket) the socket, waking all readers.
102b210fedeSGordon Ross  * That's a bit heavy-handed, making the socket unusable
103b210fedeSGordon Ross  * after this, so we do this only when disconnecting a
104b210fedeSGordon Ross  * session (i.e. stopping the SMB service), and not when
105b210fedeSGordon Ross  * handling an SMB2_cancel or SMB_nt_cancel request.
106b210fedeSGordon Ross  */
107b210fedeSGordon Ross static void
108b210fedeSGordon Ross smb_opipe_cancel(smb_request_t *sr)
109b210fedeSGordon Ross {
110b210fedeSGordon Ross 	ksocket_t so;
111b210fedeSGordon Ross 
112811599a4SMatt Barden 	switch (sr->session->s_state) {
113811599a4SMatt Barden 	case SMB_SESSION_STATE_DISCONNECTED:
114811599a4SMatt Barden 	case SMB_SESSION_STATE_TERMINATED:
115811599a4SMatt Barden 		if ((so = sr->cancel_arg2) != NULL)
116b210fedeSGordon Ross 			(void) ksocket_shutdown(so, SHUT_RDWR, sr->user_cr);
117811599a4SMatt Barden 		break;
118b210fedeSGordon Ross 	}
119b210fedeSGordon Ross }
120b210fedeSGordon Ross 
121b210fedeSGordon Ross /*
12268b2bbf2SGordon Ross  * Helper for open: build pipe name and connect.
12368b2bbf2SGordon Ross  */
12468b2bbf2SGordon Ross static int
12568b2bbf2SGordon Ross smb_opipe_connect(smb_request_t *sr, smb_opipe_t *opipe)
12668b2bbf2SGordon Ross {
12768b2bbf2SGordon Ross 	struct sockaddr_un saddr;
12868b2bbf2SGordon Ross 	smb_arg_open_t	*op = &sr->sr_open;
12968b2bbf2SGordon Ross 	const char *name;
13068b2bbf2SGordon Ross 	int rc;
13168b2bbf2SGordon Ross 
13268b2bbf2SGordon Ross 	name = op->fqi.fq_path.pn_path;
13368b2bbf2SGordon Ross 	name += strspn(name, "\\");
13468b2bbf2SGordon Ross 	if (smb_strcasecmp(name, "PIPE", 4) == 0) {
13568b2bbf2SGordon Ross 		name += 4;
13668b2bbf2SGordon Ross 		name += strspn(name, "\\");
13768b2bbf2SGordon Ross 	}
13868b2bbf2SGordon Ross 	(void) strlcpy(opipe->p_name, name, SMB_OPIPE_MAXNAME);
13968b2bbf2SGordon Ross 	(void) smb_strlwr(opipe->p_name);
14068b2bbf2SGordon Ross 
14168b2bbf2SGordon Ross 	bzero(&saddr, sizeof (saddr));
14268b2bbf2SGordon Ross 	saddr.sun_family = AF_UNIX;
14368b2bbf2SGordon Ross 	(void) snprintf(saddr.sun_path, sizeof (saddr.sun_path),
14468b2bbf2SGordon Ross 	    "%s/%s", SMB_PIPE_DIR, opipe->p_name);
14568b2bbf2SGordon Ross 	rc = ksocket_connect(opipe->p_socket, (struct sockaddr *)&saddr,
14668b2bbf2SGordon Ross 	    sizeof (saddr), sr->user_cr);
14768b2bbf2SGordon Ross 
14868b2bbf2SGordon Ross 	return (rc);
14968b2bbf2SGordon Ross }
15068b2bbf2SGordon Ross 
15168b2bbf2SGordon Ross /*
15268b2bbf2SGordon Ross  * Helper for open: encode and send the user info.
15368b2bbf2SGordon Ross  *
15468b2bbf2SGordon Ross  * We send information about this client + user to the
15568b2bbf2SGordon Ross  * pipe service so it can use it for access checks.
15668b2bbf2SGordon Ross  * The service MAY deny the open based on this info,
15768b2bbf2SGordon Ross  * (i.e. anonymous session trying to open a pipe that
15868b2bbf2SGordon Ross  * requires authentication) in which case we will read
15968b2bbf2SGordon Ross  * an error status from the service and return that.
16068b2bbf2SGordon Ross  */
16168b2bbf2SGordon Ross static void
16268b2bbf2SGordon Ross smb_opipe_send_userinfo(smb_request_t *sr, smb_opipe_t *opipe,
16368b2bbf2SGordon Ross     smb_error_t *errp)
16468b2bbf2SGordon Ross {
16568b2bbf2SGordon Ross 	XDR xdrs;
16668b2bbf2SGordon Ross 	smb_netuserinfo_t nui;
16768b2bbf2SGordon Ross 	smb_pipehdr_t phdr;
16868b2bbf2SGordon Ross 	char *buf;
16968b2bbf2SGordon Ross 	uint32_t buflen;
17068b2bbf2SGordon Ross 	uint32_t status;
17168b2bbf2SGordon Ross 	size_t iocnt = 0;
17268b2bbf2SGordon Ross 	int rc;
17368b2bbf2SGordon Ross 
17468b2bbf2SGordon Ross 	/*
17568b2bbf2SGordon Ross 	 * Any errors building the XDR message etc.
17668b2bbf2SGordon Ross 	 */
17768b2bbf2SGordon Ross 	errp->status = NT_STATUS_INTERNAL_ERROR;
17868b2bbf2SGordon Ross 
17968b2bbf2SGordon Ross 	smb_user_netinfo_init(sr->uid_user, &nui);
18068b2bbf2SGordon Ross 	phdr.ph_magic = SMB_PIPE_HDR_MAGIC;
18168b2bbf2SGordon Ross 	phdr.ph_uilen = xdr_sizeof(smb_netuserinfo_xdr, &nui);
18268b2bbf2SGordon Ross 
18368b2bbf2SGordon Ross 	buflen = sizeof (phdr) + phdr.ph_uilen;
18468b2bbf2SGordon Ross 	buf = kmem_alloc(buflen, KM_SLEEP);
18568b2bbf2SGordon Ross 
18668b2bbf2SGordon Ross 	bcopy(&phdr, buf, sizeof (phdr));
18768b2bbf2SGordon Ross 	xdrmem_create(&xdrs, buf + sizeof (phdr),
18868b2bbf2SGordon Ross 	    buflen - (sizeof (phdr)), XDR_ENCODE);
18968b2bbf2SGordon Ross 	if (!smb_netuserinfo_xdr(&xdrs, &nui))
19068b2bbf2SGordon Ross 		goto out;
19168b2bbf2SGordon Ross 
192b210fedeSGordon Ross 	mutex_enter(&sr->sr_mutex);
193b210fedeSGordon Ross 	if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
194b210fedeSGordon Ross 		mutex_exit(&sr->sr_mutex);
195b210fedeSGordon Ross 		errp->status = NT_STATUS_CANCELLED;
196b210fedeSGordon Ross 		goto out;
197b210fedeSGordon Ross 	}
198b210fedeSGordon Ross 	sr->sr_state = SMB_REQ_STATE_WAITING_PIPE;
199b210fedeSGordon Ross 	sr->cancel_method = smb_opipe_cancel;
200b210fedeSGordon Ross 	sr->cancel_arg2 = opipe->p_socket;
201b210fedeSGordon Ross 	mutex_exit(&sr->sr_mutex);
20268b2bbf2SGordon Ross 
20368b2bbf2SGordon Ross 	rc = ksocket_send(opipe->p_socket, buf, buflen, 0,
20468b2bbf2SGordon Ross 	    &iocnt, sr->user_cr);
20568b2bbf2SGordon Ross 	if (rc == 0 && iocnt != buflen)
20668b2bbf2SGordon Ross 		rc = EIO;
207b210fedeSGordon Ross 	if (rc == 0)
208b210fedeSGordon Ross 		rc = ksocket_recv(opipe->p_socket, &status, sizeof (status),
209b210fedeSGordon Ross 		    0, &iocnt, sr->user_cr);
210b210fedeSGordon Ross 	if (rc == 0 && iocnt != sizeof (status))
211b210fedeSGordon Ross 		rc = EIO;
21268b2bbf2SGordon Ross 
213b210fedeSGordon Ross 	mutex_enter(&sr->sr_mutex);
214b210fedeSGordon Ross 	sr->cancel_method = NULL;
215b210fedeSGordon Ross 	sr->cancel_arg2 = NULL;
216b210fedeSGordon Ross 	switch (sr->sr_state) {
217b210fedeSGordon Ross 	case SMB_REQ_STATE_WAITING_PIPE:
218b210fedeSGordon Ross 		sr->sr_state = SMB_REQ_STATE_ACTIVE;
219b210fedeSGordon Ross 		break;
220b210fedeSGordon Ross 	case SMB_REQ_STATE_CANCEL_PENDING:
221b210fedeSGordon Ross 		sr->sr_state = SMB_REQ_STATE_CANCELLED;
222b210fedeSGordon Ross 		rc = EINTR;
223b210fedeSGordon Ross 		break;
224b210fedeSGordon Ross 	default:
225b210fedeSGordon Ross 		/* keep rc from above */
226b210fedeSGordon Ross 		break;
227b210fedeSGordon Ross 	}
228b210fedeSGordon Ross 	mutex_exit(&sr->sr_mutex);
229b210fedeSGordon Ross 
23068b2bbf2SGordon Ross 
23168b2bbf2SGordon Ross 	/*
23268b2bbf2SGordon Ross 	 * Return the status we read from the pipe service,
23368b2bbf2SGordon Ross 	 * normally NT_STATUS_SUCCESS, but could be something
23468b2bbf2SGordon Ross 	 * else like NT_STATUS_ACCESS_DENIED.
23568b2bbf2SGordon Ross 	 */
236b210fedeSGordon Ross 	switch (rc) {
237b210fedeSGordon Ross 	case 0:
23868b2bbf2SGordon Ross 		errp->status = status;
239b210fedeSGordon Ross 		break;
240b210fedeSGordon Ross 	case EINTR:
241b210fedeSGordon Ross 		errp->status = NT_STATUS_CANCELLED;
242b210fedeSGordon Ross 		break;
243b210fedeSGordon Ross 	/*
244b210fedeSGordon Ross 	 * If we fail sending the netuserinfo or recv'ing the
245b210fedeSGordon Ross 	 * status reponse, we have probably run into the limit
246b210fedeSGordon Ross 	 * on the number of open pipes.  That's this status:
247b210fedeSGordon Ross 	 */
248b210fedeSGordon Ross 	default:
249b210fedeSGordon Ross 		errp->status = NT_STATUS_PIPE_NOT_AVAILABLE;
250b210fedeSGordon Ross 		break;
251b210fedeSGordon Ross 	}
25268b2bbf2SGordon Ross 
25368b2bbf2SGordon Ross out:
25468b2bbf2SGordon Ross 	xdr_destroy(&xdrs);
25568b2bbf2SGordon Ross 	kmem_free(buf, buflen);
25668b2bbf2SGordon Ross 	smb_user_netinfo_fini(&nui);
25768b2bbf2SGordon Ross }
25868b2bbf2SGordon Ross 
25968b2bbf2SGordon Ross /*
2603db3f65cSamw  * smb_opipe_open
2613db3f65cSamw  *
26268b2bbf2SGordon Ross  * Open an RPC named pipe. This routine should be called if
2633db3f65cSamw  * a file open is requested on a share of type STYPE_IPC.
2643db3f65cSamw  * If we recognize the pipe, we setup a new ofile.
2653db3f65cSamw  *
26668b2bbf2SGordon Ross  * Returns 0 on success, Otherwise an NT status code.
2673db3f65cSamw  */
2683db3f65cSamw int
26994047d49SGordon Ross smb_opipe_open(smb_request_t *sr, smb_ofile_t *ofile)
2703db3f65cSamw {
271148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
272c5f48fa5SGordon Ross 	smb_attr_t *ap = &op->fqi.fq_fattr;
2733db3f65cSamw 	smb_opipe_t *opipe;
2743db3f65cSamw 	smb_error_t err;
2753db3f65cSamw 
27668b2bbf2SGordon Ross 	opipe = smb_opipe_alloc(sr);
27768b2bbf2SGordon Ross 	if (opipe == NULL)
27868b2bbf2SGordon Ross 		return (NT_STATUS_INTERNAL_ERROR);
2793db3f65cSamw 
28068b2bbf2SGordon Ross 	if (smb_opipe_connect(sr, opipe) != 0) {
28168b2bbf2SGordon Ross 		smb_opipe_dealloc(opipe);
2828b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		return (NT_STATUS_OBJECT_NAME_NOT_FOUND);
2838b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	}
2848b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
28568b2bbf2SGordon Ross 	smb_opipe_send_userinfo(sr, opipe, &err);
28668b2bbf2SGordon Ross 	if (err.status != 0) {
28768b2bbf2SGordon Ross 		smb_opipe_dealloc(opipe);
28868b2bbf2SGordon Ross 		return (err.status);
28968b2bbf2SGordon Ross 	}
29068b2bbf2SGordon Ross 
29168b2bbf2SGordon Ross 	/*
29294047d49SGordon Ross 	 * We might have blocked in smb_opipe_connect long enough so
29394047d49SGordon Ross 	 * a tree disconnect might have happened.  In that case, we
29494047d49SGordon Ross 	 * would be adding an ofile to a tree that's disconnecting,
29594047d49SGordon Ross 	 * which would interfere with tear-down.
29668b2bbf2SGordon Ross 	 */
29794047d49SGordon Ross 	if (!smb_tree_is_connected(sr->tid_tree)) {
29868b2bbf2SGordon Ross 		smb_opipe_dealloc(opipe);
29994047d49SGordon Ross 		return (NT_STATUS_NETWORK_NAME_DELETED);
30068b2bbf2SGordon Ross 	}
30168b2bbf2SGordon Ross 
30294047d49SGordon Ross 	/*
30394047d49SGordon Ross 	 * Note: The new opipe is given to smb_ofile_open
30494047d49SGordon Ross 	 * via op->pipe
30594047d49SGordon Ross 	 */
30694047d49SGordon Ross 	op->pipe = opipe;
30794047d49SGordon Ross 	smb_ofile_open(sr, op, ofile);
30894047d49SGordon Ross 	op->pipe = NULL;
30994047d49SGordon Ross 
31068b2bbf2SGordon Ross 	/* An "up" pointer, for debug. */
31168b2bbf2SGordon Ross 	opipe->p_ofile = ofile;
31268b2bbf2SGordon Ross 
313c5f48fa5SGordon Ross 	/*
314c5f48fa5SGordon Ross 	 * Caller expects attributes in op->fqi
315c5f48fa5SGordon Ross 	 */
316c5f48fa5SGordon Ross 	(void) smb_opipe_getattr(ofile, &op->fqi.fq_fattr);
317c5f48fa5SGordon Ross 
318c5f48fa5SGordon Ross 	op->dsize = 0;
319c5f48fa5SGordon Ross 	op->dattr = ap->sa_dosattr;
320c5f48fa5SGordon Ross 	op->fileid = ap->sa_vattr.va_nodeid;
3213db3f65cSamw 	op->ftype = SMB_FTYPE_MESG_PIPE;
322c5f48fa5SGordon Ross 	op->action_taken = SMB_OACT_OPLOCK | SMB_OACT_OPENED;
3233db3f65cSamw 	op->devstate = SMB_PIPE_READMODE_MESSAGE
3243db3f65cSamw 	    | SMB_PIPE_TYPE_MESSAGE
3253db3f65cSamw 	    | SMB_PIPE_UNLIMITED_INSTANCES; /* 0x05ff */
3263db3f65cSamw 
32768b2bbf2SGordon Ross 	sr->smb_fid = ofile->f_fid;
32868b2bbf2SGordon Ross 	sr->fid_ofile = ofile;
3293db3f65cSamw 
3303db3f65cSamw 	return (NT_STATUS_SUCCESS);
3313db3f65cSamw }
3323db3f65cSamw 
3333db3f65cSamw /*
3343db3f65cSamw  * smb_opipe_close
3353db3f65cSamw  *
33668b2bbf2SGordon Ross  * Called by smb_ofile_close for pipes.
33768b2bbf2SGordon Ross  *
33868b2bbf2SGordon Ross  * Note: ksocket_close may block while waiting for
33968b2bbf2SGordon Ross  * any I/O threads with a hold to get out.
3403db3f65cSamw  */
3413db3f65cSamw void
3423db3f65cSamw smb_opipe_close(smb_ofile_t *of)
3433db3f65cSamw {
3443db3f65cSamw 	smb_opipe_t *opipe;
34568b2bbf2SGordon Ross 	ksocket_t sock;
3463db3f65cSamw 
34768b2bbf2SGordon Ross 	ASSERT(of->f_state == SMB_OFILE_STATE_CLOSING);
3483db3f65cSamw 	ASSERT(of->f_ftype == SMB_FTYPE_MESG_PIPE);
3493db3f65cSamw 	opipe = of->f_pipe;
3509fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_OPIPE_VALID(opipe);
3519fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
35268b2bbf2SGordon Ross 	mutex_enter(&opipe->p_mutex);
35368b2bbf2SGordon Ross 	sock = opipe->p_socket;
35468b2bbf2SGordon Ross 	opipe->p_socket = NULL;
35568b2bbf2SGordon Ross 	mutex_exit(&opipe->p_mutex);
3563db3f65cSamw 
35768b2bbf2SGordon Ross 	(void) ksocket_shutdown(sock, SHUT_RDWR, of->f_cr);
35868b2bbf2SGordon Ross 	(void) ksocket_close(sock, of->f_cr);
3593db3f65cSamw }
3603db3f65cSamw 
3613db3f65cSamw /*
3623db3f65cSamw  * smb_opipe_write
3633db3f65cSamw  *
3643db3f65cSamw  * Write RPC request data to the pipe.  The client should call smb_opipe_read
3653db3f65cSamw  * to complete the exchange and obtain the RPC response.
3663db3f65cSamw  *
3673db3f65cSamw  * Returns 0 on success or an errno on failure.
3683db3f65cSamw  */
3693db3f65cSamw int
3703db3f65cSamw smb_opipe_write(smb_request_t *sr, struct uio *uio)
3713db3f65cSamw {
37268b2bbf2SGordon Ross 	struct nmsghdr msghdr;
37368b2bbf2SGordon Ross 	smb_ofile_t *ofile;
3743db3f65cSamw 	smb_opipe_t *opipe;
37568b2bbf2SGordon Ross 	ksocket_t sock;
37668b2bbf2SGordon Ross 	size_t sent = 0;
37768b2bbf2SGordon Ross 	int rc = 0;
3783db3f65cSamw 
37968b2bbf2SGordon Ross 	ofile = sr->fid_ofile;
38068b2bbf2SGordon Ross 	ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
38168b2bbf2SGordon Ross 	opipe = ofile->f_pipe;
3829fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_OPIPE_VALID(opipe);
3833db3f65cSamw 
38468b2bbf2SGordon Ross 	mutex_enter(&opipe->p_mutex);
38568b2bbf2SGordon Ross 	sock = opipe->p_socket;
38668b2bbf2SGordon Ross 	if (sock != NULL)
38768b2bbf2SGordon Ross 		ksocket_hold(sock);
38868b2bbf2SGordon Ross 	mutex_exit(&opipe->p_mutex);
38968b2bbf2SGordon Ross 	if (sock == NULL)
3903db3f65cSamw 		return (EBADF);
39168b2bbf2SGordon Ross 
39268b2bbf2SGordon Ross 	bzero(&msghdr, sizeof (msghdr));
39368b2bbf2SGordon Ross 	msghdr.msg_iov = uio->uio_iov;
39468b2bbf2SGordon Ross 	msghdr.msg_iovlen = uio->uio_iovcnt;
39568b2bbf2SGordon Ross 
39668b2bbf2SGordon Ross 	/*
39768b2bbf2SGordon Ross 	 * This should block until we've sent it all,
39868b2bbf2SGordon Ross 	 * or given up due to errors (pipe closed).
39968b2bbf2SGordon Ross 	 */
40068b2bbf2SGordon Ross 	while (uio->uio_resid > 0) {
40168b2bbf2SGordon Ross 		rc = ksocket_sendmsg(sock, &msghdr, 0, &sent, ofile->f_cr);
40268b2bbf2SGordon Ross 		if (rc != 0)
40368b2bbf2SGordon Ross 			break;
40468b2bbf2SGordon Ross 		uio->uio_resid -= sent;
4053db3f65cSamw 	}
4063db3f65cSamw 
40768b2bbf2SGordon Ross 	ksocket_rele(sock);
4083db3f65cSamw 
40968b2bbf2SGordon Ross 	return (rc);
4103db3f65cSamw }
4113db3f65cSamw 
4123db3f65cSamw /*
4133db3f65cSamw  * smb_opipe_read
4143db3f65cSamw  *
41568b2bbf2SGordon Ross  * This interface may be called from smb_opipe_transact (write, read)
41668b2bbf2SGordon Ross  * or from smb_read / smb2_read to get the rest of an RPC response.
41768b2bbf2SGordon Ross  * The response data (and length) are returned via the uio.
4183db3f65cSamw  */
4193db3f65cSamw int
4203db3f65cSamw smb_opipe_read(smb_request_t *sr, struct uio *uio)
4213db3f65cSamw {
42268b2bbf2SGordon Ross 	struct nmsghdr msghdr;
42368b2bbf2SGordon Ross 	smb_ofile_t *ofile;
4243db3f65cSamw 	smb_opipe_t *opipe;
42568b2bbf2SGordon Ross 	ksocket_t sock;
42668b2bbf2SGordon Ross 	size_t recvcnt = 0;
4273db3f65cSamw 	int rc;
4283db3f65cSamw 
42968b2bbf2SGordon Ross 	ofile = sr->fid_ofile;
43068b2bbf2SGordon Ross 	ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
43168b2bbf2SGordon Ross 	opipe = ofile->f_pipe;
4329fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	SMB_OPIPE_VALID(opipe);
4339fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
43468b2bbf2SGordon Ross 	mutex_enter(&opipe->p_mutex);
43568b2bbf2SGordon Ross 	sock = opipe->p_socket;
43668b2bbf2SGordon Ross 	if (sock != NULL)
43768b2bbf2SGordon Ross 		ksocket_hold(sock);
43868b2bbf2SGordon Ross 	mutex_exit(&opipe->p_mutex);
43968b2bbf2SGordon Ross 	if (sock == NULL)
4403db3f65cSamw 		return (EBADF);
4413db3f65cSamw 
442b210fedeSGordon Ross 	mutex_enter(&sr->sr_mutex);
443b210fedeSGordon Ross 	if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
444b210fedeSGordon Ross 		mutex_exit(&sr->sr_mutex);
445b210fedeSGordon Ross 		rc = EINTR;
446b210fedeSGordon Ross 		goto out;
447b210fedeSGordon Ross 	}
448b210fedeSGordon Ross 	sr->sr_state = SMB_REQ_STATE_WAITING_PIPE;
449b210fedeSGordon Ross 	sr->cancel_method = smb_opipe_cancel;
450b210fedeSGordon Ross 	sr->cancel_arg2 = sock;
451b210fedeSGordon Ross 	mutex_exit(&sr->sr_mutex);
4529fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 
4533db3f65cSamw 	/*
45468b2bbf2SGordon Ross 	 * This should block only if there's no data.
45568b2bbf2SGordon Ross 	 * A single call to recvmsg does just that.
45668b2bbf2SGordon Ross 	 * (Intentionaly no recv loop here.)
4573db3f65cSamw 	 */
458b210fedeSGordon Ross 	bzero(&msghdr, sizeof (msghdr));
459b210fedeSGordon Ross 	msghdr.msg_iov = uio->uio_iov;
460b210fedeSGordon Ross 	msghdr.msg_iovlen = uio->uio_iovcnt;
46168b2bbf2SGordon Ross 	rc = ksocket_recvmsg(sock, &msghdr, 0,
46268b2bbf2SGordon Ross 	    &recvcnt, ofile->f_cr);
463b210fedeSGordon Ross 
464b210fedeSGordon Ross 	mutex_enter(&sr->sr_mutex);
465b210fedeSGordon Ross 	sr->cancel_method = NULL;
466b210fedeSGordon Ross 	sr->cancel_arg2 = NULL;
467b210fedeSGordon Ross 	switch (sr->sr_state) {
468b210fedeSGordon Ross 	case SMB_REQ_STATE_WAITING_PIPE:
469b210fedeSGordon Ross 		sr->sr_state = SMB_REQ_STATE_ACTIVE;
470b210fedeSGordon Ross 		break;
471b210fedeSGordon Ross 	case SMB_REQ_STATE_CANCEL_PENDING:
472b210fedeSGordon Ross 		sr->sr_state = SMB_REQ_STATE_CANCELLED;
473b210fedeSGordon Ross 		rc = EINTR;
474b210fedeSGordon Ross 		break;
475b210fedeSGordon Ross 	default:
476b210fedeSGordon Ross 		/* keep rc from above */
477b210fedeSGordon Ross 		break;
478b210fedeSGordon Ross 	}
479b210fedeSGordon Ross 	mutex_exit(&sr->sr_mutex);
480b210fedeSGordon Ross 
48168b2bbf2SGordon Ross 	if (rc != 0)
48268b2bbf2SGordon Ross 		goto out;
4833db3f65cSamw 
48468b2bbf2SGordon Ross 	if (recvcnt == 0) {
48568b2bbf2SGordon Ross 		/* Other side closed. */
48668b2bbf2SGordon Ross 		rc = EPIPE;
48768b2bbf2SGordon Ross 		goto out;
4883db3f65cSamw 	}
48968b2bbf2SGordon Ross 	uio->uio_resid -= recvcnt;
4903db3f65cSamw 
491bce01b59SGordon Ross out:
492bce01b59SGordon Ross 	ksocket_rele(sock);
493bce01b59SGordon Ross 
494bce01b59SGordon Ross 	return (rc);
49568b2bbf2SGordon Ross }
49668b2bbf2SGordon Ross 
497bce01b59SGordon Ross int
498a90cf9f2SGordon Ross smb_opipe_ioctl(smb_request_t *sr, int cmd, void *arg, int *rvalp)
499bce01b59SGordon Ross {
500bce01b59SGordon Ross 	smb_ofile_t *ofile;
501bce01b59SGordon Ross 	smb_opipe_t *opipe;
502bce01b59SGordon Ross 	ksocket_t sock;
503a90cf9f2SGordon Ross 	int rc;
504bce01b59SGordon Ross 
505bce01b59SGordon Ross 	ofile = sr->fid_ofile;
506bce01b59SGordon Ross 	ASSERT(ofile->f_ftype == SMB_FTYPE_MESG_PIPE);
507bce01b59SGordon Ross 	opipe = ofile->f_pipe;
508bce01b59SGordon Ross 	SMB_OPIPE_VALID(opipe);
509bce01b59SGordon Ross 
510bce01b59SGordon Ross 	mutex_enter(&opipe->p_mutex);
511bce01b59SGordon Ross 	sock = opipe->p_socket;
512bce01b59SGordon Ross 	if (sock != NULL)
513bce01b59SGordon Ross 		ksocket_hold(sock);
514bce01b59SGordon Ross 	mutex_exit(&opipe->p_mutex);
515bce01b59SGordon Ross 	if (sock == NULL)
516bce01b59SGordon Ross 		return (EBADF);
517bce01b59SGordon Ross 
518a90cf9f2SGordon Ross 	rc = ksocket_ioctl(sock, cmd, (intptr_t)arg, rvalp, ofile->f_cr);
519bce01b59SGordon Ross 
52068b2bbf2SGordon Ross 	ksocket_rele(sock);
52168b2bbf2SGordon Ross 
52268b2bbf2SGordon Ross 	return (rc);
5233db3f65cSamw }
524a90cf9f2SGordon Ross 
525a90cf9f2SGordon Ross /*
526a90cf9f2SGordon Ross  * Get the smb_attr_t for a named pipe.
527a90cf9f2SGordon Ross  * Caller has already cleared to zero.
528a90cf9f2SGordon Ross  */
529a90cf9f2SGordon Ross int
530a90cf9f2SGordon Ross smb_opipe_getattr(smb_ofile_t *of, smb_attr_t *ap)
531a90cf9f2SGordon Ross {
532a90cf9f2SGordon Ross 
533a90cf9f2SGordon Ross 	if (of->f_pipe == NULL)
534a90cf9f2SGordon Ross 		return (EINVAL);
535a90cf9f2SGordon Ross 
536a90cf9f2SGordon Ross 	ap->sa_vattr.va_type = VFIFO;
537a90cf9f2SGordon Ross 	ap->sa_vattr.va_nlink = 1;
538c5f48fa5SGordon Ross 	ap->sa_vattr.va_nodeid = (uintptr_t)of->f_pipe;
539a90cf9f2SGordon Ross 	ap->sa_dosattr = FILE_ATTRIBUTE_NORMAL;
540c5f48fa5SGordon Ross 	ap->sa_allocsz = SMB_PIPE_MAX_MSGSIZE;
541a90cf9f2SGordon Ross 
542a90cf9f2SGordon Ross 	return (0);
543a90cf9f2SGordon Ross }
544a90cf9f2SGordon Ross 
545a90cf9f2SGordon Ross int
546a90cf9f2SGordon Ross smb_opipe_getname(smb_ofile_t *of, char *buf, size_t buflen)
547a90cf9f2SGordon Ross {
548a90cf9f2SGordon Ross 	smb_opipe_t *opipe;
549a90cf9f2SGordon Ross 
550a90cf9f2SGordon Ross 	if ((opipe = of->f_pipe) == NULL)
551a90cf9f2SGordon Ross 		return (EINVAL);
552a90cf9f2SGordon Ross 
553a90cf9f2SGordon Ross 	(void) snprintf(buf, buflen, "\\%s", opipe->p_name);
554a90cf9f2SGordon Ross 	return (0);
555a90cf9f2SGordon Ross }
556a90cf9f2SGordon Ross 
557a90cf9f2SGordon Ross /*
558*55f0a249SGordon Ross  * Handle device type FILE_DEVICE_NAMED_PIPE
559*55f0a249SGordon Ross  * for smb2_ioctl
560a90cf9f2SGordon Ross  */
561a90cf9f2SGordon Ross /* ARGSUSED */
562a90cf9f2SGordon Ross uint32_t
563a90cf9f2SGordon Ross smb_opipe_fsctl(smb_request_t *sr, smb_fsctl_t *fsctl)
564a90cf9f2SGordon Ross {
565a90cf9f2SGordon Ross 	uint32_t status;
566a90cf9f2SGordon Ross 
567*55f0a249SGordon Ross 	if (!STYPE_ISIPC(sr->tid_tree->t_res_type))
568*55f0a249SGordon Ross 		return (NT_STATUS_INVALID_DEVICE_REQUEST);
569*55f0a249SGordon Ross 
570a90cf9f2SGordon Ross 	switch (fsctl->CtlCode) {
571a90cf9f2SGordon Ross 	case FSCTL_PIPE_TRANSCEIVE:
572a90cf9f2SGordon Ross 		status = smb_opipe_transceive(sr, fsctl);
573a90cf9f2SGordon Ross 		break;
574a90cf9f2SGordon Ross 
575a90cf9f2SGordon Ross 	case FSCTL_PIPE_PEEK:
576a90cf9f2SGordon Ross 	case FSCTL_PIPE_WAIT:
577a90cf9f2SGordon Ross 		/* XXX todo */
578a90cf9f2SGordon Ross 		status = NT_STATUS_NOT_SUPPORTED;
579a90cf9f2SGordon Ross 		break;
580a90cf9f2SGordon Ross 
581a90cf9f2SGordon Ross 	default:
582a90cf9f2SGordon Ross 		ASSERT(!"CtlCode");
583a90cf9f2SGordon Ross 		status = NT_STATUS_INTERNAL_ERROR;
584a90cf9f2SGordon Ross 		break;
585a90cf9f2SGordon Ross 	}
586a90cf9f2SGordon Ross 
587a90cf9f2SGordon Ross 	return (status);
588a90cf9f2SGordon Ross }
589a90cf9f2SGordon Ross 
590*55f0a249SGordon Ross uint32_t
591a90cf9f2SGordon Ross smb_opipe_transceive(smb_request_t *sr, smb_fsctl_t *fsctl)
592a90cf9f2SGordon Ross {
593a90cf9f2SGordon Ross 	smb_vdb_t	vdb;
594a90cf9f2SGordon Ross 	smb_ofile_t	*ofile;
595a90cf9f2SGordon Ross 	struct mbuf	*mb;
596a90cf9f2SGordon Ross 	uint32_t	status;
597a90cf9f2SGordon Ross 	int		len, rc;
598a90cf9f2SGordon Ross 
599a90cf9f2SGordon Ross 	/*
600a90cf9f2SGordon Ross 	 * Caller checked that this is the IPC$ share,
601a90cf9f2SGordon Ross 	 * and that this call has a valid open handle.
602a90cf9f2SGordon Ross 	 * Just check the type.
603a90cf9f2SGordon Ross 	 */
604a90cf9f2SGordon Ross 	ofile = sr->fid_ofile;
605a90cf9f2SGordon Ross 	if (ofile->f_ftype != SMB_FTYPE_MESG_PIPE)
606a90cf9f2SGordon Ross 		return (NT_STATUS_INVALID_HANDLE);
607a90cf9f2SGordon Ross 
608a90cf9f2SGordon Ross 	rc = smb_mbc_decodef(fsctl->in_mbc, "#B",
609a90cf9f2SGordon Ross 	    fsctl->InputCount, &vdb);
610a90cf9f2SGordon Ross 	if (rc != 0) {
611a90cf9f2SGordon Ross 		/* Not enough data sent. */
612a90cf9f2SGordon Ross 		return (NT_STATUS_INVALID_PARAMETER);
613a90cf9f2SGordon Ross 	}
614a90cf9f2SGordon Ross 
615a90cf9f2SGordon Ross 	rc = smb_opipe_write(sr, &vdb.vdb_uio);
616a90cf9f2SGordon Ross 	if (rc != 0)
617a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
618a90cf9f2SGordon Ross 
619a90cf9f2SGordon Ross 	vdb.vdb_tag = 0;
620a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_iov = &vdb.vdb_iovec[0];
621a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_iovcnt = MAX_IOVEC;
622a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_segflg = UIO_SYSSPACE;
623a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_extflg = UIO_COPY_DEFAULT;
624a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_loffset = (offset_t)0;
625a90cf9f2SGordon Ross 	vdb.vdb_uio.uio_resid = fsctl->MaxOutputResp;
626a90cf9f2SGordon Ross 	mb = smb_mbuf_allocate(&vdb.vdb_uio);
627a90cf9f2SGordon Ross 
628a90cf9f2SGordon Ross 	rc = smb_opipe_read(sr, &vdb.vdb_uio);
629a90cf9f2SGordon Ross 	if (rc != 0) {
630a90cf9f2SGordon Ross 		m_freem(mb);
631a90cf9f2SGordon Ross 		return (smb_errno2status(rc));
632a90cf9f2SGordon Ross 	}
633a90cf9f2SGordon Ross 
634a90cf9f2SGordon Ross 	len = fsctl->MaxOutputResp - vdb.vdb_uio.uio_resid;
635a90cf9f2SGordon Ross 	smb_mbuf_trim(mb, len);
636a90cf9f2SGordon Ross 	MBC_ATTACH_MBUF(fsctl->out_mbc, mb);
637a90cf9f2SGordon Ross 
638a90cf9f2SGordon Ross 	/*
639a90cf9f2SGordon Ross 	 * If the output buffer holds a partial pipe message,
640a90cf9f2SGordon Ross 	 * we're supposed to return NT_STATUS_BUFFER_OVERFLOW.
641a90cf9f2SGordon Ross 	 * As we don't have message boundary markers, the best
642a90cf9f2SGordon Ross 	 * we can do is return that status when we have ALL of:
643a90cf9f2SGordon Ross 	 *	Output buffer was < SMB_PIPE_MAX_MSGSIZE
644a90cf9f2SGordon Ross 	 *	We filled the output buffer (resid==0)
645a90cf9f2SGordon Ross 	 *	There's more data (ioctl FIONREAD)
646a90cf9f2SGordon Ross 	 */
647a90cf9f2SGordon Ross 	status = NT_STATUS_SUCCESS;
648a90cf9f2SGordon Ross 	if (fsctl->MaxOutputResp < SMB_PIPE_MAX_MSGSIZE &&
649a90cf9f2SGordon Ross 	    vdb.vdb_uio.uio_resid == 0) {
650a90cf9f2SGordon Ross 		int nread = 0, trval;
651a90cf9f2SGordon Ross 		rc = smb_opipe_ioctl(sr, FIONREAD, &nread, &trval);
652a90cf9f2SGordon Ross 		if (rc == 0 && nread != 0)
653a90cf9f2SGordon Ross 			status = NT_STATUS_BUFFER_OVERFLOW;
654a90cf9f2SGordon Ross 	}
655a90cf9f2SGordon Ross 
656a90cf9f2SGordon Ross 	return (status);
657a90cf9f2SGordon Ross }
658