17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#include <sys/types.h>
347c478bdstevel@tonic-gate#include <sys/sysmacros.h>
357c478bdstevel@tonic-gate#include <sys/param.h>
367c478bdstevel@tonic-gate#include <sys/systm.h>
377c478bdstevel@tonic-gate#include <sys/errno.h>
387c478bdstevel@tonic-gate#include <sys/vnode.h>
397c478bdstevel@tonic-gate#include <sys/file.h>
407c478bdstevel@tonic-gate#include <sys/proc.h>
417c478bdstevel@tonic-gate#include <sys/stropts.h>
427c478bdstevel@tonic-gate#include <sys/stream.h>
437c478bdstevel@tonic-gate#include <sys/strsubr.h>
447c478bdstevel@tonic-gate#include <sys/fs/fifonode.h>
457c478bdstevel@tonic-gate#include <sys/socket.h>
467c478bdstevel@tonic-gate#include <sys/socketvar.h>
477c478bdstevel@tonic-gate#include <sys/debug.h>
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate/*
507c478bdstevel@tonic-gate * STREAMS system calls.
517c478bdstevel@tonic-gate */
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gateint getmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *flagsp);
547c478bdstevel@tonic-gateint putmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int flags);
557c478bdstevel@tonic-gateint getpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *prip,
567c478bdstevel@tonic-gate    int *flagsp);
577c478bdstevel@tonic-gateint putpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int pri,
587c478bdstevel@tonic-gate    int flags);
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gatestatic int msgio(int fdes, struct strbuf *ctl, struct strbuf *data, int *rval,
617c478bdstevel@tonic-gate    int mode, unsigned char *prip, int *flagsp);
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gateint
647c478bdstevel@tonic-gategetmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *flagsp)
657c478bdstevel@tonic-gate{
667c478bdstevel@tonic-gate	int error;
677c478bdstevel@tonic-gate	int localflags;
687c478bdstevel@tonic-gate	int realflags = 0;
697c478bdstevel@tonic-gate	unsigned char pri = 0;
707c478bdstevel@tonic-gate	int rv = 0;
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate	/*
737c478bdstevel@tonic-gate	 * Convert between old flags (localflags) and new flags (realflags).
747c478bdstevel@tonic-gate	 */
757c478bdstevel@tonic-gate	if (copyin(flagsp, &localflags, sizeof (*flagsp)))
767c478bdstevel@tonic-gate		return (set_errno(EFAULT));
777c478bdstevel@tonic-gate	switch (localflags) {
787c478bdstevel@tonic-gate	case 0:
797c478bdstevel@tonic-gate		realflags = MSG_ANY;
807c478bdstevel@tonic-gate		break;
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gate	case RS_HIPRI:
837c478bdstevel@tonic-gate		realflags = MSG_HIPRI;
847c478bdstevel@tonic-gate		break;
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gate	default:
877c478bdstevel@tonic-gate		return (set_errno(EINVAL));
887c478bdstevel@tonic-gate	}
897c478bdstevel@tonic-gate
907c478bdstevel@tonic-gate	if ((error = msgio(fdes, ctl, data, &rv, FREAD, &pri,
917c478bdstevel@tonic-gate	    &realflags)) == 0) {
927c478bdstevel@tonic-gate		/*
937c478bdstevel@tonic-gate		 * massage realflags based on localflags.
947c478bdstevel@tonic-gate		 */
957c478bdstevel@tonic-gate		if (realflags == MSG_HIPRI)
967c478bdstevel@tonic-gate			localflags = RS_HIPRI;
977c478bdstevel@tonic-gate		else
987c478bdstevel@tonic-gate			localflags = 0;
997c478bdstevel@tonic-gate		if (copyout(&localflags, flagsp, sizeof (*flagsp)))
1007c478bdstevel@tonic-gate			error = EFAULT;
1017c478bdstevel@tonic-gate	}
1027c478bdstevel@tonic-gate	if (error != 0)
1037c478bdstevel@tonic-gate		return (set_errno(error));
1047c478bdstevel@tonic-gate	return (rv);
1057c478bdstevel@tonic-gate}
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gateint
1087c478bdstevel@tonic-gateputmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int flags)
1097c478bdstevel@tonic-gate{
1107c478bdstevel@tonic-gate	unsigned char pri = 0;
1117c478bdstevel@tonic-gate	int realflags;
1127c478bdstevel@tonic-gate	int error;
1137c478bdstevel@tonic-gate	int rv = 0;
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate	switch (flags) {
1167c478bdstevel@tonic-gate	case RS_HIPRI:
1177c478bdstevel@tonic-gate		realflags = MSG_HIPRI;
1187c478bdstevel@tonic-gate		break;
1197c478bdstevel@tonic-gate	case (RS_HIPRI|MSG_XPG4):
1207c478bdstevel@tonic-gate		realflags = MSG_HIPRI|MSG_XPG4;
1217c478bdstevel@tonic-gate		break;
1227c478bdstevel@tonic-gate	case MSG_XPG4:
1237c478bdstevel@tonic-gate		realflags = MSG_BAND|MSG_XPG4;
1247c478bdstevel@tonic-gate		break;
1257c478bdstevel@tonic-gate	case 0:
1267c478bdstevel@tonic-gate		realflags = MSG_BAND;
1277c478bdstevel@tonic-gate		break;
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate	default:
1307c478bdstevel@tonic-gate		return (set_errno(EINVAL));
1317c478bdstevel@tonic-gate	}
1327c478bdstevel@tonic-gate	error = msgio(fdes, ctl, data, &rv, FWRITE, &pri, &realflags);
1337c478bdstevel@tonic-gate	if (error != 0)
1347c478bdstevel@tonic-gate		return (set_errno(error));
1357c478bdstevel@tonic-gate	return (rv);
1367c478bdstevel@tonic-gate}
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gateint
1407c478bdstevel@tonic-gategetpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int *prip,
1417c478bdstevel@tonic-gate    int *flagsp)
1427c478bdstevel@tonic-gate{
1437c478bdstevel@tonic-gate	int error;
1447c478bdstevel@tonic-gate	int flags;
1457c478bdstevel@tonic-gate	int intpri;
1467c478bdstevel@tonic-gate	unsigned char pri;
1477c478bdstevel@tonic-gate	int rv = 0;
1487c478bdstevel@tonic-gate
1497c478bdstevel@tonic-gate	if (copyin(flagsp, &flags, sizeof (flags)))
1507c478bdstevel@tonic-gate		return (set_errno(EFAULT));
1517c478bdstevel@tonic-gate	if (copyin(prip, &intpri, sizeof (intpri)))
1527c478bdstevel@tonic-gate		return (set_errno(EFAULT));
1537c478bdstevel@tonic-gate	if ((intpri > 255) || (intpri < 0))
1547c478bdstevel@tonic-gate		return (set_errno(EINVAL));
1557c478bdstevel@tonic-gate	pri = (unsigned char)intpri;
1567c478bdstevel@tonic-gate	error = msgio(fdes, ctl, data, &rv, FREAD, &pri, &flags);
1577c478bdstevel@tonic-gate	if (error != 0)
1587c478bdstevel@tonic-gate		return (set_errno(error));
1597c478bdstevel@tonic-gate	if (copyout(&flags, flagsp, sizeof (flags)))
1607c478bdstevel@tonic-gate		return (set_errno(EFAULT));
1617c478bdstevel@tonic-gate	intpri = (int)pri;
1627c478bdstevel@tonic-gate	if (copyout(&intpri, prip, sizeof (intpri)))
1637c478bdstevel@tonic-gate		return (set_errno(EFAULT));
1647c478bdstevel@tonic-gate	return (rv);
1657c478bdstevel@tonic-gate}
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gateint
1687c478bdstevel@tonic-gateputpmsg(int fdes, struct strbuf *ctl, struct strbuf *data, int intpri,
1697c478bdstevel@tonic-gate    int flags)
1707c478bdstevel@tonic-gate{
1717c478bdstevel@tonic-gate	unsigned char pri;
1727c478bdstevel@tonic-gate	int rv = 0;
1737c478bdstevel@tonic-gate	int error;
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gate	if ((intpri > 255) || (intpri < 0))
1767c478bdstevel@tonic-gate		return (set_errno(EINVAL));
1777c478bdstevel@tonic-gate	pri = (unsigned char)intpri;
1787c478bdstevel@tonic-gate	error = msgio(fdes, ctl, data, &rv, FWRITE, &pri, &flags);
1797c478bdstevel@tonic-gate	if (error != 0)
1807c478bdstevel@tonic-gate		return (set_errno(error));
1817c478bdstevel@tonic-gate	return (rv);
1827c478bdstevel@tonic-gate}
1837c478bdstevel@tonic-gate
1847c478bdstevel@tonic-gate/*
1857c478bdstevel@tonic-gate * Common code for getmsg and putmsg calls: check permissions,
1867c478bdstevel@tonic-gate * copy in args, do preliminary setup, and switch to
1877c478bdstevel@tonic-gate * appropriate stream routine.
1887c478bdstevel@tonic-gate */
1897c478bdstevel@tonic-gatestatic int
1907c478bdstevel@tonic-gatemsgio(int fdes, struct strbuf *ctl, struct strbuf *data, int *rval,
1917c478bdstevel@tonic-gate    int mode, unsigned char *prip, int *flagsp)
1927c478bdstevel@tonic-gate{
1937c478bdstevel@tonic-gate	file_t *fp;
1947c478bdstevel@tonic-gate	vnode_t *vp;
1957c478bdstevel@tonic-gate	struct strbuf msgctl, msgdata;
1967c478bdstevel@tonic-gate	int error;
1977c478bdstevel@tonic-gate	int flag;
1987c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(curthread);
1997c478bdstevel@tonic-gate	rval_t rv;
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate	if ((fp = getf(fdes)) == NULL)
2027c478bdstevel@tonic-gate		return (EBADF);
2037c478bdstevel@tonic-gate	if ((fp->f_flag & mode) == 0) {
2047c478bdstevel@tonic-gate		releasef(fdes);
2057c478bdstevel@tonic-gate		return (EBADF);
2067c478bdstevel@tonic-gate	}
2077c478bdstevel@tonic-gate	vp = fp->f_vnode;
2087c478bdstevel@tonic-gate	if (vp->v_type == VFIFO) {
2097c478bdstevel@tonic-gate		if (vp->v_stream) {
2107c478bdstevel@tonic-gate			/*
2117c478bdstevel@tonic-gate			 * must use sd_vnode, could be named pipe
2127c478bdstevel@tonic-gate			 */
2137c478bdstevel@tonic-gate			(void) fifo_vfastoff(vp->v_stream->sd_vnode);
2147c478bdstevel@tonic-gate		} else {
2157c478bdstevel@tonic-gate			releasef(fdes);
2167c478bdstevel@tonic-gate			return (ENOSTR);
2177c478bdstevel@tonic-gate		}
2187c478bdstevel@tonic-gate	} else if ((vp->v_type != VCHR && vp->v_type != VSOCK) ||
2197c478bdstevel@tonic-gate		    vp->v_stream == NULL) {
2207c478bdstevel@tonic-gate		releasef(fdes);
2217c478bdstevel@tonic-gate		return (ENOSTR);
2227c478bdstevel@tonic-gate	}
2237c478bdstevel@tonic-gate	if ((ctl != NULL) &&
2247c478bdstevel@tonic-gate	    copyin(ctl, &msgctl, sizeof (struct strbuf))) {
2257c478bdstevel@tonic-gate		releasef(fdes);
2267c478bdstevel@tonic-gate		return (EFAULT);
2277c478bdstevel@tonic-gate	}
2287c478bdstevel@tonic-gate	if ((data != NULL) &&
2297c478bdstevel@tonic-gate	    copyin(data, &msgdata, sizeof (struct strbuf))) {
2307c478bdstevel@tonic-gate		releasef(fdes);
2317c478bdstevel@tonic-gate		return (EFAULT);
2327c478bdstevel@tonic-gate	}
2337c478bdstevel@tonic-gate
2347c478bdstevel@tonic-gate	if (mode == FREAD) {
2357c478bdstevel@tonic-gate		if (ctl == NULL)
2367c478bdstevel@tonic-gate			msgctl.maxlen = -1;
2377c478bdstevel@tonic-gate		if (data == NULL)
2387c478bdstevel@tonic-gate			msgdata.maxlen = -1;
2397c478bdstevel@tonic-gate		flag = fp->f_flag;
2407c478bdstevel@tonic-gate		rv.r_val1 = 0;
2417c478bdstevel@tonic-gate		if (vp->v_type == VSOCK) {
2427c478bdstevel@tonic-gate			error = sock_getmsg(vp, &msgctl, &msgdata, prip,
2437c478bdstevel@tonic-gate			    flagsp, flag, &rv);
2447c478bdstevel@tonic-gate		} else {
2457c478bdstevel@tonic-gate			error = strgetmsg(vp, &msgctl, &msgdata, prip,
2467c478bdstevel@tonic-gate			    flagsp, flag, &rv);
2477c478bdstevel@tonic-gate		}
2487c478bdstevel@tonic-gate		*rval = rv.r_val1;
2497c478bdstevel@tonic-gate		if (error != 0) {
2507c478bdstevel@tonic-gate			releasef(fdes);
2517c478bdstevel@tonic-gate			return (error);
2527c478bdstevel@tonic-gate		}
2537c478bdstevel@tonic-gate		if (lwp != NULL)
2547c478bdstevel@tonic-gate			lwp->lwp_ru.msgrcv++;
2557c478bdstevel@tonic-gate		if (((ctl != NULL) &&
2567c478bdstevel@tonic-gate		    copyout(&msgctl, ctl, sizeof (struct strbuf))) ||
2577c478bdstevel@tonic-gate		    ((data != NULL) &&
2587c478bdstevel@tonic-gate		    copyout(&msgdata, data, sizeof (struct strbuf)))) {
2597c478bdstevel@tonic-gate			releasef(fdes);
2607c478bdstevel@tonic-gate			return (EFAULT);
2617c478bdstevel@tonic-gate		}
2627c478bdstevel@tonic-gate		releasef(fdes);
2637c478bdstevel@tonic-gate		return (0);
2647c478bdstevel@tonic-gate	}
2657c478bdstevel@tonic-gate
2667c478bdstevel@tonic-gate	/*
2677c478bdstevel@tonic-gate	 * FWRITE case
2687c478bdstevel@tonic-gate	 */
2697c478bdstevel@tonic-gate	if (ctl == NULL)
2707c478bdstevel@tonic-gate		msgctl.len = -1;
2717c478bdstevel@tonic-gate	if (data == NULL)
2727c478bdstevel@tonic-gate		msgdata.len = -1;
2737c478bdstevel@tonic-gate	flag = fp->f_flag;
2747c478bdstevel@tonic-gate	if (vp->v_type == VSOCK) {
2757c478bdstevel@tonic-gate		error = sock_putmsg(vp, &msgctl, &msgdata, *prip, *flagsp,
2767c478bdstevel@tonic-gate		    flag);
2777c478bdstevel@tonic-gate	} else {
2787c478bdstevel@tonic-gate		error = strputmsg(vp, &msgctl, &msgdata, *prip, *flagsp, flag);
2797c478bdstevel@tonic-gate	}
2807c478bdstevel@tonic-gate	releasef(fdes);
2817c478bdstevel@tonic-gate	if (error == 0 && lwp != NULL)
2827c478bdstevel@tonic-gate		lwp->lwp_ru.msgsnd++;
2837c478bdstevel@tonic-gate	return (error);
2847c478bdstevel@tonic-gate}
2857c478bdstevel@tonic-gate
2867c478bdstevel@tonic-gate
2877c478bdstevel@tonic-gate#if defined(_LP64) && defined(_SYSCALL32)
2887c478bdstevel@tonic-gate
2897c478bdstevel@tonic-gatestatic int msgio32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data,
2907c478bdstevel@tonic-gate    int *rval, int mode, unsigned char *prip, int *flagsp);
2917c478bdstevel@tonic-gate
2927c478bdstevel@tonic-gateint
2937c478bdstevel@tonic-gategetmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t *flagsp)
2947c478bdstevel@tonic-gate{
2957c478bdstevel@tonic-gate	int error;
2967c478bdstevel@tonic-gate	int32_t localflags;
2977c478bdstevel@tonic-gate	int realflags = 0;
2987c478bdstevel@tonic-gate	unsigned char pri = 0;
2997c478bdstevel@tonic-gate	int rv = 0;
3007c478bdstevel@tonic-gate
3017c478bdstevel@tonic-gate	/*
3027c478bdstevel@tonic-gate	 * Convert between old flags (localflags) and new flags (realflags).
3037c478bdstevel@tonic-gate	 */
3047c478bdstevel@tonic-gate	if (copyin(flagsp, &localflags, sizeof (*flagsp)))
3057c478bdstevel@tonic-gate		return (set_errno(EFAULT));
3067c478bdstevel@tonic-gate	switch (localflags) {
3077c478bdstevel@tonic-gate	case 0:
3087c478bdstevel@tonic-gate		realflags = MSG_ANY;
3097c478bdstevel@tonic-gate		break;
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate	case RS_HIPRI:
3127c478bdstevel@tonic-gate		realflags = MSG_HIPRI;
3137c478bdstevel@tonic-gate		break;
3147c478bdstevel@tonic-gate
3157c478bdstevel@tonic-gate	default:
3167c478bdstevel@tonic-gate		return (set_errno(EINVAL));
3177c478bdstevel@tonic-gate	}
3187c478bdstevel@tonic-gate
3197c478bdstevel@tonic-gate	if ((error = msgio32(fdes, ctl, data, &rv, FREAD, &pri,
3207c478bdstevel@tonic-gate	    &realflags)) == 0) {
3217c478bdstevel@tonic-gate		/*
3227c478bdstevel@tonic-gate		 * massage realflags based on localflags.
3237c478bdstevel@tonic-gate		 */
3247c478bdstevel@tonic-gate		if (realflags == MSG_HIPRI)
3257c478bdstevel@tonic-gate			localflags = RS_HIPRI;
3267c478bdstevel@tonic-gate		else
3277c478bdstevel@tonic-gate			localflags = 0;
3287c478bdstevel@tonic-gate		if (copyout(&localflags, flagsp, sizeof (*flagsp)))
3297c478bdstevel@tonic-gate			error = EFAULT;
3307c478bdstevel@tonic-gate	}
3317c478bdstevel@tonic-gate	if (error != 0)
3327c478bdstevel@tonic-gate		return (set_errno(error));
3337c478bdstevel@tonic-gate	return (rv);
3347c478bdstevel@tonic-gate}
3357c478bdstevel@tonic-gate
3367c478bdstevel@tonic-gateint
3377c478bdstevel@tonic-gateputmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t flags)
3387c478bdstevel@tonic-gate{
3397c478bdstevel@tonic-gate	unsigned char pri = 0;
3407c478bdstevel@tonic-gate	int realflags;
3417c478bdstevel@tonic-gate	int error;
3427c478bdstevel@tonic-gate	int rv = 0;
3437c478bdstevel@tonic-gate
3447c478bdstevel@tonic-gate	switch (flags) {
3457c478bdstevel@tonic-gate	case RS_HIPRI:
3467c478bdstevel@tonic-gate		realflags = MSG_HIPRI;
3477c478bdstevel@tonic-gate		break;
3487c478bdstevel@tonic-gate	case (RS_HIPRI|MSG_XPG4):
3497c478bdstevel@tonic-gate		realflags = MSG_HIPRI|MSG_XPG4;
3507c478bdstevel@tonic-gate		break;
3517c478bdstevel@tonic-gate	case MSG_XPG4:
3527c478bdstevel@tonic-gate		realflags = MSG_BAND|MSG_XPG4;
3537c478bdstevel@tonic-gate		break;
3547c478bdstevel@tonic-gate	case 0:
3557c478bdstevel@tonic-gate		realflags = MSG_BAND;
3567c478bdstevel@tonic-gate		break;
3577c478bdstevel@tonic-gate
3587c478bdstevel@tonic-gate	default:
3597c478bdstevel@tonic-gate		return (set_errno(EINVAL));
3607c478bdstevel@tonic-gate	}
3617c478bdstevel@tonic-gate	error = msgio32(fdes, ctl, data, &rv, FWRITE, &pri, &realflags);
3627c478bdstevel@tonic-gate	if (error != 0)
3637c478bdstevel@tonic-gate		return (set_errno(error));
3647c478bdstevel@tonic-gate	return (rv);
3657c478bdstevel@tonic-gate}
3667c478bdstevel@tonic-gate
3677c478bdstevel@tonic-gate
3687c478bdstevel@tonic-gateint
3697c478bdstevel@tonic-gategetpmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t *prip,
3707c478bdstevel@tonic-gate    int32_t *flagsp)
3717c478bdstevel@tonic-gate{
3727c478bdstevel@tonic-gate	int error;
3737c478bdstevel@tonic-gate	int32_t flags;
3747c478bdstevel@tonic-gate	int32_t intpri;
3757c478bdstevel@tonic-gate	unsigned char pri;
3767c478bdstevel@tonic-gate	int rv = 0;
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate	if (copyin(flagsp, &flags, sizeof (*flagsp)))
3797c478bdstevel@tonic-gate		return (set_errno(EFAULT));
3807c478bdstevel@tonic-gate	if (copyin(prip, &intpri, sizeof (intpri)))
3817c478bdstevel@tonic-gate		return (set_errno(EFAULT));
3827c478bdstevel@tonic-gate	if ((intpri > 255) || (intpri < 0))
3837c478bdstevel@tonic-gate		return (set_errno(EINVAL));
3847c478bdstevel@tonic-gate	pri = (unsigned char)intpri;
3857c478bdstevel@tonic-gate	error = msgio32(fdes, ctl, data, &rv, FREAD, &pri, &flags);
3867c478bdstevel@tonic-gate	if (error != 0)
3877c478bdstevel@tonic-gate		return (set_errno(error));
3887c478bdstevel@tonic-gate	if (copyout(&flags, flagsp, sizeof (flags)))
3897c478bdstevel@tonic-gate		return (set_errno(EFAULT));
3907c478bdstevel@tonic-gate	intpri = (int)pri;
3917c478bdstevel@tonic-gate	if (copyout(&intpri, prip, sizeof (intpri)))
3927c478bdstevel@tonic-gate		return (set_errno(EFAULT));
3937c478bdstevel@tonic-gate	return (rv);
3947c478bdstevel@tonic-gate}
3957c478bdstevel@tonic-gate
3967c478bdstevel@tonic-gateint
3977c478bdstevel@tonic-gateputpmsg32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int32_t intpri,
3987c478bdstevel@tonic-gate    int32_t flags)
3997c478bdstevel@tonic-gate{
4007c478bdstevel@tonic-gate	unsigned char pri;
4017c478bdstevel@tonic-gate	int rv = 0;
4027c478bdstevel@tonic-gate	int error;
4037c478bdstevel@tonic-gate
4047c478bdstevel@tonic-gate	if ((intpri > 255) || (intpri < 0))
4057c478bdstevel@tonic-gate		return (set_errno(EINVAL));
4067c478bdstevel@tonic-gate	pri = (unsigned char)intpri;
4077c478bdstevel@tonic-gate	error = msgio32(fdes, ctl, data, &rv, FWRITE, &pri, &flags);
4087c478bdstevel@tonic-gate	if (error != 0)
4097c478bdstevel@tonic-gate		return (set_errno(error));
4107c478bdstevel@tonic-gate	return (rv);
4117c478bdstevel@tonic-gate}
4127c478bdstevel@tonic-gate
4137c478bdstevel@tonic-gate/*
4147c478bdstevel@tonic-gate * Common code for getmsg and putmsg calls: check permissions,
4157c478bdstevel@tonic-gate * copy in args, do preliminary setup, and switch to
4167c478bdstevel@tonic-gate * appropriate stream routine.
4177c478bdstevel@tonic-gate */
4187c478bdstevel@tonic-gatestatic int
4197c478bdstevel@tonic-gatemsgio32(int fdes, struct strbuf32 *ctl, struct strbuf32 *data, int *rval,
4207c478bdstevel@tonic-gate    int mode, unsigned char *prip, int *flagsp)
4217c478bdstevel@tonic-gate{
4227c478bdstevel@tonic-gate	file_t *fp;
4237c478bdstevel@tonic-gate	vnode_t *vp;
4247c478bdstevel@tonic-gate	struct strbuf32 msgctl32, msgdata32;
4257c478bdstevel@tonic-gate	struct strbuf msgctl, msgdata;
4267c478bdstevel@tonic-gate	int error;
4277c478bdstevel@tonic-gate	int flag;
4287c478bdstevel@tonic-gate	klwp_t *lwp = ttolwp(curthread);
4297c478bdstevel@tonic-gate	rval_t rv;
4307c478bdstevel@tonic-gate
4317c478bdstevel@tonic-gate	if ((fp = getf(fdes)) == NULL)
4327c478bdstevel@tonic-gate		return (EBADF);
4337c478bdstevel@tonic-gate	if ((fp->f_flag & mode) == 0) {
4347c478bdstevel@tonic-gate		releasef(fdes);
4357c478bdstevel@tonic-gate		return (EBADF);
4367c478bdstevel@tonic-gate	}
4377c478bdstevel@tonic-gate	vp = fp->f_vnode;
4387c478bdstevel@tonic-gate	if (vp->v_type == VFIFO) {
4397c478bdstevel@tonic-gate		if (vp->v_stream) {
4407c478bdstevel@tonic-gate			/*
4417c478bdstevel@tonic-gate			 * must use sd_vnode, could be named pipe
4427c478bdstevel@tonic-gate			 */
4437c478bdstevel@tonic-gate			(void) fifo_vfastoff(vp->v_stream->sd_vnode);
4447c478bdstevel@tonic-gate		} else {
4457c478bdstevel@tonic-gate			releasef(fdes);
4467c478bdstevel@tonic-gate			return (ENOSTR);
4477c478bdstevel@tonic-gate		}
4487c478bdstevel@tonic-gate	} else if ((vp->v_type != VCHR && vp->v_type != VSOCK) ||
4497c478bdstevel@tonic-gate		    vp->v_stream == NULL) {
4507c478bdstevel@tonic-gate		releasef(fdes);
4517c478bdstevel@tonic-gate		return (ENOSTR);
4527c478bdstevel@tonic-gate	}
4537c478bdstevel@tonic-gate	if (ctl != NULL) {
4547c478bdstevel@tonic-gate		if (copyin(ctl, &msgctl32, sizeof (msgctl32))) {
4557c478bdstevel@tonic-gate			releasef(fdes);
4567c478bdstevel@tonic-gate			return (EFAULT);
4577c478bdstevel@tonic-gate		}
4587c478bdstevel@tonic-gate		msgctl.len = msgctl32.len;
4597c478bdstevel@tonic-gate		msgctl.maxlen = msgctl32.maxlen;
4607c478bdstevel@tonic-gate		msgctl.buf = (caddr_t)(uintptr_t)msgctl32.buf;
4617c478bdstevel@tonic-gate	}
4627c478bdstevel@tonic-gate	if (data != NULL) {
4637c478bdstevel@tonic-gate		if (copyin(data, &msgdata32, sizeof (msgdata32))) {
4647c478bdstevel@tonic-gate			releasef(fdes);
4657c478bdstevel@tonic-gate			return (EFAULT);
4667c478bdstevel@tonic-gate		}
4677c478bdstevel@tonic-gate		msgdata.len = msgdata32.len;
4687c478bdstevel@tonic-gate		msgdata.maxlen = msgdata32.maxlen;
4697c478bdstevel@tonic-gate		msgdata.buf = (caddr_t)(uintptr_t)msgdata32.buf;
4707c478bdstevel@tonic-gate	}
4717c478bdstevel@tonic-gate
4727c478bdstevel@tonic-gate	if (mode == FREAD) {
4737c478bdstevel@tonic-gate		if (ctl == NULL)
4747c478bdstevel@tonic-gate			msgctl.maxlen = -1;
4757c478bdstevel@tonic-gate		if (data == NULL)
4767c478bdstevel@tonic-gate			msgdata.maxlen = -1;
4777c478bdstevel@tonic-gate		flag = fp->f_flag;
4787c478bdstevel@tonic-gate		rv.r_val1 = 0;
4797c478bdstevel@tonic-gate		if (vp->v_type == VSOCK) {
4807c478bdstevel@tonic-gate			error = sock_getmsg(vp, &msgctl, &msgdata, prip,
4817c478bdstevel@tonic-gate			    flagsp, flag, &rv);
4827c478bdstevel@tonic-gate		} else {
4837c478bdstevel@tonic-gate			error = strgetmsg(vp, &msgctl, &msgdata, prip,
4847c478bdstevel@tonic-gate			    flagsp, flag, &rv);
4857c478bdstevel@tonic-gate		}
4867c478bdstevel@tonic-gate		*rval = rv.r_val1;
4877c478bdstevel@tonic-gate		if (error != 0) {
4887c478bdstevel@tonic-gate			releasef(fdes);
4897c478bdstevel@tonic-gate			return (error);
4907c478bdstevel@tonic-gate		}
4917c478bdstevel@tonic-gate		if (lwp != NULL)
4927c478bdstevel@tonic-gate			lwp->lwp_ru.msgrcv++;
4937c478bdstevel@tonic-gate		if (ctl != NULL) {
4947c478bdstevel@tonic-gate			/* XX64 - range check */
4957c478bdstevel@tonic-gate			msgctl32.len = msgctl.len;
4967c478bdstevel@tonic-gate			msgctl32.maxlen = msgctl.maxlen;
4977c478bdstevel@tonic-gate			msgctl32.buf = (caddr32_t)(uintptr_t)msgctl.buf;
4987c478bdstevel@tonic-gate			if (copyout(&msgctl32, ctl, sizeof (msgctl32))) {
4997c478bdstevel@tonic-gate				releasef(fdes);
5007c478bdstevel@tonic-gate				return (EFAULT);
5017c478bdstevel@tonic-gate			}
5027c478bdstevel@tonic-gate		}
5037c478bdstevel@tonic-gate		if (data != NULL) {
5047c478bdstevel@tonic-gate			/* XX64 - range check */
5057c478bdstevel@tonic-gate			msgdata32.len = msgdata.len;
5067c478bdstevel@tonic-gate			msgdata32.maxlen = msgdata.maxlen;
5077c478bdstevel@tonic-gate			msgdata32.buf = (caddr32_t)(uintptr_t)msgdata.buf;
5087c478bdstevel@tonic-gate			if (copyout(&msgdata32, data, sizeof (msgdata32))) {
5097c478bdstevel@tonic-gate				releasef(fdes);
5107c478bdstevel@tonic-gate				return (EFAULT);
5117c478bdstevel@tonic-gate			}
5127c478bdstevel@tonic-gate		}
5137c478bdstevel@tonic-gate		releasef(fdes);
5147c478bdstevel@tonic-gate		return (0);
5157c478bdstevel@tonic-gate	}
5167c478bdstevel@tonic-gate
5177c478bdstevel@tonic-gate	/*
5187c478bdstevel@tonic-gate	 * FWRITE case
5197c478bdstevel@tonic-gate	 */
5207c478bdstevel@tonic-gate	if (ctl == NULL)
5217c478bdstevel@tonic-gate		msgctl.len = -1;
5227c478bdstevel@tonic-gate	if (data == NULL)
5237c478bdstevel@tonic-gate		msgdata.len = -1;
5247c478bdstevel@tonic-gate	flag = fp->f_flag;
5257c478bdstevel@tonic-gate	if (vp->v_type == VSOCK) {
5267c478bdstevel@tonic-gate		error = sock_putmsg(vp, &msgctl, &msgdata, *prip, *flagsp,
5277c478bdstevel@tonic-gate		    flag);
5287c478bdstevel@tonic-gate	} else {
5297c478bdstevel@tonic-gate		error = strputmsg(vp, &msgctl, &msgdata, *prip, *flagsp, flag);
5307c478bdstevel@tonic-gate	}
5317c478bdstevel@tonic-gate	releasef(fdes);
5327c478bdstevel@tonic-gate	if (error == 0 && lwp != NULL)
5337c478bdstevel@tonic-gate		lwp->lwp_ru.msgsnd++;
5347c478bdstevel@tonic-gate	return (error);
5357c478bdstevel@tonic-gate}
5367c478bdstevel@tonic-gate
5377c478bdstevel@tonic-gate#endif /* _LP64 && _SYSCALL32 */
538